11

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.

2
  • JSON is valid Javascript, you know. Commented Oct 23, 2012 at 10:32
  • 1
    Welcome to Stack Overflow. You can format source code with the {} toolbar button. I've done it for you this time. Commented Oct 23, 2012 at 10:48

3 Answers 3

2

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.

0

I'd say you're up on your own on that one. Anyways, doing it shouldn't be too difficult. Just parse the JSON you have and then iterate over each item, apply the logic you want for each "class" and append the result to a string. Once done, print that string and use any JS formatter to get your code.

2
  • 1
    Thanks, was just interested if there was anything already out there. Not even sure it's a good idea to work with those objects instead of JSON objects themselves.
    – Tarelli
    Commented Oct 23, 2012 at 11:18
  • That depends on what are you trying to do exactly. I remember seeing something similar for C#, but not sure where, so maybe you should give Google a try. Commented Oct 23, 2012 at 11:20
-2

The only thing you could do is to add a _type_ property to your json objects (name it in some weird way so that it doesn't clash with other literals) that identify your type. You could then map that string to another object in javascript that lists the available properties.

You could do this - it doesn't mean it's a good idea. Json is made to be directly used in javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.