JSON for ages is widely used format to keep configuration, exchange and store data etc. Zebkit uses JSON:
Standard JSON format is quite simple and often cannot cover desired requirements. Zebkit extends JSON interpretation to support number of advanced features. The new JSON interpretation is called Zson (zebkit.Zson class), the number of new JSON features it introduces are the following:
a = new RegExp("[a-z]+","i")
) class: { "a" : { "@RegExp" : [ "[a-z]+", "i" ] } }
{ "test": 100 }
{ "a": 12, "b": "%{a}" }
{ "a": { ".expr" : "10 + 10" } }
{ "a": "%{<img> picture.jpg}" }
. External files referenced with Zson:
Loaded asynchronously.
Can be loaded recursively. That means loaded files can contain references to external resources and so on.
Zebkit Zson keeps the loadings files in order. That means any subsequent field value can be a reference to a an externally loaded field.
For instance Zson below loads “embedded.json” and set the loaded content as a value of “e” field. In its turn “f” field is reference to loaded “e”:
{ "a" : 100,
"b" : { "c": { "@Date" : [] } },
"d" : "%{a}",
"e" : "%{<json> http://test.com/embedded.json}",
"f" : "%{e.m.k}"
}
Where “embedded.json” is the following:
{ "m" : { "k" : "Hello" } }
The result of the Zson loading described below:
{"m":{ "k": "Hello" }}
The code that load the Zson us shown below:
new Zson().then(function("config.json", zson) {
zson.root // handle fully loaded json
});
Zson can be used to configure classes and class instances with required properties. The way to do it looks as follow:
var A = zebkit.Class([
function() { // constructor
this.b = this.a = null;
},
function setC(c) { // setter of property "c"
this.c = c;
}
]);
{
"a" : "value of property 'a'",
"b" : { "@Date": [] },
"c" : [ 1,2,3 ]
}
var a = new A();
// configure instance of class "A" with JSON
zebkit.Zson.then("config.json", a);
The result is: