Skip to content

Commit 1db0732

Browse files
committed
add j2s test
1 parent a38bb7b commit 1db0732

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

test/j2s/j2s.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "j2s.h" // generated by: gen j2s.proto
2+
#include "co/flag.h"
3+
#include "co/cout.h"
4+
5+
int main(int argc, char** argv) {
6+
flag::parse(argc, argv);
7+
8+
co::Json v;
9+
v.add_member("b", false)
10+
.add_member("i", 23)
11+
.add_member("s", "hello")
12+
.add_member("data", co::Json().add_member("ii", 11).add_member("ss", "good"))
13+
.add_member("ai", co::Json().push_back(1).push_back(2).push_back(3))
14+
.add_member("ao", co::Json().push_back(co::Json().add_member("xx", 88).add_member("yy", "nice")));
15+
16+
co::print("Json v:\n", v, '\n');
17+
18+
xx::XX x;
19+
x.from_json(v);
20+
21+
co::print("x.b: ", x.b);
22+
co::print("x.i: ", x.i);
23+
co::print("x.s: ", x.s);
24+
co::print("x.data.ii: ", x.data.ii);
25+
co::print("x.data.ss: ", x.data.ss);
26+
co::print("x.ai[0]: ", x.ai[0]);
27+
co::print("x.ai[1]: ", x.ai[1]);
28+
co::print("x.ai[2]: ", x.ai[2]);
29+
co::print("x.ao[0].xx: ", x.ao[0].xx);
30+
co::print("x.ao[0].yy: ", x.ao[0].yy);
31+
32+
co::print("\nx.as_json():");
33+
co::print(x.as_json());
34+
35+
return 0;
36+
}

test/j2s/j2s.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Autogenerated.
2+
// DO NOT EDIT. All changes will be undone.
3+
#pragma once
4+
5+
#include "co/json.h"
6+
7+
namespace xx {
8+
9+
struct XX {
10+
struct _unamed_s1 {
11+
int ii;
12+
fastring ss;
13+
14+
void from_json(const co::Json& _x_) {
15+
ii = (int)_x_.get("ii").as_int64();
16+
ss = _x_.get("ss").as_c_str();
17+
}
18+
19+
co::Json as_json() const {
20+
co::Json _x_;
21+
_x_.add_member("ii", ii);
22+
_x_.add_member("ss", ss);
23+
return _x_;
24+
}
25+
};
26+
27+
struct _unamed_s2 {
28+
int xx;
29+
fastring yy;
30+
31+
void from_json(const co::Json& _x_) {
32+
xx = (int)_x_.get("xx").as_int64();
33+
yy = _x_.get("yy").as_c_str();
34+
}
35+
36+
co::Json as_json() const {
37+
co::Json _x_;
38+
_x_.add_member("xx", xx);
39+
_x_.add_member("yy", yy);
40+
return _x_;
41+
}
42+
};
43+
44+
bool b;
45+
int i;
46+
fastring s;
47+
_unamed_s1 data;
48+
co::vector<int> ai;
49+
co::vector<_unamed_s2> ao;
50+
51+
void from_json(const co::Json& _x_) {
52+
b = _x_.get("b").as_bool();
53+
i = (int)_x_.get("i").as_int64();
54+
s = _x_.get("s").as_c_str();
55+
data.from_json(_x_.get("data"));
56+
do {
57+
auto& _unamed_v1 = _x_.get("ai");
58+
for (uint32 i = 0; i < _unamed_v1.array_size(); ++i) {
59+
ai.push_back((int)_unamed_v1[i].as_int64());
60+
}
61+
} while (0);
62+
do {
63+
auto& _unamed_v1 = _x_.get("ao");
64+
for (uint32 i = 0; i < _unamed_v1.array_size(); ++i) {
65+
_unamed_s2 _unamed_v2;
66+
_unamed_v2.from_json(_unamed_v1[i]);
67+
ao.emplace_back(std::move(_unamed_v2));
68+
}
69+
} while (0);
70+
}
71+
72+
co::Json as_json() const {
73+
co::Json _x_;
74+
_x_.add_member("b", b);
75+
_x_.add_member("i", i);
76+
_x_.add_member("s", s);
77+
_x_.add_member("data", data.as_json());
78+
do {
79+
co::Json _unamed_v1;
80+
for (size_t i = 0; i < ai.size(); ++i) {
81+
_unamed_v1.push_back(ai[i]);
82+
}
83+
_x_.add_member("ai", _unamed_v1);
84+
} while (0);
85+
do {
86+
co::Json _unamed_v1;
87+
for (size_t i = 0; i < ao.size(); ++i) {
88+
_unamed_v1.push_back(ao[i].as_json());
89+
}
90+
_x_.add_member("ao", _unamed_v1);
91+
} while (0);
92+
return _x_;
93+
}
94+
};
95+
96+
} // xx

test/j2s/j2s.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package xx
2+
3+
object XX {
4+
bool b
5+
int i
6+
string s
7+
data { // anonymous object, field name can be put ahead
8+
int ii
9+
string ss
10+
}
11+
[int] ai // array of int
12+
ao [{ // array of anonymous object, field name can be put ahead
13+
int xx
14+
string yy
15+
}]
16+
}

0 commit comments

Comments
 (0)