简体   繁体   English

定义参数Json或对象

[英]Define parameter Json or object

I've method that draw some controllos on the screen,now I want that this method will be an API and make it more daynamic in order to provide to the user ability to decide which control he want to draw on the screen. 我有一种在屏幕上绘制一些控件的方法,现在我希望该方法将是一个API并使它更加实用,以便向用户提供决定他要在屏幕上绘制哪个控件的能力。 I thougt to ask to provide some json structure but not sure if its better then object...,any suggestion 我想问一下提供一些json结构,但不确定它是否比对象好……,有什么建议

eg 例如

withSection1 true
withSection2  false
withSecttion3 false
withSection4 true
.....

but by default to add some parameter if the user want to draw all instead of provide all the data(section's).so I not sure how to build it. 但是默认情况下,如果用户要绘制全部而不是提供所有数据(部分的数据),则添加一些参数。因此,我不确定如何构建它。

update 更新

Assume I use a json for the section's How should I put the defualt in the method signature if user want by deafult to draw all the controlles 假设我使用json作为本节的“如果用户想通过聋子绘制所有控件,应如何将默认值放在方法签名中

{
    "withSection1": 'true'
    "withSection2": 'true'
    "withSection3": 'false'
    "withSection4": 'true'

}

so the method signature will be like drawScreen(jsonStr) 因此方法签名将类似于drawScreen(jsonStr)

The main difference between object (if you want to use simple objects whitout playing with the prototype) and JSON is you won't be able to use functions in JSON. 对象(如果您想使用简单的对象而不玩原型)和JSON之间的主要区别是您将无法使用JSON中的函数。

If you don't need functions (never), you should certainly go for JSON because it is more restrictive and it is almost always a good thing to prevent user from doing something you don't want. 如果不需要(永远不需要)函数,则应该使用JSON,因为它的限制性更强,并且防止用户执行您不希望做的事情几乎总是一件好事。 This also can help you to use it with another langage if you plan to do that. 如果您打算这样做,还可以帮助您将其与另一种语言一起使用。

However, I created a javascript/node.js framework (a link is in my profile if you want to see an example) where I extensively use javascript objects for configuration because it is really more modular (you can factorize some code in variables, merge some files together without doing a lot of work, you win a little performance on interpretation, ...)! 但是,我创建了一个javascript / node.js框架(如果要查看示例,则在我的配置文件中有一个链接),在该框架中,我广泛使用javascript对象进行配置,因为它确实更具模块化(可以将变量中的某些代码分解,合并一些文件在一起而不做大量工作,您在解释方面会获得一些表现,...)! Oh I forgot something important! 哦,我忘了重要的事! You cannot use comment in JSON. 您不能在JSON中使用注释。

Here some trivial examples (don't code like that :): 这里有一些琐碎的例子(不要那样编码:):

Object 宾语

// foo.js
var foo = {
    foo: 1
};

// bar.js
var bar = {
    bar: function() { return 2; }
};

// merge.js
var obj = merge(foo, bar); // Implement merge function once.

JSON JSON

foo.json (no comments) foo.json(无评论)

{
    "foo": 1
}

bar.json (no comments) bar.json(无评论)

{
    "bar": 'no functions'
}

// merge.js
// You have to get the JSON files, then parse them (JSON.parse in javascript), then merge them.

As I said you can see more important use of objects following the link in my profile. 正如我所说,您可以在个人资料中的链接后面看到更重要的对象使用情况。

UPDATE UPDATE

If you want to set default value and/or which field and which type of field you want in your JSON, you certainly should have another JSON or object describing your structure. 如果要在JSON中设置默认值和/或要设置哪个字段和哪种类型的字段,则当然应该有另一个JSON或对象来描述您的结构。 In the framework, the input of the user is processed from a contract file for example: 在框架中,从合同文件处理用户的输入,例如:

var jsonConfiguration = '{"withSection1": false}';

var contract = {
    withSection1: {
        type: 'boolean',
        default: true
    },
    withSection2: {
        type: 'boolean',
        default: true
    },
    withSection3: {
        type: 'boolean',
        default: false
    },
    withSection4: {
        type: 'boolean',
        default: true
    }
}

// Check types, add default values if needed, ...
var configuration = processConfiguration(jsonConfiguration, contract);

drawScreen(configuration);

function processConfiguration(jsonConfiguration, contract) {
    var configuration = JSON.parse(jsonConfiguration),
        processedConfiguration = {}
    ;

    for (var key in contract) {
        if (undefined === configuration[key]) {
            processedConfiguration[key] = contract[key].default;
        } else {
            processedConfiguration[key] = configuration[key];
        }
    }

    return processedConfiguration;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM