转载

Jsonnet - 一个帮助你定义 JSON 数据的配置语言

Jsonnet is a domain specific configuration language that helps you define JSON data. Jsonnet lets you compute fragments of JSON within the structure, bringing the same benefit to structured data that templating languages bring to plain text. The example below illustrates a few features -- referring to another part of the structure, overriding object fields, and string operations.

// Jsonnet Example {  person1: {   name: "Alice",   welcome: "Hello " + self.name + "!",  },  person2: self.person1 { name: "Bob" }, }  
{  "person1": {   "name": "Alice",   "welcome": "Hello Alice!"  },  "person2": {   "name": "Bob",   "welcome": "Hello Bob!"  } }  

Use Jsonnet to organize your JSON data, or to help your users organize the data they send to you. It is easy to integrate Jsonnet's library or commandline interpreter into existing systems that take JSON or YAML . Some example situations are application configuration (files in etc), build systems, package definitions, configuration management systems, and cloud deployment agents.

Variants

Jsonnet is ideal when you need to derive new data from existing data with subtle changes. This was hinted at in the above example. A more comprehensive illustration is the simple build configuration below. An object CCompiler is defined, which is then specialized for GCC and Clang. These are used to help construct an array of build targets.

Note the example mixins Opt and Dbg that can be applied to add optimization flags. Mixins are the ultimate form of inheritance, as they can be combined arbitrarily via the + operator. In simple cases, they behave identically to conventional object-oriented languages like Java.

// Compiler template local CCompiler = {  cFlags: [],  out: "a.out",  local flags_str = std.join(" ", self.cFlags),  local files_str = std.join(" ", self.files),  cmd: "%s %s %s -o %s" % [self.compiler, flags_str, files_str, self.out], }; // GCC specialization local Gcc = CCompiler { compiler: "gcc" }; // Another specialization local Clang = CCompiler { compiler: "clang" }; // Mixins - append flags local Opt = { cFlags: super.cFlags + ["-O3", "-DNDEBUG"] }; local Dbg = { cFlags: super.cFlags + ["-g"] }; // Output: {  targets: [   Gcc { files: ["a.c", "b.c"] },   Clang { files: ["test.c"], out: "test" },   Clang + Opt { files: ["test2.c"], out: "test2" },   Gcc + Opt + Dbg { files: ["foo.c", "bar.c"], out: "baz" },  ] }  

Try it outhere

Provenance, Copyright, License

Jsonnet was designed and implemented at Google as a 20% project. The name Jsonnet is a portmanteau of JSON and sonnet , pronounced "jay sonnet". Jsonnet has been open-sourced under the Apache 2.0 license. Jsonnet is not an official Google product.

News

Sept 06, 2014 New website, lots of new content!

August 23, 2014 Jsonnet is used as a configuration language in cloud-launcher !

August 09, 2014 We have created a group for community discussions.

正文到此结束
Loading...