r/cpp • u/nlohmann nlohmann/json • Jun 14 '20
JSON for Modern C++ 3.8.0
https://github.com/nlohmann/json/releases/tag/v3.8.04
u/_Js_Kc_ Jun 15 '20 edited Jul 14 '20
Don't like the magic constructor(s) that much, but it's not a huge problem. If one just remembers to use json::array for arrays, json::object for objects and static_cast<json> for scalars, that's explicit and doesn't silently do anything funky.
That and the dummy hash implementation that allocates (!) probably does more harm than good. edit: no longer true
Still way ahead of the competition in API usability.
3
u/nlohmann nlohmann/json Jul 14 '20
I finally had some time to fix the hash function: https://github.com/nlohmann/json/pull/2269
2
48
10
u/NilacTheGrim Jun 15 '20
I love you /u/nlohmann ! Yours may not be the fastest JSON lib for C++ under the sub. But god-dammit if it isn't the most elegant. Everybody on the planet should strive to design a lib as elegant as yours.
Also thanks for making the object_t and array_t template-able. It's interesting to play around with swapping implementations to do things like e.g. preserve insertion order on objects, etc. Very cool of you my man.
I love your lib. Keep up the great work!
14
u/mintyc Jun 14 '20
This is a high quality library that I point to for its overall structure and package support.
If you want a fast library, rather than rapidjson, maybe look at https://simdjson.org/ which claims and shows benchmarks 2.5x faster than rapidjson
Note: I haven't yet tried it or validated its claims.
16
u/VinnieFalco wg21.org | corosio.org Jun 14 '20
SIMDJSON is wonderful, but do note that it is READ-ONLY. You can't build a DOM in memory and then serialize it. Really, it is not comparable to the other libraries.
9
u/mintyc Jun 14 '20
Thank you.
Any plans for offering a Boost-free version of Boost.JSON because the only reason I don't consider it is the dependency on Boost packaging and other parts of boost. A bit unfair perhaps, but I want libraries and not frameworks moving forward.
12
u/VinnieFalco wg21.org | corosio.org Jun 14 '20
Any plans for offering a Boost-free version of Boost.JSON
It already exists, just define
BOOST_JSON_STANDALONEand compile with C++17 or later, and Boost is not needed. This configuration is tested on CI, it should work today.6
u/quicknir Jun 15 '20
Boost isn't a framework, it's a collection of libraries, some (not all) of which depend on others. That's vastly different. Obviously it's still fair to ask about boost jsons dependencies.
2
u/Contango42 Jun 15 '20
I don't mind if something depends on Boost. I use vcpkg on Windows/Linux (MSVC/gcc/clang) and it just works.
18
u/Ivan171 /std:c++latest enthusiast Jun 14 '20
You forgot to say whether he can do that with Boost.JSON.
13
1
1
u/lednakashim ++C is faster Jun 15 '20
What happens if you try to store a NaN?
std::numeric_limits<T>::quiet_NaN ?
4
u/nlohmann nlohmann/json Jun 15 '20
The value is internally stored as
double, so the NaN is preserved. Formats that support NaN like CBOR serialize it like that. As JSON does not have a NaN value, it is serialized tonull.1
u/lednakashim ++C is faster Jun 15 '20
So when using cereal for sterilization it becomes a text string "nan", is that correct behavior?
4
u/nlohmann nlohmann/json Jun 15 '20
As there is no specified way to serialize NaN, each library must make a decision how to implement this situation. This library decided for `null`, but `"nan"` would be fine as well. In any case, roundtripping is impossible without making assumptions.
4
Jun 15 '20
Personally, I prefer this library's `null` approach. When parsing json to a DOM and then marshaling it to native structure, it avoids having to do an extra string-to-float conversion.
3
u/Contango42 Jun 15 '20
Never heard of this library until now. I use a combination of Python and C++ (mainly C++) and the API for this library is, in a word, beautiful. It warms my heart to see syntax so clean and expressive, I'm sold.
9
Jun 14 '20
do I use it
yes
do I like it?
no
.
.
.
I love it! gonna use this occasion to say: well done sir!
1
Jun 14 '20
I am using it in all my.projects. best json lib so far. One thing, how come json is not in the standard lib?
6
u/nlohmann nlohmann/json Jun 14 '20
I think because it is really a lot of work to standardize it. There were efforts on this once (https://github.com/nlohmann/std_json), but this is a side project of mine, and I rather write code than specification in my free time :)
1
4
u/IronicallySerious Jun 14 '20
This library is godsent for JSON support in C++. I have been using it mostly for easy serialization.
6
u/bishop3000 Jun 14 '20
Header only libs are the best. We use it and it’s great. Extendable as well
-18
u/VinnieFalco wg21.org | corosio.org Jun 14 '20
The default configuration for Boost.JSON is to use static or dynamic linking. Header-only is opt-in.
3
u/TheBrainStone Jun 14 '20
The biggest issue I have with this library is that there’s no error tolerance (like missing top level braces or trailing commas, missing quotes, etc.) and no plans to add support for that either.
6
u/BlueDwarf82 Jun 15 '20
Well, the reason you need to handle that is because other parsers accept it. If every JSON parser rejected it producers of broken JSON would notice and fix it. So you could say that, by not supporting it, this library is helping to make the problem disappear.
You should now go and ask other libraries to stop accepting JSON with trailing commas.
2
u/TheBrainStone Jun 15 '20
The issue aren’t automatically generated files/strings. But much rather user created ones.
11
u/Sopel97 Jun 14 '20
That's because it's for JSON. You want a SHITSON parser instead.
5
u/TheBrainStone Jun 15 '20
I’d love to live in your rainbow world where you’re guaranteed to always get well formed JSON from everywhere.
2
0
3
u/frumious Jun 14 '20
Really fantastic, as always! The new binary support is particularly interesting and timely for my use!
1
1
-8
u/VinnieFalco wg21.org | corosio.org Jun 14 '20
How does this library compare in terms of API and performance to (Not-Yet-Part-Of) Boost.JSON?
12
Jun 14 '20
You got downvoted like crazy for repeatedly providing that information unprompted. The boat has kinda sailed to fish for it more subtly.
6
u/VinnieFalco wg21.org | corosio.org Jun 14 '20
Still plenty more boats scheduled to leave the dock :) In any event, informing users of their choices helps everyone especially when those choices are advantageous.
10
Jun 14 '20
Kudos for being honest about it, I guess. It would have been easy to do this through an alt.
-1
0
0
u/forcecharlie baulk maintainer Jun 15 '20
I would rather use json.hpp at the expense of some performance, thanks to the contributors of JSON for Modern C++. Wonderful work.
-24
u/VinnieFalco wg21.org | corosio.org Jun 14 '20
If you like nlohmann's, give Boost.JSON a try (Note: This is not yet an official Boost library, but a formal review is planned):
https://github.com/CPPAlliance/json
Boost.JSON significantly out-performs nlohmann's in all ways:
http://vinniefalco.github.io/doc/json/json/images/parse-twitter.png
http://vinniefalco.github.io/doc/json/json/benchmarks.html
Boost.JSON avoids using class templates for DOM types, resulting in fast compilation and an easy-to-link library. Or, you can use it in header-only mode. Use it with or without Boost. Boost.JSON has particularly robust support for stateful allocators, using a ref-counting smart pointer to a `std::pmr::memory_resource` (or `boost::container::memory_resource` before C++17).
More and more users are making the switch from RapidJSON and nlohmann's JSON to Boost.JSON, will you be next?
41
u/STL MSVC STL Dev Jun 15 '20
Moderator warning: As many other commenters have noted, this is not appropriate. Please don't do this again.
If there's a post about library A, and an unrelated commenter asks "how does this compare to library B", then it's reasonable for B's maintainer to provide their take on the differences in a single reply (worded as fairly as possible). It's also reasonable, if A's maintainer or other commenters make assertions about library B, for B's maintainer to reply with clarification (again, in a reasonable technical manner, without self-promoting puffery). It's not reasonable to add an unsolicited comment about library B, and it's completely over the line to spam the post with numerous comments.
Final note: In fact, this self-promotion is contrary to your own interest - it certainly lowers my opinion of "Boost."JSON and the C++ Alliance in general. Taking a dependency on code is a major decision, and I don't want to depend on people who behave unreasonably. Therefore, behave reasonably.
1
u/anticrisisg Jun 15 '20
I'm going to say it was a momentary indiscretion. Vinnie's code is really great.
12
u/jpan127 Jun 14 '20
I'm sure it's a good library, but advertising on another library's advertisement is inappropriate.
11
15
u/amubtdx Jun 14 '20
You should probably make a separate thread instead of a comment here as the main post is only about nlohmann/json
10
0
Jun 15 '20
[deleted]
2
u/nlohmann nlohmann/json Jun 15 '20
The library should compile without warning. I would be glad to hear if you experience warnings still.
0
Jun 14 '20
[deleted]
6
u/Plazmatic Jun 14 '20
I'm very confused... Are you asking for macros? What would macros do to improve things? Types often don't have 1:1 member variable to serialization.
5
u/dscottboggs Jun 14 '20
Are you asking for macros?
Yes. Might not be something C++ is capable of but Crystal's JSON::Serializable uses macros to define the
to_jsonandfrom_jsonmethods. For exampleclass K include JSON::Serializable property int : Int32 property string : String @[JSON::Field(key: "someField")] property some_field : Array(String) @[JSON::Field(ignore: true)] property ignored : String endI was imagining something like a
JSON_FIELD(int fieldName);kinda syntax but again not sure if that's possible in C++...7
u/nlohmann nlohmann/json Jun 14 '20
Maybe this is what you mean: https://github.com/nlohmann/json/issues/2175
2
u/bishop3000 Jun 14 '20
This would be nice but quite dangerous as well. It needs to support all corner cases like if settings are missing in json or it’s an optional or you want to store it with different name in json etc.
5
u/nlohmann nlohmann/json Jun 14 '20
Yes, I think the macro can only support the 80% case where you just want to have a JSON roundtripping without much effort. For anything else, we have the possibility to get crazy in your
from_json/to_jsonfunctions.1
u/jpan127 Jun 14 '20
I agree with this thread, the flexibility of explicit definitions is a must, but the simplicity of default definitions is clean.
3
u/ReversedGif Jun 14 '20
For the common use case of wanting to serialize/deserialize a POD struct to/from a JSON object, having a macro to automatically generate the helpers can reduce boilerplate by a rough factor of 10 (multiline class with separate from/to methods -> a macro call that just lists all the member variable names).
TL;DR: Your confusion confuses me.
1
u/nlohmann nlohmann/json Jun 14 '20
Maybe this is what you mean: https://github.com/nlohmann/json/issues/2175
6
u/EmperorArthur Jun 14 '20
Fair warning, using Macros means there's it can't be converted into a Module!
1
Jun 14 '20 edited Jun 14 '20
It took the approach that one can define a trait for their type and the library builds a json parser that directly parses into that types constructor. This gives the flexibility to provide a mapping of json member names(they don't always follow C++ naming) to the items int he classes ctor.
using type = json_member_list<json_string<"member_a">, json_array<"member_b", int>>defines a ctor that takes the JSON members "member_a" and "member_b" and parses them into the call arguments of the types constructor. This has the side effect of allowing constexpr, no allacting by the parser, and non-movable types.
Trailing comments are free, but I had to make comments, like #comments to end of line
a compile option as they come with a price in performance.
21
u/tradrich Jun 14 '20
Seems reasonable. Not sure I'd immediately use it over RapidJSON though. Can you convince me?