jsonhxdef: quickly generate haxe typedefs from valid json

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

jsonhxdef: quickly generate haxe typedefs from valid json

Justin Donaldson-3
I have a small script that I put together that creates typedefs from arbitrary json strings:
https://gist.github.com/1454243

The script can turn this:
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

into these:

typedef JsonHxDef = {
    glossary:Glossary
}

typedef Glossary = {
    title:String
    GlossDiv:GlossDiv
}

typedef GlossDiv = {
    title:String
    GlossList:GlossList
}

typedef GlossList = {
    GlossDiv:GlossDiv
}

typedef GlossDivX = {
    GlossTerm:String
    GlossSee:String
    Abbrev:String
    ID:String
    Acronym:String
    SortAs:String
    GlossDef:GlossDef
}

typedef GlossDef = {
    para:String
    GlossSeeAlso:Array<String>
}



This makes it really easy to create typedefs for data transferred as json.

Of course, not all json works well with this approach.  The script doesn't try to fix valid json that becomes invalid haxe object fields, such as fields named "1foo", or "$#@$".  The compiler will throw an error for these. 

The script handles all the basic types: Strings, Floats, Ints, and Bools. It even tries to create typedefs recursively for child objects.
The script doesn't always make the right decisions for these typedef names, since it has to infer the type from the field name that references it.  In some cases, the same name is found more than once.  In that case, the script will add "X" to the duplicate name until it finds a unique name.  The script will also not be able to detect duplicate instances of the same json object structure (e.g., to give all these consistent structures the same type name).   However, it won't create duplicates with arrays of objects.  Instead, it only creates one definition based on the first element in the array.  Also, the script will always call its top level object "JsonHxDef". 

The script is pretty raw, but handles most of my needs at the moment.  If you find it useful, find some bugs, or want to suggest some improvements, let me know.
You invoke the script by calling:
./jsonhxdef test.json
the typedefs are written to stdout

Best,
-Justin



--
haXe - an open source web programming language
http://haxe.org