Google has open sourced Jsonnet, a configuration language that supersedes JSON and adds new features without breaking backwards compatibility: comments, references, arithmetic and conditional operators, array and object comprehension, imports, functions, local variables, inheritance and others. Jsonnet programs are translated to compliant JSON data formats.

Comments. Jsonnet accepts both C (/* …. */) and C++ (//… ) comment styles.

References. The keyword self can be used to reference the current object, while the operator$ references the root object.

Arithmetic and Conditional Operators. The operator adds numbers, strings, arrays, and objects. The == or != operators are evaluated to true or falseif works like the ternary operator ?: in C. Following are some Jsonnet operations and the resulting JSON code, taken from the language’s examples:

// 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
}

Building Arrays and Objects. The for construct can be used to build arrays and objects as shown in the next sample:

{
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 }
}

Modularity. With Jsonnet the code can be split up into multiple files that are then accessed using import. Objects imported are then concatenated with other objects using +.

Functions. Jsonnet values can contain functions which are marked as hidden fields which are not translated into JSON. Functions are used for various evaluations. An example can be seenhere.

Jsonnet also features local variables, a way of inheriting objects by importing them and using the concatenation operator +, computed and optional fields.

The Jsonnet language engine is implemented in C++11 and wrapped around with a C API for easier porting to other languages. C and Python libraries are provided. The C++ implementation can be compiled to JavaScript with Emscripten and an unofficial nodejs package is available.

Google Proposes to Enhance JSON with Jsonnet的更多相关文章

  1. 使用google的GSON解析json格式的数据

    GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...

  2. Google提议使用Jsonnet来增强JSON

    Google开源了一门配置语言Jsonnet来取代JSON,它完全向后兼容并加入了一些新特性:注释.引用.算术运算.条件操作符,数组和对象内含,引入,函数,局部变量,继承等.Jsonnet程序被翻译为 ...

  3. Net.Json 常用例子

    #JsonConvert 例子 内容主要都是官方的例子,加上一些中文注释而已. 主要方便自己查询,分享一份出来. 参考文档: https://www.newtonsoft.com/json/help/ ...

  4. 【JavaScript】详解JSON

    目录结构: // contents structure [-] 什么是JSON JSON和XML的比较 相同点 不同点 JSON语法 如何解析JSON文本 eval()方法 JSON.parse()方 ...

  5. 使用Gson解析json

    前边的博客说过将json解析成java的方法,使用的是 这几个jar包,但是在解析时层遇到一个问题,就是在将时间字符串转换为java的Timestamp对象时会抛出异常,这个问题一直放在哪里没有去解决 ...

  6. JSON风格指南-真经

    简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语 ...

  7. Newtonsoft.Json的使用

    JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.和 XML 一样,JSON 也是基于纯文本的数据格式 ...

  8. Android开源库--Gson谷歌官方json解析库

    官方文档地址:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html 官方网站:http://code.go ...

  9. C#操作JSON学习

    JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...

随机推荐

  1. Jdk配置串在profile中

    JAVA_HOME=/home/will/appSource/jdk1.7.0_25PATH=$JAVA_HOME/bin:$PATHCLASSPATH=.:$JAVA_HOME/lib/dt.jar ...

  2. apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName的解决

    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ...

  3. HDOJ-三部曲一(搜索、数学)-1013-Sudoku

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  4. 20145210姚思羽《Java程序设计》实验一实验报告

    实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验知识点 1. ...

  5. Unity3D ShaderLab 简单的立方体图反射

    Unity3D ShaderLab 简单的立方体图反射 反射是着色器模拟现实环境的一个关键因素,它能使我们的着色器渲染效果更加具备视觉冲击,因为他利用了我们周围的环境, 让着色器反射外界的场景信息并将 ...

  6. dbd到mongo的序列化问题及稳定性

    最近工作压力有点大,情绪偶然会失控,要好好反省一下自己. 上周本来打算写CSP相关的东西,但是目前做得还不够多,积累的经验不足,就放弃了.中间找到很好的参考资料,一并放在这里.一篇是比较短的论文,可以 ...

  7. 跟开涛老师学shiro -- 授权

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...

  8. word文档快速取消图片的链接

    快捷键Ctrl+Shift+F9 首先,Ctrl+A全选文章或者用鼠标拖动的方法选中部分文中: 批量删除word文档中的超级链接然后,同时按下键盘上的Ctrl+Shift+F9. 效果就出现了! 宏方 ...

  9. 办公软件word使用技巧 - imsoft.cnblogs

    1,Word中插入水平线 1. 输入连续的三个“-”(减号),然后再敲回车键,就插入了一个细水平分隔线. 2. 输入三个“_” (下划线),然后再敲回车键,就插入了一个粗水平分隔线. 3. 输入三个“ ...

  10. [转载] TCP与UDP对比

    TCP和UDP区别     TCP UDP 是否连接 面向连接 面向非连接 传输可靠性 可靠的 不可靠的 应用场合 传输大量的数据 少量数据 速度 慢 快     OSI 和 TCP/IP 模型在传输 ...