Skip to content

Commit

Permalink
support xml
Browse files Browse the repository at this point in the history
  • Loading branch information
zwkno1 committed Mar 14, 2019
1 parent e0e2d9a commit b66461a
Show file tree
Hide file tree
Showing 12 changed files with 593 additions and 244 deletions.
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
cmake_minimum_required (VERSION 2.8)

option(ENABLE_TEST "enablle unittest" OFF)
option(ENABLE_PROTOBUF_EXAMPLE "protobuf example" OFF)

set(CMAKE_CXX_STANDARD 17)

add_subdirectory(examples)


option(ENABLE_TEST "enablle unittest" OFF)
option(ENABLE_PROTOBUF_EXAMPLE "protobuf example" OFF)

if (ENABLE_TEST)
enable_testing()
include(GNUInstallDirs)
Expand Down
20 changes: 10 additions & 10 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ project(serialize_example)

include_directories(../include)

add_executable(example_json
example_json.cpp
add_executable(example
example.cpp
)

if(ENABLE_PROTOBUF_EXAMPLE)
Expand All @@ -17,13 +17,13 @@ if(ENABLE_PROTOBUF_EXAMPLE)

file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_executable(example_protobuf
example_protobuf.cpp
${ProtoSources}
${ProtoHeaders}
)
target_link_libraries(example_protobuf ${PROTOBUF_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_executable(example_protobuf
example_protobuf.cpp
${ProtoSources}
${ProtoHeaders}
)
target_link_libraries(example_protobuf ${PROTOBUF_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
endif()

95 changes: 95 additions & 0 deletions examples/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

#include <iostream>
#include <fstream>

// print debug message
//#define ENABLE_SERIALIZATION_TRACE 1

#include <serialization/serialization.h>

struct Person
{
std::string name;
int32_t age;

bool operator==(const Person & other) const
{
return (name == other.name) && (age == other.age);
}

SERIALIZATION_DEFINE(name, age)
};

struct AddressBook
{
std::vector<Person> people;
std::optional<std::string> opt;
std::map<std::string, Person > mapTest;
std::vector<std::vector<std::vector<int> > > vectorTest;

bool operator==(const AddressBook & other) const
{
return (people == other.people)
&& (opt == other.opt)
&& (mapTest == other.mapTest)
&& (vectorTest == other.vectorTest);
}

SERIALIZATION_DEFINE(people, opt, mapTest, vectorTest)
};

int main(int argc, char * argv[])
{
try
{
AddressBook ab = {
{
{"aaaaaaa", 12},
{"qwasasdq", 22331},
{"12qweqweqweqwe", 123123}
},
"opt string",
{
{"a", {"a", 1} },
{"b", {"b", 2} },
{"c", {"c", 3} },
},
{
{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
{ {11, 12, 13}, {14, 15, 16} },
{ {21, 22, 23} },
}
};

// json serialize
serialization::json_oarchive json_oa;
serialization::serialize(json_oa, ab);
std::cout << "json serilize result: " << json_oa.data() << std::endl;

// json unserialize
AddressBook ab2;
serialization::json_iarchive json_ia;
json_ia.load_data(json_oa.data());
serialization::unserialize(json_ia, ab2);
std::cout << "json unserilize result: " << (ab == ab2 ? "success" : "fail") << std::endl;

// xml serialize
serialization::xml_oarchive xml_oa;
serialization::serialize(xml_oa, ab);
std::string xml_str = xml_oa.data();
std::cout << "xml serilize result: " << xml_str << std::endl;

// xml unserialize
AddressBook ab3;
serialization::xml_iarchive xml_ia;
xml_ia.load_data(&xml_str[0]);
serialization::unserialize(xml_ia, ab3);
std::cout << "xml unserilize result: " << (ab == ab3 ? "success" : "fail") << std::endl;

}
catch (serialization::serialization_error & err)
{
std::cout << err.where() << ":" << err.what() << std::endl;
}
}

74 changes: 0 additions & 74 deletions examples/example_json.cpp

This file was deleted.

22 changes: 15 additions & 7 deletions include/serialization/detail/json_iarchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,31 @@ class json_iarchive
size_type sequence_size() const
{
if(stack_.empty() || (!stack_.top()->IsArray()))
return 0;
throw_serialization_error("expect array", "");
return stack_.top()->GetArray().Size();
}

size_type load_sequence_start() const
{
if(stack_.empty() || (!stack_.top()->IsArray()))
throw_serialization_error("expect array", "json");
return stack_.top()->GetArray().Size();
}

bool load_sequence_start(size_type i) const
void load_sequence_end() const
{
}

void load_sequence_item_start(size_type i) const
{
serialization_trace trace(__func__, "json");
assert(!stack_.empty());
if(stack_.empty() || (!stack_.top()->IsArray()))
throw_serialization_error("expect array", "");
if(i >= stack_.top()->GetArray().Size())
return false;
throw_serialization_error("array size", "json");
stack_.push(&stack_.top()->GetArray()[i]);
return true;
}

void load_sequence_end() const
void load_sequence_item_end() const
{
serialization_trace trace(__func__, "json");
assert(!stack_.empty());
Expand Down
32 changes: 20 additions & 12 deletions include/serialization/detail/json_oarchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class json_oarchive
{
}

const char * data() const
{
return buffer_.GetString();
}

size_t size() const
{
return buffer_.GetSize();
}

void save_key_start(const char * key)
{
serialization_trace trace(__func__, key);
Expand All @@ -41,7 +51,7 @@ class json_oarchive
writer_.EndObject();
}

void save_sequence_start()
void save_sequence_start(std::size_t size)
{
serialization_trace trace(__func__, "json");
writer_.StartArray();
Expand All @@ -53,6 +63,14 @@ class json_oarchive
writer_.EndArray();
}

void save_sequence_item_start()
{
}

void save_sequence_item_end()
{
}

void save(bool v)
{
writer_.Bool(v);
Expand Down Expand Up @@ -100,18 +118,8 @@ class json_oarchive
serialization_trace trace(__func__, "json");
}

const char * data() const
{
return buffer_.GetString();
}

size_t size() const
{
return buffer_.GetSize();
}

private:
rapidjson::StringBuffer buffer_;
rapidjson::StringBuffer buffer_;

rapidjson::Writer<rapidjson::StringBuffer> writer_;
};
Expand Down
2 changes: 1 addition & 1 deletion include/serialization/detail/serialization_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class serialization_error: public std::exception
const char *m_where;
};

void throw_serialization_error(const char * where, const char * what)
inline void throw_serialization_error(const char * where, const char * what)
{
throw serialization_error(what, where);
}
Expand Down
Loading

0 comments on commit b66461a

Please sign in to comment.