라이브러리 소스 : https://github.com/jc-lab/JsBsonRPCSerializable
테스트 VS프로젝트 : https://github.com/jc-lab/JsBsonRPCSerializable-testproject
JsBsonRPCSerializable는 C++ 에서 객체를 Serialize/Deserialze 할 수 있게 도와주는 라이브러리 입니다.
Serialize 형식은 Bson 형식을 따르며, JSONObjectMapper 클래스를 통해 json(rapidjson)변환 기능도 지원합니다.
테스트 소스
#include <stdio.h>
#include <string>
#include <map>
#include <list>
#include <vector>
#include "JsBsonRPCSerializable/Serializable.h"
#include "JsBsonRPCSerializable/plugins/JSONObjectMapper.h"
void dump(std::vector<unsigned char> &buffer)
{
size_t i = 0;
size_t len = buffer.size();
for (i = 0; i < len; i++)
{
printf("%02x ", buffer[i]);
}
printf("\n");
}
class TestSubClassB : public JsBsonRPC::Serializable
{
public:
JsBsonRPC::SType<int32_t> a;
JsBsonRPC::SType<std::string> b;
TestSubClassB() : Serializable("test2", 101)
{
this->serializableMapMember("a", a);
this->serializableMapMember("b", b);
}
};
class TestClassA : public JsBsonRPC::Serializable
{
public:
JsBsonRPC::SType<int32_t> a;
JsBsonRPC::SType<int64_t> b;
JsBsonRPC::SType<double> d;
JsBsonRPC::SType<std::string> e;
JsBsonRPC::SType<std::vector<char> > xa; // binary
JsBsonRPC::SType<std::list<std::string> > xb;
JsBsonRPC::SType<std::list< std::vector<char> > > xc;
JsBsonRPC::SType<double> xd;
JsBsonRPC::SType<std::map<std::string, std::string> > xf;
JsBsonRPC::SType<TestSubClassB> sub;
JsBsonRPC::SType<std::map<std::string, TestSubClassB> > submap;
TestClassA() : Serializable("test", 101)
{
this->serializableMapMember("a", a);
this->serializableMapMember("b", b);
this->serializableMapMember("d", d);
this->serializableMapMember("e", e);
this->serializableMapMember("xa", xa);
this->serializableMapMember("xb", xb);
this->serializableMapMember("xc", xc);
this->serializableMapMember("xd", xd);
this->serializableMapMember("xf", xf);
this->serializableMapMember("sub", sub);
this->serializableMapMember("submap", submap);
}
};
int main()
{
std::vector<unsigned char> payload;
std::vector<char> test;
TestClassA testA;
TestClassA testB;
TestClassA testC;
testA.a.set(0xff);
testA.b.set(0x1000000000000002);
testA.d.set(3.14);
testA.e.set("aaaa");
testA.xd.set(1.1);
testA.xa.ref().push_back('1');
testA.xa.ref().push_back('2');
testA.xa.ref().push_back('3');
testA.xa.ref().push_back('4');
testA.xb.ref().push_back("hello");
testA.xb.ref().push_back("world");
test.clear();
test.push_back('1');
test.push_back('1');
test.push_back('1');
testA.xc.ref().push_back(test);
test.clear();
test.push_back('2');
test.push_back('2');
test.push_back('2');
testA.xc.ref().push_back(test);
testA.xf.ref()["aaaa"] = "aaaa";
testA.xf.ref()["bbbb"] = "bbbb";
testA.xf.ref()["cccc"] = "c";
testA.xf.ref()["ee"] = "ad";
testA.xf.ref()["dd"] = "aae";
testA.sub.ref().a.set(10);
testA.sub.ref().b.set("SUB TEXT!");
testA.submap.ref()["a"].a.set(10);
testA.submap.ref()["a"].b.set("20");
testA.submap.ref()["b"].a.set(10);
testA.submap.ref()["b"].b.set("40");
payload.clear();
testA.serialize(payload);
dump(payload);
testB.deserialize(payload);
JsBsonRPC::JSONObjectMapper objectMapper;
std::string jsondata = objectMapper.serialize(&testA);
objectMapper.deserialize(&testC, jsondata);
printf("JSON : %s\n", jsondata.c_str());
return 0;
}
출력결과
34 01 00 00 10 61 00 ff 00 00 00 12 62 00 02 00 00 00 00 00 00 10 01 64 00 1f 85 eb 51 b8 1e 09 40 02 65 00 05 00 00 00 61 61 61 61 00 05 78 61 00 04 00 00 00 00 31 32 33 34 04 78 62 00 1f 00 00 00 02 30 00 06 00 00 00 68 65 6c 6c 6f 00 02 31 00 06 00 00 00 77 6f 72 6c 64 00 00 04 78 63 00 1b 00 00 00 05 30 00 03 00 00 00 00 31 31 31 05 31 00 03 00 00 00 00 32 32 32 00 01 78 64 00 9a 99 99 99 99 99 f1 3f 03 78 66 00 46 00 00 00 02 61 61 61 61 00 05 00 00 00 61 61 61 61 00 02 62 62 62 62 00 05 00 00 00 62 62 62 62 00 02 63 63 63 63 00 02 00 00 00 63 00 02 64 64 00 04 00 00 00 61 61 65 00 02 65 65 00 03 00 00 00 61 64 00 00 03 73 75 62 00 1d 00 00 00 10 61 00 0a 00 00 00 02 62 00 0a 00 00 00 53 55 42 20 54 45 58 54 21 00 00 03 73 75 62 6d 61 70 00 37 00 00 00 03 61 00 16 00 00 00 10 61 00 0a 00 00 00 02 62 00 03 00 00 00 32 30 00 00 03 62 00 16 00 00 00 10 61 00 0a 00 00 00 02 62 00 03 00 00 00 34 30 00 00 00 00
JSON : {"a":255,"b":1152921504606846978,"d":3.14,"e":"aaaa","xa":"MTIzNA==","xb":["hello","world"],"xc":["MTEx","MjIy"],"xd":1.1,"xf":{"aaaa":"aaaa","bbbb":"bbbb","cccc":"c","dd":"aae","ee":"ad"},"sub":{"a":10,"b":"SUB TEXT!"},"submap":{"a":{"a":10,"b":"20"},"b":{"a":10,"b":"40"}}}
'내가만드는것_만든것 > 오픈소스' 카테고리의 다른 글
sqlcipher 에서 mbedtls 사용하기 (0) | 2019.05.08 |
---|---|
[JsCPPBean] C++에서 Java Bean 구현하는 라이브러리 (0) | 2019.04.12 |
Spring-boot HttpInvoker 에서 json 사용하기 (2) | 2019.02.28 |
MS3 (Amazon S3 호환 Object Storage) Spring 서버/라이브러리 (0) | 2018.12.18 |
C++용 Serializable JSONObjectMapper (0) | 2018.11.30 |
댓글