Skip to content

How to append a custom data field to nlohmann::json object #4717

Answered by gregmarr
Ju1He1 asked this question in Q&A
Discussion options

You must be logged in to vote

It's because you're writing the datatype in

friend void to_json(nlohmann::json& jsonOuputObject, const MyClass& dataInput)

but trying to read it in

  friend void from_json(const nlohmann::json& inputJson, SharedVariantPtr& outputData)

You need to either put variant_datatype in the Variant class, or do all the loading and saving in the MyClass functions.

You can use a member function in Variant, but you have to create the Variant object in the shared_ptr, call the Variant function yourself passing in the type, and not use the std::shared_ptr<Variant> built-in handling.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Ju1He1
Comment options

Answer selected by Ju1He1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants