Convert from RapidJson to Nlohmann #3595
-
Hi, How can i convert my code from rapidjson to nlohmann?
My current stage is this (i don't know if is the better way):
And how i can iterate when detect an array like in this line Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 30 replies
-
Have a look at https://json.nlohmann.me/features/arbitrary_types/. With conversion functions for auto accessPackages = std::vector<AccessPackage>{};
accessPackages.reserve(value.size());
for (auto &valueAccessPackage : value)
{
if (valueAccessPackage.is_object())
accessPackages.emplace_back(valueAccessPackage.get<AccessPackage>());
} |
Beta Was this translation helpful? Give feedback.
-
Nice. But conversion can be more "smart"? Because in my case, the API sometimes return the same field as string and as int64_t, for example. Can i need check first if it is string or int64_t before get the value. Is possible do it on |
Beta Was this translation helpful? Give feedback.
-
Im trying use it as array/vector as suggested here. But i get error:
Error: |
Beta Was this translation helpful? Give feedback.
-
Works for me. https://godbolt.org/z/rh7qqzG4x (Also, the |
Beta Was this translation helpful? Give feedback.
-
One more question about my migration. In rapidjson i need test item by item to check if each item exists and is valid. This was made using this methodology to my application don't crash from invalid json response. I made a new convertion here but i don't have experience to understand what is more secure in my case: Version 1:
Version 2:
|
Beta Was this translation helpful? Give feedback.
-
More one thing. I create from/to json for almost models here. I need use try in this case?
I can use only this?
|
Beta Was this translation helpful? Give feedback.
-
I want to thank the @falbrechtskirchinger and @gregmarr for the help. Our library was fully migrated to nlohmann with success. We don't have performance problems on mobile and everything is working nice. The development time with nlohmann is lower and better compared to other libraries. The possibility to create a custom to_json and from_json help me a lot and make easier convert all APIs jsons from text to our models. Thank you guys. |
Beta Was this translation helpful? Give feedback.
Works for me.
https://godbolt.org/z/rh7qqzG4x
(Also, the
std::move()
is of course not needed. I've removed it above.)