Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does parseTo check struct members ? #28

Closed
yanzixiang opened this issue Jun 28, 2022 · 6 comments
Closed

does parseTo check struct members ? #28

yanzixiang opened this issue Jun 28, 2022 · 6 comments

Comments

@yanzixiang
Copy link

yanzixiang commented Jun 28, 2022

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.

@jorgen
Copy link
Owner

jorgen commented Jun 28, 2022

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

@jorgen jorgen closed this as completed Jun 28, 2022
@yanzixiang
Copy link
Author

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.

@jorgen
Copy link
Owner

jorgen commented Jun 29, 2022

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.

@jorgen
Copy link
Owner

jorgen commented Jun 29, 2022

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

@yanzixiang
Copy link
Author

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.

@yanzixiang
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants