I've implemented a really simple way to parse JSON into typed objects. You 
create types that relate to the structure of the JSON, and it works 
everything out. Very impressed with how Julia's type system hit a great 
balance of power and simplicity.

Feedback pls on usefulness - see this 
gist<https://gist.github.com/filmackay/8970338>
:

# define our types
type DefB
    datatype::DataType
    unique::Bool
    DefB() = new(Any, false) # default values
end

type DefA
    childAs::Dict{String, DefA}
    childBs::Dict{String, DefB}
    
    DefA() = new(Dict{String, DefA}(), Dict{String, DefB}())
end

# a sample JSON document
json = """
{
    "childAs":
    {
        "z": {},
        "x":
        {
            "childBs":
            {
                "b1": { "datatype": "Int", "unique": "false" },
                "b2": { "datatype": "String", "unique": "false" }
            }
        }
    },
    "childBs":
    {
        "b3": { "datatype": "Int32", "unique": "false" },
        "b4" :{ "datatype": "String", "unique": "false" }
    }
}"""

# go
tparse(DefA, JSON.parse(json))

# 
DefA(["x"=>DefA(Dict{String,DefA}(),["b2"=>DefB(String,false),"b1"=>DefB(Int64,false)]),"z"=>DefA(Dict{String,DefA}(),Dict{String,DefB}())],["b3"=>DefB(Int32,false),"b4"=>DefB(String,false)])

Reply via email to