-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Comments
No, this is not possible. A
I would need to understand your use case better to comment further. |
Thanks for your quick response; I think I'll just require a first argument of 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. |
You can probably get what you want by having a C++ struct that serializes consistently through a to_json() overload. |
Do you need further assistance with this issue? |
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. |
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:
Is it OK to use a
std::vector<nlohmann::json>
? What about the dictionary -- should I usestd::map<std::string, nlohmann::json>?
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.
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?
Which version of the library did you use?
develop
branchThe text was updated successfully, but these errors were encountered: