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采用完全独立于语言的文本格式,可以很容易在 ...
随机推荐
- 多态-II(接口实现)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ERP部门的添加(十一)
功能需求: 1.有部门管理权限的人员进行添加部门基本信息. 2.有部门权限管理的人员查询部门基本信息. 3.有部门权限管理的人员进行修改部门基本信息. 4.在一个页面中实现,使用弹出对话框方式 存储过 ...
- 郝斌老师的SQL教程
时隔两年,重拾数据库编程.郝斌老师的sql教程通俗易懂,用作复习简直不能太赞.
- HttpHandler中无法使用Session(context.Session 为Null)
一.问题描述: 最近需要做一个对特定请求(.report)进行响应的接口,当然是使用实现IHttpHandler来进行处理,实现IHttpHandler接口就必须要实现它的两个方法,ProcessRe ...
- JNI c++ 调用 java
----------------------------------------------c++--------------------------------------- #include &q ...
- 在Silverlight中实施RESTful调用
在Silverlight中实施RESTful调用: http://book.51cto.com/art/201006/206314.htm http://bbs.csdn.net/topic ...
- NSData和NSString 、 NSFileManager
1 NSData和NSMutableData的基本使用 1.1 问题 NSData类是IOS提供的用于以二进制的形式操作文件数据的类,NSData有两个常用的属性length和bytes,length ...
- c/c++ main函数执行之前/后
转载自:http://bbs.csdn.net/topics/300103318#r_78088969 main函数之前--真正的函数执行入口或开始 一种解释: 实际上,在可执行文件被加载之后,控制权 ...
- Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- PAT (Basic Level) Practise:1033. 旧键盘打字
[题目链接] 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在2行中分别给出坏掉的那些键.以 ...