Google Proposes to Enhance JSON with Jsonnet
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 false. if 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的更多相关文章
- 使用google的GSON解析json格式的数据
GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...
- Google提议使用Jsonnet来增强JSON
Google开源了一门配置语言Jsonnet来取代JSON,它完全向后兼容并加入了一些新特性:注释.引用.算术运算.条件操作符,数组和对象内含,引入,函数,局部变量,继承等.Jsonnet程序被翻译为 ...
- Net.Json 常用例子
#JsonConvert 例子 内容主要都是官方的例子,加上一些中文注释而已. 主要方便自己查询,分享一份出来. 参考文档: https://www.newtonsoft.com/json/help/ ...
- 【JavaScript】详解JSON
目录结构: // contents structure [-] 什么是JSON JSON和XML的比较 相同点 不同点 JSON语法 如何解析JSON文本 eval()方法 JSON.parse()方 ...
- 使用Gson解析json
前边的博客说过将json解析成java的方法,使用的是 这几个jar包,但是在解析时层遇到一个问题,就是在将时间字符串转换为java的Timestamp对象时会抛出异常,这个问题一直放在哪里没有去解决 ...
- JSON风格指南-真经
简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语 ...
- Newtonsoft.Json的使用
JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.和 XML 一样,JSON 也是基于纯文本的数据格式 ...
- Android开源库--Gson谷歌官方json解析库
官方文档地址:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html 官方网站:http://code.go ...
- C#操作JSON学习
JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...
随机推荐
- vi中的批量替换
举个例子啊: 将文件tihuan(假设此文本中字符a)中的所有字符a换成字符w,其命令为: 1.vi tihuan 2.按esc键 3.按shift+: 4.在:后输入 %s/a/w/g 就ok ...
- ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法
ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法 前几天卸掉了用了好多年的Windows,安装了Ubuntu12.04,就开始各种搭环境.今天装好了MySQ ...
- 数位dp-POJ-3252-Round Numbers
最近一直在看书和博客,即使做出几道题来也是看别人题解写的,感觉没自己的东西,所以很久没更新博客 看了很多数位dp的题和题解,发现数位dp题是有固定的模版的,并且终于自己做出来一道. 我觉得用记忆化搜索 ...
- LITTLE SHOP OF FLOWERS_DP
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20802 Accepted: 9613 Description You ...
- GridView 分页方法
要实现GrdView分页的功能. 操作如下: 1.更改GrdView控件的AllowPaging属性为true. 2.更改GrdView控件的PageSize属性为 任意数值(默认为10) 3.更改G ...
- 用C语言计算圆的面积~!!!!!!!
#include <stdio.h>void main(){ int a,b,c,y,g,f; printf("圆柱底面的半径,圆柱的高"); scanf(" ...
- Smart210学习记录-------文件操作
一.linux文件操作(只能在linux系统上用) 创建:int creat(const char* filename, mode_t mode) filename 表示要创建的文件名,mode表示对 ...
- 转:SSL协议详解
http://kb.cnblogs.com/page/162080/ 背景介绍 最近在看<密码学与网络安全>相关的书籍,这篇文章主要详细介绍一下著名的网络安全协议SSL. 在开始SS ...
- iptables导致数据包过多时连接失败
前几天做服务器压力测试,本地10个线程不停的去向服务器建立连接,然后请求数据,然后连接再关闭,程序每运行几万次之后就会发现客户端程序没办法connect服务器,connect超时. 一开始怀疑是自己服 ...
- 【Sublime Text 3】插件
TrailingSpacer 高亮显示多余的空格和Tab HTML-CSS-JS Prettify