最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JSON schema to Javascript typed object - Stack Overflow

programmeradmin4浏览0评论

Is there any library around to generate Javascript typed object (JS functions) from a JSON schema? Basically the equivalent JS version of this /. 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 plex with $ref's etc I hope this gives the idea.

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./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 plex with $ref's etc I hope this gives the idea.

Share Improve this question edited Oct 23, 2012 at 10:47 Álvaro González 147k45 gold badges279 silver badges377 bronze badges asked Oct 23, 2012 at 10:27 TarelliTarelli 6347 silver badges18 bronze badges 2
  • JSON is valid Javascript, you know. – Waleed Khan Commented Oct 23, 2012 at 10:32
  • 1 Wele to Stack Overflow. You can format source code with the {} toolbar button. I've done it for you this time. – Álvaro González Commented Oct 23, 2012 at 10:48
Add a ment  | 

3 Answers 3

Reset to default 2

Does this djvi library cover your requirements?

The provided example shows:

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

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

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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论