-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
JSON Creation #1461
Comments
Relying on the brace-initialization is a little bit funny in this library and might vary depending on your compiler (see #1359). I'd be explicit about use of object vs array, and also break it up into smaller pieces (functions/lambdas depending on your style). Doing it like this also help avoid typo bugs (which still compile fine) by not repeating the keys several times. json j = json::object();
j["headsetConfig"] = [] {
json headsetConfig = json::object(); // note I am explicitly specifying I want this to be an object
headsetConfig["templateConfiguration"] = [] {
json templateConfiguration = json::object();
templateConfiguration["configTemplateVersion"] = "1";
// ...
templateConfiguration["someArray"] = [] {
json someArray = json::array(); // note I am explicitly specifying I want this to be an array
for (auto const& thing : collection) {
someArray.push_back(JsonObjectForThing(thing));
}
}();
return templateConfiguration;
}();
return headsetConfig;
}(); For even cleaner code (those lambdas are pretty ugly!) you can provide your own overloads to |
@fkicenko Do you need further assistance? |
Hey... the lambda's simply don't work here... Is there a more elegant way to do this with this library? The JSON structure is really simple enough. Thanks for the help! |
IMO overloading Here's an example with nesting: #include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
struct Foo {
std::string value;
};
struct Bar {
int value;
};
struct Nested {
Foo foo;
Bar bar;
};
using json = nlohmann::json;
void to_json(json& j, Foo const& f) {
j["foo_value"] = f.value;
}
void to_json(json& j, Bar const& b) {
j["bar_value"] = b.value;
}
void to_json(json& j, Nested const& n) {
j["foo"] = n.foo;
j["bar"] = n.bar;
}
int main(int argc, const char** argv) {
Nested serialiseMe{{"myname"}, {42}};
json serialised(serialiseMe);
std::cout << std::setw(2) << serialised << std::endl;
return 0;
} |
Ok this is good... If you can give me an example of nesting an Array within an Object I think i'm good to go. This example gives me 2 objects but just fuzzy on now nesting an Array in there. Thanks!!! |
Can you provide an example of the input, the current output, and the expected output? |
Hello,
I'm trying to create a JSON structure that contains Arrays and Objects that are hierarchical. Could spend a considerable amount of time but thought I would post my questions first. I'm having issues creating Objects (key:value) pairs eg: "modelSeries":"520", within an Array. Also creating Arrays within Arrays. I'm attaching a JSON file that i'm working on and below is the code i've created so far. Hopefully someone can shed some light on the easiest way to do this.
Code:
headsetJSON.zip
The text was updated successfully, but these errors were encountered: