Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema to Javascript typed object

Is there any library around to generate Javascript typed object (JS functions) from a JSON schema? Basically the equivalent JS version of this http://code.google.com/p/jsonschema2pojo/. Thanks.

EDIT:

Starting from :

{
    "description": "An entity",
    "type":"object",
    "properties": {
        "geometries": {"type": "array",
            "items": {
                "$ref" : "geometry"
             }
          }
    }
}

I'd like some code like this to be generated for me

function Entity {
    this.geometries;
}

Obviously the schema could be more complex with $ref's etc I hope this gives the idea.

like image 273
Tarelli Avatar asked Oct 23 '12 10:10

Tarelli


1 Answers

Does this djvi library cover your requirements?

The provided example shows:

var jsonSchema = {"common":{"properties":{"type":{"enum":["common"]}},"required":["type"]}};

var env = new djvi();
env.addSchema('test', jsonSchema);
env.instance('test#/common');
// => { type: 'common' }

I suspect that is the solution you are after.

Now this is not the exact solution you are after, but I had a similar problem and created the following solution to return a parent object as a function, it may assist:

var dbdict = {
    "title": "Entity",
    "description": "An entity",
    "type":"object",
    "properties": {
        "geometries": {"type": "array",
            "items": {
                "$ref" : "geometry"
             }
          }
    }
}

var walkJSONSchema = function (JSONSchema, returnFunction) {

    var walkObject = function(PROPS) {
        var $this = this,
            $child = {}
        ;

        if(returnFunction == true) {
            $child = new function() {};
        }

        //console.log("PROPS");
        //console.log(PROPS);

        for(var key in PROPS) {
            console.log("key:"+key+" type:"+PROPS[key].type+" default:"+PROPS[key].default);
            switch(PROPS[key].type) {
                case "boolean":
                    $child[key] = PROPS[key].default || undefined;
                    break;
                case "integer":
                case "number":
                    $child[key] = PROPS[key].default || undefined;
                    break;
                case "array":
                    $child[key] = [].push($this.walkObject(PROPS[key].properties));
                    break;
                case "object":
                    $child[key] = $this.walkObject(PROPS[key].properties);
                    break;
                case "string":
                    $child[key] = PROPS[key].default || undefined;
                    break;
            };
        };

        return $child;
    }

    return walkObject(JSONSchema.properties);
}

Entity = walkJSONSchema(dbdict, true);

Of course you could script the retrieval of the "Entity" from the schema doc however you like, but this way at least you get a function.

like image 79
Anthropic Avatar answered Oct 18 '22 11:10

Anthropic