-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cc
99 lines (77 loc) · 2.1 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#define ESON_IMPLEMENTATION
#include "eson.h"
#include <iostream>
#include <cstdlib>
#include <cstdio>
static void
ESONTest()
{
eson::Value v;
double dbl = 1.234;
eson::Value vd(dbl);
double dbl2 = 3.4;
eson::Value vd2(dbl2);
int64_t i = 144;
eson::Value ival(i);
std::string name("jojo");
eson::Value sval(name);
char bindata[12];
for (int j = 0; j < 12; j++) {
bindata[j] = static_cast<char>(j);
}
eson::Value bval(reinterpret_cast<const uint8_t*>(bindata), 12);
eson::Object subO;
subO["muda"] = vd2;
eson::Object o;
o["abora"] = vd;
o["dora"] = ival;
o["name"] = sval;
o["bin"] = bval;
o["sub"] = eson::Value(subO); // compound object.
v = eson::Value(o);
// First calcuate required size for serialized data.
int64_t sz = static_cast<int64_t>(v.Size());
uint8_t* buf = new uint8_t[size_t(sz)]; // or use mmap() if sz is large.
uint8_t* ptr = &buf[0];
ptr = v.Serialize(ptr);
assert((ptr-&buf[0]) == sz);
FILE* fp = fopen("output.eson", "wb");
fwrite(buf, 1, static_cast<size_t>(sz), fp);
fclose(fp);
eson::Value ret;
std::string err = eson::Parse(ret, static_cast<const uint8_t*>(buf));
if (!err.empty()) {
std::cout << "err:" << err << std::endl;
}
eson::Value doval = ret.Get("dora");
printf("dora = %f\n", doval.Get<double>());
eson::Value dval = ret.Get("muda");
printf("muda = %f\n", dval.Get<double>());
eson::Value sub = ret.Get("sub");
printf("sub.isObj = %d\n", sub.IsObject());
eson::Binary bin = ret.Get("bin").Get<eson::Binary>();
assert(bin.size == 12);
printf("bin len = %ld\n", bin.size);
for (int j = 0; j < bin.size; j++) {
printf(" bin[%d] = %d\n", j, bin.ptr[j]);
}
if (ret.Get("bin1").IsBinary()) {
eson::Binary bin1 = ret.Get("bin1").Get<eson::Binary>();
printf("bin1.size = %d\n", static_cast<int>(bin1.size));
//assert(bin1.size == 0);
}
assert(ret.Get("bin1").IsBinary() == false);
delete [] buf;
}
int
main(
int argc,
char** argv)
{
(void)argc;
(void)argv;
printf("Testing ESON C++ binding...\n");
ESONTest();
printf("Test DONE\n");
return EXIT_SUCCESS;
}