netheril96 / StaticJSON

Fast, direct and static typed parsing of JSON with C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Minimal example for nested optional JSON fields

matu3ba opened this issue · comments

I am curious, if (de)serializing from and to minimal nested optional JSON fields <-> struct describing the JSON with boolean fields to existence of the optional fields is a supported use case.

If yes, can you give me some pointers on the big picture of how the code would look?
Ideally with a minimal usable example, if it is not too much effort.

Thanks for your effort.

UPDATE: Code for the use case
Assume a JSON schema

{
  "required_field":"data1","optional_field":{"req_field_of_optional":"data2","nested_opt_field":"data3"}
}

which fills

struct json_repr {
  typeof(data1) required_field;
  bool optional_field_isUsed;
  typeof(data2) req_field_of_optional;
  bool nested_opt_field_isUsed;
  typeof(data3) nested_opt_field;
};

Another conforming JSON schema would be

{
  "required_field":"data1"
}

and another one

{
  "required_field":"data1","optional_field":{"req_field_of_optional":"data2"}
}

I'm not sure what the boolean field here is for.

In staticjson, you can use std::unique_ptr<T> std::shared_ptr<T> or std::optional<T> for an optional value.

I'm not sure what the boolean field here is for.

The boolean field indicates that the optional is used. std::optional was introduced in C++17, but upgrading from C++11 is not yet possible for me.

C++11 has std::unique_ptr and std::shared_ptr.