I’m currently trying to remove as many dependancies as possible from my app. Currently thinking it would be nice to use JUCE’s json parsing rather than a 3rd party library like nlohmann. However, juce::var lacks some functions that would make handling the parsed json a lot less verbose.
juce::var has getProperty and hasProperty, but no getProperties.
Iterating over properties is a pain. You need to get the dynamic object from the var, from the dynamic object you need to get the NamedValueSet, and then you can iterate that. Adding begin/end to var, would make things a lot easier, especially if they could do both arrays and objects.
Finally, JSONUtils supports using json pointers for setting values, but not getting values.
I have created potential implementations here: Figbug/variantimprovements by FigBug · Pull Request #1474 · juce-framework/JUCE · GitHub
Example:
juce::String json =
R"(
{
"id" : "root",
"children" :
[
{ "id": "child1", "name": "bob", "size": 2 },
{ "id": "child2", "name": "sam", "size": 4 },
{ "id": "child3", "name": "joe", "size": 8 },
{ "id": "child4", "name": "rex", "size": 9 }
]
}
)";
auto obj = juce::JSON::parse (json);
DBG(obj.getProperties().joinIntoString (", "));
DBG("====");
DBG(juce::JSONUtils::getPointer (obj, "/children/2/name", {}).toString());
DBG("====");
for (auto itr : juce::JSONUtils::getPointer (obj, "/children/2", {}))
DBG(itr.name.toString() + ": " + itr.value.toString());
DBG("====");
for (auto itr : juce::JSONUtils::getPointer (obj, "/children", {}))
DBG(itr.name.toString() + ": " + juce::JSON::toString (itr.value));
DBG("====");
for (auto itr : juce::var (3))
DBG(itr.name.toString() + ": " + itr.value.toString());
Output:
id, children
====
joe
====
id: child3
name: joe
size: 8
====
0: {
"id": "child1",
"name": "bob",
"size": 2
}
1: {
"id": "child2",
"name": "sam",
"size": 4
}
2: {
"id": "child3",
"name": "joe",
"size": 8
}
3: {
"id": "child4",
"name": "rex",
"size": 9
}
====
