AnCH Framework 0.1
Another C++ Hack Framework
Loading...
Searching...
No Matches
AnCH JSON library documentation

Introduction

AnCH JSON library is JSON serialization and deserialization library for JSON format.

Prerequisites

AnCH JSON is a standalone library.

Installation

make install

Conception

Fields which have to be serialized and deserialized have to be declared in application consuming AnCH JSON library.
Fields registation is performed by implementing the function registerFields(anch::json::JSONMapper<T>&) where T is the type of your class/structure. You can regiter fields directly or functions which take a const reference of T as unique parameter.
For compose type, you have to specified the base type in template (see example below).
Seralization can be performed by calling function anch::json::serialize(T&,std::ostream&) or by retrieving the JSONMapper through JSONFactory

Examples

Serialize structure into JSON format:

struct Test {
std::string _id;
std::string _value;
std::vector<int32_t> _nums;
inline const std::string& getValue() const {
return _value;
}
};
template<>
void
anch::json::registerFields(JSONMapper<Test>& mapper) {
mapper
.registerField("id", &Test::_id)
.registerField("value", std::function<const std::string&(const Test&)>(&Test::getValue))
.registerField<int32_t>("nums", &Test::_nums)
;
}
int main(void) {
Test test;
test._id = "deb94ebc-be28-4899-981a-29199b7a487d";
test._value = "this is a value";
test._nums = {1,2,3,4};
std::ostringstream oss;
std::cout << "Serialized test: " << oss.str() << std::endl;
std::string res = anch::json::serialize(test);
std::cout << "Serialized test as string: " << res << std::endl;
}
void serialize(const T &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS)