jorgen/json_struct

does parseTo check struct members ?

Closed this issue · 6 comments

for example

struct a{
  int a;
  JS_OBJ(a);
}

struct b{
  int b;
  JS_OBJ(b);
}

a s_a;
s_a.a = 19;
b s_b;
s_b.b = 0;

auto json = JS::serializeStruct(s_a);
JS::ParseContext context(json);
auto error = context.parseTo(s_b);

the code give me no error,
but s_b.b is still zero.

This will not work since there is no metadata on the struct. You will have to add a JS_OBJ macro or a JS_OBJ_EXT.

I think these kind of questions a well suited for in the new "Discussions" forum. Check it out here:
https://github.com/jorgen/json_struct/discussions

This will not work since there is no metadata on the struct. You will have to add a JS_OBJ macro or a JS_OBJ_EXT.

I think these kind of questions a well suited for in the new "Discussions" forum. Check it out here: https://github.com/jorgen/json_struct/discussions

I have add JS_OBJ macro,
the result is same.

Oh! I see what you mean. So the names of the JSON members will be the same as the C++ struct members when you use the JS_OBJ macro. If you don't want to use the same name then you will have to use the JS_MEMBER_WITH_NAME or JS_MEMBER_ALIASES macro style. Its use is described in this example:
https://github.com/jorgen/json_struct/blob/master/examples/02_alias.cpp

So your struct a will make a json object with a member called a "ie: { "a": 19 }, but your struct b has a member called b. And when parsing the JSON into the struct b it cant find a member called a, only a member called b.

Also in your example you are trying to send in the type name, ie a and not s_a and b not s_b.

Also in your example you are trying to send in the type name, ie a and not s_a and b not s_b.

Yes ,you are right.
but i think a user ,like me, want an error when it can NOT find the member called a.

https://github.com/jorgen/json_struct/blob/master/tests/json-struct-optional.cpp

  JS::ParseContext context(json, sizeof(json));
  context.allow_missing_members = false;
  context.allow_unnasigned_required_members = false;

try this.