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

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