Skip to content

Commit

Permalink
add j2s test
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Sep 9, 2023
1 parent a38bb7b commit 1db0732
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/j2s/j2s.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "j2s.h" // generated by: gen j2s.proto
#include "co/flag.h"
#include "co/cout.h"

int main(int argc, char** argv) {
flag::parse(argc, argv);

co::Json v;
v.add_member("b", false)
.add_member("i", 23)
.add_member("s", "hello")
.add_member("data", co::Json().add_member("ii", 11).add_member("ss", "good"))
.add_member("ai", co::Json().push_back(1).push_back(2).push_back(3))
.add_member("ao", co::Json().push_back(co::Json().add_member("xx", 88).add_member("yy", "nice")));

co::print("Json v:\n", v, '\n');

xx::XX x;
x.from_json(v);

co::print("x.b: ", x.b);
co::print("x.i: ", x.i);
co::print("x.s: ", x.s);
co::print("x.data.ii: ", x.data.ii);
co::print("x.data.ss: ", x.data.ss);
co::print("x.ai[0]: ", x.ai[0]);
co::print("x.ai[1]: ", x.ai[1]);
co::print("x.ai[2]: ", x.ai[2]);
co::print("x.ao[0].xx: ", x.ao[0].xx);
co::print("x.ao[0].yy: ", x.ao[0].yy);

co::print("\nx.as_json():");
co::print(x.as_json());

return 0;
}
96 changes: 96 additions & 0 deletions test/j2s/j2s.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Autogenerated.
// DO NOT EDIT. All changes will be undone.
#pragma once

#include "co/json.h"

namespace xx {

struct XX {
struct _unamed_s1 {
int ii;
fastring ss;

void from_json(const co::Json& _x_) {
ii = (int)_x_.get("ii").as_int64();
ss = _x_.get("ss").as_c_str();
}

co::Json as_json() const {
co::Json _x_;
_x_.add_member("ii", ii);
_x_.add_member("ss", ss);
return _x_;
}
};

struct _unamed_s2 {
int xx;
fastring yy;

void from_json(const co::Json& _x_) {
xx = (int)_x_.get("xx").as_int64();
yy = _x_.get("yy").as_c_str();
}

co::Json as_json() const {
co::Json _x_;
_x_.add_member("xx", xx);
_x_.add_member("yy", yy);
return _x_;
}
};

bool b;
int i;
fastring s;
_unamed_s1 data;
co::vector<int> ai;
co::vector<_unamed_s2> ao;

void from_json(const co::Json& _x_) {
b = _x_.get("b").as_bool();
i = (int)_x_.get("i").as_int64();
s = _x_.get("s").as_c_str();
data.from_json(_x_.get("data"));
do {
auto& _unamed_v1 = _x_.get("ai");
for (uint32 i = 0; i < _unamed_v1.array_size(); ++i) {
ai.push_back((int)_unamed_v1[i].as_int64());
}
} while (0);
do {
auto& _unamed_v1 = _x_.get("ao");
for (uint32 i = 0; i < _unamed_v1.array_size(); ++i) {
_unamed_s2 _unamed_v2;
_unamed_v2.from_json(_unamed_v1[i]);
ao.emplace_back(std::move(_unamed_v2));
}
} while (0);
}

co::Json as_json() const {
co::Json _x_;
_x_.add_member("b", b);
_x_.add_member("i", i);
_x_.add_member("s", s);
_x_.add_member("data", data.as_json());
do {
co::Json _unamed_v1;
for (size_t i = 0; i < ai.size(); ++i) {
_unamed_v1.push_back(ai[i]);
}
_x_.add_member("ai", _unamed_v1);
} while (0);
do {
co::Json _unamed_v1;
for (size_t i = 0; i < ao.size(); ++i) {
_unamed_v1.push_back(ao[i].as_json());
}
_x_.add_member("ao", _unamed_v1);
} while (0);
return _x_;
}
};

} // xx
16 changes: 16 additions & 0 deletions test/j2s/j2s.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xx

object XX {
bool b
int i
string s
data { // anonymous object, field name can be put ahead
int ii
string ss
}
[int] ai // array of int
ao [{ // array of anonymous object, field name can be put ahead
int xx
string yy
}]
}

0 comments on commit 1db0732

Please sign in to comment.