Skip to content

Instantly share code, notes, and snippets.

@nlohmann
Last active March 1, 2017 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlohmann/924dab4b3a4d02d02190c49339258d4e to your computer and use it in GitHub Desktop.
Save nlohmann/924dab4b3a4d02d02190c49339258d4e to your computer and use it in GitHub Desktop.
Mail for the std-proposals forum
Hi there,
I am wondering whether JSON [RFC7159] support would be a helpful extension to the C++ standard library (pure library extension), including, but not limited to, the following aspects:
1. A variant-like container type (for this mail, let's call it "std::json") that combines C++ types for the JSON value types [RFC7159, chapter 3]:
- string (e.g. std::string),
- number (e.g., double or int64_t),
- boolean (e.g., bool),
- array (e.g., std::vector), and
- object (e.g., std::map).
This type should have an intuitive API (i.e., all expected container methods), but also use as much syntactic sugar as possible (e.g., using initializer lists to express arrays like "std::json my_array = {"a string", 17, 42.12};".
2. A serialization function to create a textual representation (called "JSON text" in [RFC7159]) from a std::json value that conforms to the JSON grammar [RFC7159, chapter 2-7].
3. A deserialization function (i.e., a parser) [RFC7159, chapter 9] to create a std::json value from a JSON text.
There are currently dozens of libraries [json.org] written in C or C++ solving these aspects. However, it would be of great convenience to have JSON be part of the C++ standard library. In particular, the wide use of JSON as exchange format for structured data as well as to express simple configuration data would could solve a lot of use cases within the C++ standard library.
I would be willing to draft a proposal based on the experience I made with my C++ JSON library [nlohmann/json]. Of course, I would be interested in your thoughts on this.
All the best,
Niels
References
[RFC7159] https://tools.ietf.org/html/rfc7159.html
[json.org] http://json.org
[nlohmann/json] https://github.com/nlohmann/json
@mariokonrad
Copy link

Hi Niels.

In general, I think the wording is good. I have five points of feedback, and I apologize for the nitpicking.

  1. Formatting is strange (horizontal scrolling). Maybe it's just github?

  2. In point (1): "A variant-like data type". Since JSON is (potentially/normally) a hierarchical composition of data, the wording could also be like "A variant-like container type". This clears some ambiguity between the term "variant" (which is part of C++17) and the description in point (1) about "expected container methods". std::variant is not a container in the sense of the other containers, providing the expected functions (begin/end/size/etc...), but std::json is (a potentially heterogeneous container, but none the less).

  3. The enumeration of types in point (1) are easier to read as bullet points, also better overview.

  4. Maybe it could be helpful to mention, that it is purely a library extension, no core language changes, e.g. ".. JSON be part of the C++ standard library (pure library extension) ..." or similar. Or it is not necessary, not sure...

  5. I think it's good to not also include jsonpaths (despite requested several times in the forums/reddit). The proposal would be to huge.

I like the text, looking forward to see reactions.

@nlohmann
Copy link
Author

nlohmann commented Mar 1, 2017

Hey @mariokonrad,

thanks for the feedback!

  1. Formatting is indeed strange. I did not want to use Markdown, but ASCII-gists seem to have poor support.
  2. Good point. I changed the text.
  3. Done.
  4. Done.
  5. Do you mean JSONPath (http://jsonpath.com, http://goessner.net/articles/JsonPath/index.html#e2)?

Cheers,
Niels

@nlohmann
Copy link
Author

nlohmann commented Mar 1, 2017

@mariokonrad
Copy link

Great!

JSONPath (http://jsonpath.com), yes this was it. I remember people asking for this on reddit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment