Google proposes to use Jsonnet to enhance JSON

Source: Internet
Author: User

Google opens up a configuration language jsonnet to replace JSON, which is completely backwards compatible and includes new features: annotations, references, arithmetic operations, conditional operators, arrays and object inclusions, Introduction, functions, local variables, inheritance, and so on. The Jsonnet program is translated into a JSON-compatible data format.

notes: Jsonnet accepts two styles of annotation--c style (/* .... */) and C + + style (//... )。

reference: the keyword self refers to the current object, and the operator $ refers to the root object.

arithmetic operations and conditional operators: operator + can calculate numbers and, stitch strings, arrays, and objects; operator = = and! = Used to discriminate true or false. The operator if and the ternary expression in the C language? Similar Here are some examples of jsonnet operators and translated JSON code (using the official language example):

bar_menu.3.jsonnet{    foo:3,         bar:2 * self.foo,  //multiplication.    Baz: "The value" + Self.bar + "is"         + (if Self.bar > 5 then "large" else "small") + ".",    array: [1, 2, 3] + [ 4],    obj: {a:1, b:2} + {b:3, c:4},    equality:1 = = "1",}
{   "foo": 3,   "bar": 6,   "baz": "The value 6 is large.",   "Array": [1, 2, 3, 4],   "obj": {a:1, B:3, C:4},   "equality": false}

build arrays and objects: constructor for can be used to build arrays and objects, here's an example:

{    foo: [1, 2, 3],    bar: [x * x for x in Self.foo if x >= 2],    baz: {[' field ' + X]: X for x in Self.foo},
   obj: {["foo" + "Bar"]: 3},}
{   "foo": [1, 2, 3],   "Bar": [4, 9],   "baz": {      "field1": 1,      "Field2": 2,      "field3": 3   },
   
     "obj": {"Foobar": 3}}
   

Modular: The code written by Jsonnet can be split into multiple files, accessed using import, and the introduced objects can be spliced using operator +.

function: The value of jsonnet can include functions, which are marked as hidden fields and are not translated into JSON. function is used for multiple evaluations, click here to see examples.

Jsonnet also supports features such as local variables, introduced object inheritance, operator + stitching, computable and optional fields.

The Jsonnet language engine is implemented using C++11, and is now available in C and Python libraries for portability to other languages using the C language API. The C + + implementation engine can be compiled into JavaScript code via Emscripten, and you can also use unofficial NPM packages in node. js.

To learn more, we recommend reading the language specification and comparing this article with other configuration languages.

Google proposes to use Jsonnet to enhance JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.