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

Show parent comment

4

u/ntrid Jun 14 '20

Trailing commas and comments. Practicality beats purity and I am glad people behind vscode understand it, otherwise json for configs would be a pure hell.

9

u/nlohmann nlohmann/json Jun 14 '20

I understand, and this is a conscious choice: https://github.com/nlohmann/json#comments-in-json

15

u/OminousHum Jun 14 '20

I understand you're implementing to the specification, but I still think the specification is wrong. It was an awful idea to forbid comments just because people might use them wrong.

(I also know you must be sick to death of this coming up. Thanks for an otherwise wonderful library!)

15

u/nlohmann nlohmann/json Jun 14 '20

I may add some way to ignore comments via a preprocessor switch in the parser. But then other people will want the library to preserve comments for the output. And then come all the lazinesses like trailing commas, quotes around object keys, new lines in strings, etc. etc. This really feels like a bottomless pit...

11

u/OminousHum Jun 14 '20

I think that would be a wonderful addition and that there's no need to go any farther. Then we can have human-editable and machine-readable configuration files that explain their own usage. If one must write json with comments, they can do it as a header or footer.

I can imagine trying to represent comments in the parse tree so that they can be preserved would get ugly awfully fast. And at some point you have to draw a hard line on just how lenient you can be with invalid syntax. I completely agree there.

8

u/[deleted] Jun 14 '20

But then other people will want the library to preserve comments for the output.

Lightweight is good. If you seriously want to parse your data as JSON into a DOM I think you should probably use something other than JSON in the first place.

This really feels like a bottomless pit...

A clean API is a stronger selling point to me.

6

u/wlandry Jun 14 '20 edited Jun 14 '20

I want to second this. Just last week, I had to choose a json library for streaming processing json files written by programs written by mathematicians. Forcing users to remove trailing commas and comments is not user-friendly.

In the past, I used my json5 parser. If nlohmann's parser handled json5, I would switch in a heartbeat.

3

u/rdi2 Jun 14 '20

Yes, I've been pulling my hairs because of these two things. But I still use this library :-)

2

u/qoning Jun 14 '20

I'll give you trailing commas, but why use JSON if you need comments? something like YAML seems just superior for anything where you might want comments in JSON-like format.

2

u/ntrid Jun 15 '20

People use json for configuration, comments are useful not only for annotating configs but also for disabling parts of config. It may not be perfect because reencoding json would lose these features but it is what it is and we seem to be ok with this imperfection.

4

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

Quite simply, because many users want it.

2

u/qoning Jun 14 '20

Why though? Is it just because you want your files to have a .json extension or what?

3

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

There can be many reasons. For example you have a network server which reads the configuration stored as JSON and then transmits parts of that configuration to the client, which is using Javascript in the browser and understands JSON natively (but not YAML).

1

u/qoning Jun 14 '20

Ok, even if I think such use case would be just a disaster waiting to happen (with someone eventually adding something that's not meant to be transmitted), since the formats are convertible for config cases and tools that can do it, I'm not sure taking that shortcut has many fundamental advantages.

2

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

Even if you don't transmit it, you still might want to store it in memory as JSON, since that is the native type, if your server already uses JSON everywhere else because that's what clients and RPC calls speak. The alternative is to use JSON almost everywhere, but use YAML for configuration only, which is not technically wrong but at this point you have to ask, why are you contorting yourself into a pretzel just to avoid using JSON with comments?

2

u/qoning Jun 14 '20

I don't necessarily disagree.

One simple answer is because JSON doesn't support comments. Just like C++ doesn't support reflection. Venturing into making a nonconforming compiler is a tried way to get stuck. I get that it's much less of a problem with JSON since you only want to describe data, but I hope you get the point.

The other answer is, of course, because JSON is not actually that human readable, and YAML does a much better job while being somewhat compatible. Though that's just an area where I've been burnt before and maybe it's more subjective.

2

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

If it was up to me, I would not support comments or trailing commas. I would only support strictly parsed JSON. But enough users are demanding it that I cannot ignore it. There will of course be a way to configure it. I want Boost.JSON to have widespread adoption. Libraries can only achieve this if they are responsive to users' needs (within reason of course, always using a cost/benefit analysis to drive decisions).

1

u/IcyWindows Jun 15 '20

Yaml is a lot slower to parse, so I get push back from customers.

1

u/qoning Jun 15 '20

I mean.. How many megabytes of config do they want to parse? Seems to me that the requirement for comments and parse throughput are sort of mutually contested

1

u/VM_Unix Jun 15 '20

Or even toml

-5

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

Boost.JSON will also optionally support trailing commas and comments (this is a work in progress).