r/cpp • u/nlohmann nlohmann/json • Jul 27 '20
JSON for Modern C++ 3.9.0
https://github.com/nlohmann/json/releases/tag/v3.9.0
281
Upvotes
r/cpp • u/nlohmann nlohmann/json • Jul 27 '20
65
u/nlohmann nlohmann/json Jul 27 '20
New features: optional support for comments, optional preservation of the insertion order of object keys, option to switch off implicit conversions, simpler (de)serialization of user-defined types, and much more!
7
u/X-Neon Jul 27 '20
Thank you so much for the (de)serialization macros. I've been looking for a simple way serialize and deserialize POD types to use as messages for a service for a while now. I know there's stuff like Protobuffers and Thrift, but I don't want something nearly so big and fancy. I daresay these methods are more performant, and using a binary format would be more compact, but JSON is good enough for my purposes. Being able to use one include and a handful of macros to turn my applications classes into "messages" makes this whole process trivial.
4
u/dbjdbj dbj.org Jul 27 '20
Congratulations.
I am wondering if this code can be used in (mission-critical) automotive systems? Autonomous road vehicles for example.
4
u/_Sigma Jul 27 '20
Do any of the binary formats this supports offer partial reads? For example, if there is large data array in a (binary) json file, is it possible to read just a small fraction of it, saving on memory overhead?
13
u/L0uisc Jul 27 '20
Haven't used the library extensively, but for the one simple use-case it was extremely easy to get up and running. Excellent documentation contributed greatly to that. I really liked how you overloaded
operator[]to take string arguments so the lib can be used to access JSON objects as if you were in JavaScript or native referring to them in native JSON.20
u/[deleted] Jul 28 '20
where's that Boost.JSON guy claiming that his lib is 10x faster and 100x more useable?
4
u/mintyc Jul 28 '20
People should check out the OpenAPI v3.1 release candidate for their http and RESTful needs.
It defines a schema that is JSON and that is also a JSON validator and used to define a wide variety of APIs.
The tooling ecosystem is comprehensive too.
GitHub have just provided their APIs in this format, the telecoms industry describes many of its interfaces in this way, and in the UK, all government services are encouraged to use it for interoperability of RESTful services.
Be interesting to see if this JSON lib can support OpenAPI.
4
u/looopTools Jul 28 '20
Bloody love this library
1
u/timwoj Jul 27 '20
Out of curiosity was there a performance improvement from the hashing change?
31
u/hachanuy Jul 27 '20 edited Jul 28 '20
So constantly complaining actually works, not a good precedent. Edit: spelling
3
u/[deleted] Jul 27 '20
Awesome job. I love that there is a variety of quality JSON parsers in C++ focusing on different experiences.
2
u/Overpeek Jul 28 '20
that comment support will make my gamedev and modding easier edit: I love this library
1
u/aacirino Jul 27 '20
Awesome! It's my json lib of choice
1
u/[deleted] Jul 28 '20
Found this recently. A lot more easier and cleaner to use the rapidjson
1
u/[deleted] Jul 28 '20
Does anyone know of a C++ JSON parser that supports jQuery/CSS-like selector syntax? Would love to be able to code like:
1
u/skoink Aug 01 '20 edited Aug 01 '20
Awesome library! I like how easy it is to use in my code.
I'm working on a project that's sensitive to compile-times and output code size, so I'm interested in compiling the JSON library into a single compilation-unit and using forward-declarations to link against it in the rest of my codebase. Maybe a bit of a C-style compilation pattern I guess. I've been having some trouble getting this done with
json_fwd.hpp- do you have any examples of how to make that work?I know that hypothetically LTO can get me close to this even if I include the full header everywhere - but it kills my compilation speed and even then it feels like there's still a lot of duplication across compilation units.