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

optional fields #2

Open
qwertynat opened this issue Jan 29, 2021 · 2 comments
Open

optional fields #2

qwertynat opened this issue Jan 29, 2021 · 2 comments

Comments

@qwertynat
Copy link

Nice library!

Is this able to deal with JSON which has optional fields?

@bk192077
Copy link
Owner

Thanks )

It's not entirely clear what optional means. If this is the case when there is a field in the c++ structure, but there is no field in json, then this field will be initialized with the default value or the value set by the Default option.

@sven-plastwich
Copy link

sven-plastwich commented Feb 19, 2021

Hey,
yes, great library that helped me getting forward. I have a rest-endpoint (c#) that spits out e.g. the following responses:

  1. { "NullableBool":false, "NullableInt":null }
  2. { "NullableBool":true, NullableInt:1 }
  3. { "NullableBool":null, NullableInt:1 }

So I setup a structure like this:

struct MyStruct {
    std::optional<bool> NullableBool;
    std::optional<int> NullableInt;
};

... and made some assumptions in the StructMappingExceptions (complex-type vs. bool) of the set_bool + set_integral methods and set the value e.g. like this:
set<std::optional<bool>>(o, value, it->second);

I did not dig any deeper yet to check if we could have a type optional<bool> and I do not like my poc - even if it works in all my testcases.

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

3 participants