r/programming • u/nlohmann • Dec 17 '17
JSON for Modern C++ version 3.0.0 released
https://github.com/nlohmann/json/releases/tag/v3.0.012
u/doom_Oo7 Dec 17 '17
I really wonder why there has to be so many JSON libraries for C++. Two or three why not, but there are more than 30 different libs: https://github.com/miloyip/nativejson-benchmark
3
u/BCosbyDidNothinWrong Dec 18 '17
As with anything, most of them are going to be downright terrible, so maybe a few gems of solid software can rise from the mess.
1
1
u/ggtsu_00 Dec 17 '17
Still no way to validate/verify if a string contains valid json without resorting throwing and catching exceptions?
8
u/nlohmann Dec 17 '17
It's actually a feature of 3.0.0:
We added a non-throwing syntax check (#458): The new accept function returns a Boolean indicating whether the input is proper JSON. We also added a Boolean parameter allow_exceptions to the existing parse functions to return a discarded value in case a syntax error occurs instead of throwing an exception.
-29
u/powdertaker Dec 17 '17
Utterly, completely, pointless.
4
3
u/2402a7b7f239666e4079 Dec 18 '17
How? JSON is used everywhere in 2017. Having good tools to support and use it are critical to a modern language if you expect it to be taken seriously.
0
Dec 17 '17
[deleted]
15
u/ggtsu_00 Dec 17 '17
I'm guessing you come from a Java/C# background? Usually the deserialization code would do some manual object/member variable mapping:
obj.my_string = jsonObj["my_string"].get<string>(); obj.my_int = jsonObj["my_int"].get<int>(); ...It is safer to be explicit this way instead of relying on variable names being automatically cast into serialized key fields using reflection which can often lead to security vulnerabilities overriding the internal variables of a class by deserializing untrusted inputs.
11
u/nlohmann Dec 17 '17
In the library, you can define such a mapping once for each user-defined type (see https://github.com/nlohmann/json#arbitrary-types-conversions) an then just write
Object obj = jsonObj;.21
Dec 17 '17
You basically put the implicit schema into your code by saying "I expect this value to be an int, please return an int, or an error"
2
7
u/pjmlp Dec 17 '17
It is possible to do compile time reflection via template meta-programming, it doesn't look nice, has some limitations, but gets the job done.
Eventually we will get native compile time reflection support, which will allow to write a more simple code, easier to maintain.
Other alternative are schema based tools that generate the code.
Failing that, there is the manual way of writing Serialize()/Deserialize() functions.
4
u/lumberjackninja Dec 17 '17 edited Dec 17 '17
At my previous job, we had a couple of firmware images (written in C99) that were configured/controlled over a JSON REST interface.
I ended up writing a JSON (de)serialization library that used a list of "value descriptors"; basically, a structure that included a value type (int, float, string, object, list), a pointer to storage, and optional pointers for serialization and deserialization functions. The pointers were defined as union types for type consistency, e.g.
typedef union { int32_t* i32; u32_t* u32; float* flt; char* str; void* obj; void* array; } JSONValuePointer;Then, for each type I was interested in making available over the REST interface, I defined a function that would populate a list of value descriptors with the relevant information for an instance of that type.
So, for instance, if this is a type I wanted to JSON-ify:
typedef struct { int32_t turn_on_temperature; int32_t turn_off_temperature; } ThermostatSetPoint;I would define a value descriptor generator function, like so:
void ThermostatSetPoint_get_valdescs(ThermostatSetPoint *target, JSONValueDescriptor *desc_list, size_t num_descs) { if (num_descs >= 2) { desc_list[0].value_type = JSONValueType.i32; desc_list[0].name = "turn_on_temperature"; desc_list[0].target.i32 = &target.turn_on_temperature; // populate rest of descriptors } return; }Nested objects/values were handled by calling their respective descriptor getters and pointing to an offset index in the descriptor list; basically, you end up doing a depth-first flattening the whole tree.
The list of descriptors would be passed to a JSON serializer/deserializer. The other option for design would be to define JSON functions for each type that took a pointer to an instance and just a regular C string, but then keeping track of your position and errors when doing nested parsing was more difficult.
The value descriptor method introduces a lot of boilerplate, but it payed off when introducing new types or changing the definitions of other types.
The whole JSON library was pretty neat, and I wish I still had access to it; it's definitely something I would have put up on github. I may try re-writing something similar in the future, just as an exercise. The architecture was inspired by the JSON C library that Eric S. Raymond wrote for implenting GPSD; I liked the general idea, but wanted a minimum of complexity and resource requirements (again, this was an embedded environment).
2
5
u/official_business Dec 17 '17
How does this handle insanely large json files? I've been using rapidjson and its SAX api to extract data from large json docs on disk. While the SAX-like API can be a bit mindbending at times it's great for using tiny amounts of memory and is blazingly fast.
I've tried other parsers and they load the entire doc into memory which isn't great when the doc is a gig and you only want a tiny piece of that.