But in the process of using JSON, I ran into a problem-a problem that must have been encountered by everyone, that is: JSON does not define how dates and times are passed.
Although Json2.js has increased the date/time format for date functions and ISO-8601 in an update in March this year, the support for dates in a variety of commonly used development tools is diverse, bizarre, and completely without uniformity.
And besides date/time, we need some class or function support, which is not supported by JSON.
Some people may see it here: Since JSON doesn't support it, why not use other data description/transmission mode?
The reason is that JSON itself is a subset of the functionality of JavaScript (its reference standard is ECMAScript), and anyone with a little knowledge of JavaScript can easily take advantage of JSON.
The easiest way to parse JSON is to use the Eval function directly as a JavaScript code, and JSON is often used to pass between different applications on the Internet, so it's risky to directly pass the JSON content you receive to the Eval function. Therefore, the JSON format is strictly defined in the RFC document, and the method of verifying its security is given.
And this test method forbids the function to run.
All in all, because JSON is a bit inconvenient to use on a "occasional" scale, I started to expand JSON.
After referencing the RFC-4627, Json2.js, and some of the common JavaScript syntax shaders, I found that although Json2.js already has support for date/time, it uses the pattern of parsing, which means that if there is not a certain understanding of parsing, It's hard to extend it, and even if I've been learning a bit about grammar, it's not easy to extend it, let alone future maintenance.
So I decided to use the simpler regular expression filtering method suggested in RFC-4627.
The basic implementation of this extension is this:
Copy Code code as follows:
function Xenon () {}
var protoxenon = Xenon.prototype;
Protoxenon.xeval = function (s) {
var al = [], VL = [], ol = {};
function $ (i, V) {
i = parseint (i);
return Ol[i] | | (Ol[i] = v);
return ol.propertyisenumerable (i)? Ol[i]: (ol[i] = v);
}
for (var n in this)
if (this.propertyisenumerable (n) && typeof this[n] = = ' function ')
Al.push (n), Vl.push (This[n]);
Return eval (' 0,function (' + al + ') {return ' + S + ';} '). Apply (this, VL);
};
Protoxenon.safexeval = function (s) {
var T = this;
Return (!/[^\),: {}\[\]0-9.\-+eaeflnr-u \n\r\t]/.test (
S.replace (/"(\\.| [^"\\]) * "/g,")
. replace (/([^\s:\[,\ (]+?) \ ($/g,function ($) {
Return t.propertyisenumerable ($)? ': $ + ' (';
Return t.propertyisenumerable ($)? '' : '@';
})) || NULL) &&
This.xeval (s);
};
The basic use is to create a Xenon object, set new members for it, and enable the extension function.
You can add an extension function directly to a Xenon object, or you can declare a function in the global scope and then set the member value of a non-function type on the Xenon object.
Example:
Copy Code code as follows:
var xenon = new Xenon ();
Xenon. Array = 0;
xenon.$ = 0;
Xenon.date = function (s) {return new date (s);
var o = xenon.safexeval (' {"List": Array (3,6,9), "created": $ (1,date ("Tue June 02:48:03 utc+0800"), "Modified": $ (1) }');
Print (o.list);
Print (o.created);
Print (o.modified = = o.created);
Note: This example cannot be executed directly as JScript.NET code, and to be used in JScript.NET you must pass the string "unsafe" as the second argument to the Eval function.
Note The 2:function keyword is added "0," to be compatible with the JScript engine used by IE-the current non-CLI version of the JScript engine does not correctly understand the meaning of parentheses surrounding function definitions in its eval implementation, thus raising a syntax error.
In this example, three function extensions are used: The array is a JavaScript built-in function in the global scope; $ is a built-in feature I implemented in xenon that can refer to the same object in more than one place, and date is the wrapper to the date builder.
In the implementation of xenon I did not let it support the new operator to create an object, and I did not find any reason to use new instead of extending the function directly.
About the name: Originally intended to be called Xjson, but later think that a bit lame, changed to Xeon (extensible ECMAScript Object notation) and then found that seems to be the registered trademarks of Intel, So an extra n in the middle becomes a xenon (extensible Native ECMAScript Object notation). After checking the dictionary, it is the name of a chemical element ... Let's just do it.
About security: In the process of designing a test method, I tried to test the combination of characters I wanted to avoid injecting problems. But because of the lack of practical testing, I am not good at grammar analysis and other things, so it may not be absolutely safe. If anyone finds a security breach, you can notify me to improve it.
There is time later I will do a simple function of converting from ECMAScript object to xenon; If there is really plenty of time, maybe I will implement the conversion process that contains the class name and constructor.