Archive 17/01/2023.

JSON loading example

slapin

Is there any JSON loading example in Urho? I know the support is there, but I wonder how to use it.

Mike

You can also have a look at the Editor’s “Localization” feature.

slapin

@Mike please help me fith this:

I have the following JSON file, how can I parse it with Urho?
how can I get keys and values?

{
        "composites": {
                "dispname": "Composites",
                "members": {
                        "sequence": {
                                "dispname": "Sequence",
                                "max_children": -1,
                                "properties": {
                                }
                        },       
                        "memsequence": {
                                "dispname": "MemSequence",
                                "max_children": -1,
                                "properties": {
                                }
                        },
                        "selection": {
                                "dispname": "Selection",
                                "max_children": -1,
                                "properties": { 
                                }
                        },      
                        "memselection": {
                                "dispname": "MemSelection",
                                "max_children": -1,
                                "properties": { 
                                }       
                        }       
                }       
        }
}
slapin

Basically I don’t understand how to get these “selection”, “memselection”, “sequence”,… etc. values.

slapin

well, if I do
JSONFile@ jsonfile = JSONFile();
jsonfile.Load(path)
JSONValue root = jsongile.GetRoot();

I see some data and root.size contains sane value,
but I really can’t go any farther. is JSON API really implemented or it was done
for some very specific needs?

Thanks!

and yes, I know I can do XML - it works fine, but this time I need JSON as XML is insane for stuff one have to write manually…

slapin

I see the problem. AngelScript have no JSON binding, so it is not possible to use JSON from AngelScript.

Will file a bug about that.

Mike

From what I remember:

  • First get the root:

JSONValue composites = jsonFile.root.Get(“composites”);

  • Each time you have a key that opens a curly brace, you get it as a JSONValue:

JSONValue members = composites.Get(“members”);
JSONValue sequence= members.Get(“sequence”);

  • Then you get each key value nested in the JSONValue:

String dispname = sequence.Get(“dispname”).GetString(); // “dispname” is the key, “Composites” is the value (a string here)

Mike

The example that I provided is in AngelScript…

slapin

But there is no way to know key values.
GetVariantMap() and other methods are not bound. C++ can freely pass files like this
unlike AngelScript.

slapin

Well, using Get is useless to me as I need to add more key values and build list out of them.
Anyway I need to build the list of values and that is not possible.

slapin

Filed a bug about this if anybody interested:

I resume that only XML is supported for value sets now. Which is sad news for me, as it is really booooring
to write XMLs :frowning:

slapin

@Mike thanks for help, but this looks beyond what can be done.

Victor

I could be misunderstanding, but from the issue it looks like you want to know how to iterate through a json file?

I would think that it would work like XML, using JSONValue::IsNull to check if you’ve reached the end.

Example using xml

XMLFile* texturesXML = cache->GetResource<XMLFile>(texturesFilePath_);
XMLElement child = texturesXML->GetRoot().GetChild();
while (!child.IsNull()) {
     elem = root.CreateChild("layer");
     elem.SetAttribute("name", child.GetAttribute("name"));
     child = child.GetNext();
}

Since you’re already doing this with XML, it should be easy to convert. I’ve not used AngelScript for anything in my project, however, creating the necessary bindings shouldn’t be too bad if you’re missing the IsNull method.

slapin

No, that won’t work.

Victor

Creating the bindings, or looping through the objects? Sorry, I’m a bit unfamiliar with AS so I’m not sure :frowning:

Victor

So, just to be sure, I’ve saved a scene as a JSON file in the editor, and loaded it up. So the answer must be somewhere in the Editor’s AS code. At least, I’d suspect you’d find your answer there.

slapin

Well, saving scenes is in C++ code and it is quite specific to that purpose.

Victor

Ah, I see. I didn’t realize that was done with C++ :worried: