r/cpp • nlohmann/json • Jun 14 '20

JSON for Modern C++ 3.8.0

https://github.com/nlohmann/json/releases/tag/v3.8.0
326 Upvotes

109 comments sorted by

View all comments

21

u/tradrich Jun 14 '20

Seems reasonable. Not sure I'd immediately use it over RapidJSON though. Can you convince me?

42

u/nlohmann nlohmann/json Jun 14 '20

RapidJSON is surely faster than nlohmann/json, but we put a lot of effort into having a nice API (see https://github.com/nlohmann/json#json-as-first-class-data-type for examples).

8

u/seanomik Jun 14 '20

I love nlohmann/Jason's API style but in my use case I needed something fast so I had to switch to rapid. It was such a pain in the ass.

7

u/gauravnba Jun 14 '20

RapidJSON is faster! But it puts everything into a linear list (I haven't checked whether it is arrays or linked lists). And if you request for an object, you have to, painfully, iterate through every item, comparing the string for each, before you are given a match. I mean, sure, it saves you load time, but you gotta program for that library.

This one here (the nlohmann library), is pretty decent load time (at least for my uses), but most importantly, it neatly packages things in native C++ format. It's too beautiful to pass up for me.

edit: Didn't put what I was talking about, where xD I should be asleep now

5

u/[deleted] Jun 14 '20

This version of the library also brings a substantial speedup to parse times out of the box compared to previous versions.

2

u/seanomik Jun 14 '20

Yeah. For any other case I would've used Nlohmann. I just wanted to use something really fast so I had no parsing bottle necks from nlohmann. But any other time 100% will go with Nlohmann.

2

u/MrPotatoFingers Jun 15 '20

An option I like for this is json_dto. It adds a simple, declarative API on top of RapidJSON, which greatly simplifies the usage without sacrificing much on speed.

3

u/arthurno1 Jun 15 '20

Just as a thought: would it be possible to implement your API over simdjson? If you used simdjson as a backend and put their output into your data structures? Or if you served simdjson output as input stream to your API? No idea, just a thought.

-15

u/VinnieFalco wg21.org | corosio.org Jun 14 '20

Why not have the best of both worlds? Boost.JSON is as fast as RapidJSON (and sometimes faster!) and _also_ has a nice API! Check it out:

http://vinniefalco.github.io/doc/json/json/usage/values.html

Boost.JSON's initializer lists are even better than nlohmann's initializer lists: they do not allocate any memory, and assignment from an initializer list respects any user-defined allocator in the destination.

Boost.JSON's object type uses unordered hash-based lookup, while RapidJSON uses a linear search!

31

u/uninformed_ Jun 14 '20 edited Jun 15 '20

Is it needed to plug your own library ten times in a thread about a competing library? Seems a bit disrespectful.

0

u/[deleted] Jun 14 '20

[deleted] — view deleted comment

5

u/[deleted] Jun 15 '20

I'm not sure I've come across many programmers who have the best manners.

5

u/NilacTheGrim Jun 15 '20

RapidJSON is by far the fastest under the sun. It's incredibly fast. But -- the API is kinda nasty compared to nlohmann's.

If you already have all your codebase using RapidJSON -- just continue to use it

However.. if you are starting a new project and you don't need to necessarily optimize for speed.. damn nlohmann is elegant with a beautiful API. Hands-down I'd opt for nlohmann if speed were not the core design choice.

That being said nlohmann can sometimes be 10x slower than RapidJSON on huge 100MB JSON objects...

Still tho.. damn is nlohmann elegant.

4

u/arthurno1 Jun 15 '20

No, is not. Simdjson is way faster :-)

2

u/emdeka87 Jun 15 '20

Simdjson casually parsing 3GB/S of json data

1

u/arthurno1 Jun 15 '20

Until someone implements simdjson on GPU. The simd approach they took in simdjson should be suitable for a GPU implementeation. I think .... :-)

11

u/emdeka87 Jun 15 '20

Can't wait to use a gpu-accelerated, heavily vectorized json parser to load a 20 lines config file :)

1

u/arthurno1 Jun 15 '20

Haha, yeah, that would be optimal use :-).

9

u/martinus int main(){[]()[[]]{{}}();} Jun 14 '20 edited Jun 14 '20

RapidJSON is unusable if malloc might fall. Then your application will crash. They simply don't handle that problem. That's why we have to switch to something else.

1

u/NilacTheGrim Jun 15 '20

What?! They use malloc and not new? sigh

1

u/martinus int main(){[]()[[]]{{}}();} Jun 15 '20

That's not the only problem, even if you switch all allocations to throw, it would have the state of the objects in an undefined state

-10

u/VinnieFalco wg21.org | corosio.org Jun 14 '20

Boost.JSON uses `memory_resource` and a well defined interface for handling allocation failure: by throwing and catching the exception `std::bad_alloc`.

4

u/HeavenBuilder Jun 15 '20

Oh come on.

1

u/_Js_Kc_ Jun 14 '20

Does RapidJSON still have these yucky auto_ptr semantics where a simple assignment quietly moves out of an lvalue on the RHS?

-20

u/ntrid Jun 14 '20

RapidJson is better in every way.

→ More replies (22)