Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use compile-time type guarantees for JSON structures? #2195

Closed
1 of 3 tasks
dvhwgumby opened this issue Jun 17, 2020 · 5 comments
Closed
1 of 3 tasks

Comments

@dvhwgumby
Copy link

What do you want to achieve?

I'm writing code that talks to a network service using JSON or msgpack (it's a WAMP service if you care).

Every message has two "arguments": a JSON array and a dictionary (object). My calls use two JSON types, but if the wrong type is supplied I can only tell at runtime. It would be great if there were a way to tell at compile time.

What have you tried?

When the arguments are all the same type of scalar (e.g. ints) I can use the implicit conversion. But that often isn't the case.

So: my questions are:

  1. Is it OK to use a std::vector<nlohmann::json>? What about the dictionary -- should I use std::map<std::string, nlohmann::json>?

  2. Are there internal types I should use, perhaps that are more stable than the types above that could change? The doxygen doc wasn't clear and searchng around in the source didn't uncover anything that seemed authoritative.

  3. Is this "against the spirit of JSON" and something you recommend I just not worry about?

Can you provide a small code example?

Which compiler and operating system are you using?

  • Compiler: g++ 10/Clang++ 10, std=C++20 (which is really c++17 plus a few C++20 features)
  • Operating system: Mac, Linux, iOS.

Which version of the library did you use?

  • latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch
@nlohmann
Copy link
Owner

No, this is not possible. A json value uses a union internally, so at compile time, all values are possible and only at runtime a json value gets a type.

json values are C++ values just like others, so putting them into a vector or map should not be a problem. As JSON offers arrays and objects by itself, I am not sure whether this is really necessary though.

I would need to understand your use case better to comment further.

@dvhwgumby
Copy link
Author

Thanks for your quick response; I think I'll just require a first argument of std::vector<nlohmann::json> and leave it at that. You can close this request.

As you asked, here's some FYI: I am writing a system that interacts with a number of clients using the ambiguously-named WAMP protocol for RPCs and for publish/subscribe. Every WAMP message has the same gross structure, consisting of a JSON array of arguments and a JSON object that is a keyword dictionary. RPC responses are the same pair of structures.

As a C++ developer I have a general desire to unearth bugs at compile time whenever possible, but that is not always possible or even reasonable, and this is simply one of those cases.

@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented Jun 19, 2020

You can probably get what you want by having a C++ struct that serializes consistently through a to_json() overload.

@nlohmann
Copy link
Owner

Do you need further assistance with this issue?

@dvhwgumby
Copy link
Author

No thank you, I will close it.

I think my desire was un-JSONy (it's on the interface where JSON's and C++'s Weltanschauungen differ). And Francois Chabot's suggestion was a good one.

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

No branches or pull requests

3 participants