Custom properties and json serialization

What would you like to see in future versions of the editor?
Post Reply
njguy281
Posts: 4
Joined: Sun Oct 12, 2014 12:56 am

Custom properties and json serialization

Post by njguy281 »

If you use a json serialization library like GSON for java, the way in which RUBE saves custom properties doesn't work. This is because rube forces you to choose a custom property type, ie. int, vec2, bool, etc. These words then show up in the json exported scene file. If you are using a serialization library, the word int for example is a Java keyword. Therefore you can't properly serialize any custom rube property with the type 'int'. You have to use the string property and cast it into an int because luckily Rube has it as a lower case s in string opposed to String in Java.

Basically the rube custom proerties are incompatible with GSON which is a super fast and awesome json serialization tool. If you try it you'll get what I mean.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Custom properties and json serialization

Post by iforce2d »

hmm... gson seems to be only well suited for saving and loading data by the same program, and loses the point of having a flexible text format that can be parsed and interchanged among many applications, with no reserved words...

Anyway, I think it's worth adding an option to the export settings, to export in a format that plays by the gson rules, so I'll do that in the next release. If I understand it correctly, you need something like this right?

Code: Select all

// current export format
{
    "int" : 34,
    "name" : "myCustomInt"
}

// make gson happy format
{
    "myCustomInt" : 34
}
For now though, perhaps you could use an intermediate step to convert the exported JSON. I wrote a PHP script for you to do this, as below. PHP is available for Windows, Mac and Linux and is pretty easy to install. You would save that script in a file, then invoke it on the command line, eg. "php myscript.php fileToConvert.json". The output will be dumped to the terminal so you can redirect it into another file. This step could then be set up as an after-export hook so that it is done automatically every time you export. The resulting output will be in minimized form (no newlines or whitespace formatting), and you may find that floating point values become a tiny bit different, but probably nothing you would notice. That's the best I can do for now.

Code: Select all

<?php

if ( count($argv) < 2 ) {
    echo "Please specify the file to convert\n";
    exit;
}

function processCustomProperty(&$prop) {
    foreach ($prop as $key => $val) {
        if ($key === "name")
            $name = $val;
        else
            $value = $val;
    }
    $prop = array();
    $prop[$name] = $value;
}

function processNode(&$node) {
    if (!is_array($node))
        return;        
    foreach ($node as $key => &$value) {
        if ($key === "customProperties") {
            foreach ($value as &$prop)
                processCustomProperty($prop);
        }
        processNode($value);
    }    
}

$json = json_decode(file_get_contents($argv[1]), true);

processNode($json);

echo json_encode($json);

?>
njguy281
Posts: 4
Joined: Sun Oct 12, 2014 12:56 am

Re: Custom properties and json serialization

Post by njguy281 »

Thanks for giving it a look over. But yes your example is exactly what I meant. Rube is a fantastic tool by the way which is why I bought it and love it.
Post Reply