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

Automatically generate JavaScript from JSON Schema - Stack Overflow

programmeradmin1浏览0评论

After hourlong searching I have found no solution about my problem and now I hope someome could be able to help me here.

So basically what I am looking for is a way to generate a JavaScript file (*.js) from a JSON (Schema) file automatically using NodeJS. I know that there are things like fs.write, but this is surely no fitting way for my problem, I think. And so far I have found no other way to create my JavaScript file other than that.

Basically I want to translate:

{
"type":"object",
"properties": {
    "name": {
        "type": "string"
    },
    "age": {
        "type": "integer",
        "default":12
    },
    "favorite_color": {
        "type": "string"
    },
    "gender": {
        "type": "string",
        "enum": [
            "male",
            "female"
        ]
    }
}
}

Into JavaScript code like:

var data = function() { 

    data.baseConstructor.call(this);
    this.name = ko.observable("");
    this.age = ko.obseravble(12);
    this.favorite_color = ko.observable();
    this.gender = ko.observable(data.genderModes.male);

}

data.genderModes = {

   male: "male",
   female: "female" 
}

Would someone be able to give me a hint to my problem?

After hourlong searching I have found no solution about my problem and now I hope someome could be able to help me here.

So basically what I am looking for is a way to generate a JavaScript file (*.js) from a JSON (Schema) file automatically using NodeJS. I know that there are things like fs.write, but this is surely no fitting way for my problem, I think. And so far I have found no other way to create my JavaScript file other than that.

Basically I want to translate:

{
"type":"object",
"properties": {
    "name": {
        "type": "string"
    },
    "age": {
        "type": "integer",
        "default":12
    },
    "favorite_color": {
        "type": "string"
    },
    "gender": {
        "type": "string",
        "enum": [
            "male",
            "female"
        ]
    }
}
}

Into JavaScript code like:

var data = function() { 

    data.baseConstructor.call(this);
    this.name = ko.observable("");
    this.age = ko.obseravble(12);
    this.favorite_color = ko.observable();
    this.gender = ko.observable(data.genderModes.male);

}

data.genderModes = {

   male: "male",
   female: "female" 
}

Would someone be able to give me a hint to my problem?

Share Improve this question asked Feb 16, 2015 at 8:00 Stefan C.Stefan C. 1011 silver badge6 bronze badges 5
  • name and favorite_color have the same configuration but are generated differently – shyam Commented Feb 16, 2015 at 9:10
  • That does not help me with my problem/question – Stefan C. Commented Feb 16, 2015 at 9:21
  • that was an observation so that you can clarify the rules for the generation. btw, what did you do so far that did not fit your problem? – shyam Commented Feb 16, 2015 at 9:26
  • Well, right now I am about to dynamically generate strings that would reflect the content of my .js file and I think that this is the only way to really do it – Stefan C. Commented Feb 16, 2015 at 10:42
  • 2 Sometimes this is just a lot of typing. Find a good find and replace option in your editor helps a lot (as does linting your code and running tests to make sure you didn't miss any). What are you hoping to achieve with this? What will all the .js files do? There might be a different approach to try if we knew what you were trying to do (give some bigger context). – clay Commented Feb 16, 2015 at 17:50
Add a ment  | 

1 Answer 1

Reset to default 2

I don't know how to convert your json to js function but if you want to create object from json schema, you could use json-schema-defaults. After creating one object with json-schema-defaults you can create anothers with Object.create and you can add first level properties to new objects which is created by Object.create function.

var a = require('json-schema-defaults')({
"type":"object",
"properties": {
    "name": {
        "type": "string"
    },
    "age": {
        "type": "integer",
        "default":12
    },
  }
});

var b = Object.create(a,{
                         id:{ value:1 }, 
                          f:{ value:function() { 
                                     console.log('run lola run');
                                    }
                            }
                         }
                       );
发布评论

评论列表(0)

  1. 暂无评论