How to serialize/deserialize custom scalar type? #3435
-
For example, I defined a custom email type (email_t) here, but it cannot compile,
please help, thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
You'll either need to add a default constructor to your type or use a different signature for the deserialization function (see https://json.nlohmann.me/features/arbitrary_types/#how-can-i-use-get-for-non-default-constructiblenon-copyable-types). Additionally, this is ambiguous. if (j.is_null())
{
e = {}; // <-- ambiguous
}
else
{
e = j.get<std::string>();
} Rewriting it this way should do what you want: if (!j.is_null())
{
e = j.get<std::string>();
} |
Beta Was this translation helpful? Give feedback.
-
You shouldn't need this
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the advice, I modified the code as below, but it still cannot compile, can anybody show me a correct example?
The error is:
|
Beta Was this translation helpful? Give feedback.
-
After some test, finally I got the working solution, It may be an example for other people.
thanks. |
Beta Was this translation helpful? Give feedback.
After some test, finally I got the working solution, It may be an example for other people.