Xenon json Variant

Source: Internet
Author: User

However, in the process of using JSON, I encountered a problem-this problem must have been encountered by many people, that is: JSON does not define the date and time transmission mode.
Although json2.js has added Date functions and ISO-8601-style Date/time formats in an update in March this year, the support for Date in a variety of commonly used development tools is still varied and strange, there is no uniformity at all.
Besides the date and time, some classes or functions are required in some cases, which are not supported by JSON.

Some people may have to ask: Since JSON is not supported, why not use other data description/transmission methods?
The reason is that JSON itself is a subset of the functions of JavaScript (its reference standard is ECMAScript). Anyone who understands JavaScript can easily use JSON.

The simplest way to parse JSON is to directly use the eval function to execute it as JavaScript code, while JSON is often transferred between different applications on the Internet, therefore, it is highly risky to directly pass the received JSON content into the eval function. Therefore, the JSON format is strictly specified in the RFC document and a way to verify its security is provided.
This test method disables function execution.

All in all, because JSON is "occasionally" inconvenient to use, I began to use my brains to expand JSON.


After referring to RFC-4627, json2.js, and some common JavaScript syntax pasters, I found that although json2.js already has support for date/time, however, it adopts the syntax analysis mode, which means it is difficult to extend it if you do not have a certain understanding of syntax analysis; even if I have learned a little about syntax analysis, it is not easy to extend it, let alone discuss future maintenance.
So I decided to use the simpler Regular Expression filtering method recommended in the RFC-4627.

The basic implementation of this extension is as follows:
Copy codeThe Code is 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 ($0, $1 ){
// Return T. propertyIsEnumerable ($1 )? '': $1 + '(';
Return T. propertyIsEnumerable ($1 )? '':'@';
}) | Null )&&
This. xeval (s );
};

The basic usage is to create a xenon object and set new members for it to enable extended functions.
You can add extended functions directly to the xenon object, declare functions in the global scope, and set non-function Member values on the xenon object.
Example:
Copy codeThe Code is 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 (0800, 9), "created": $ (1, date ("Tue Jul 27 02:48:03 UTC + 2010 ")), "modified": $(1 )}');
Print (o. list );
Print (o. created );
Print (o. modified = o. created );

Note: This example cannot be directly executed as JScript. NET code. To use it in JScript. NET, you must pass the string "unsafe" as the second parameter to the eval function.
NOTE 2: Add "0, "is to be compatible with the JScript engine used by IE. In its eval implementation, the current non-CLI version of JScript engine cannot properly understand the meaning of the parentheses surrounding the function definition, this causes a syntax error.
In this example, three function extensions are used: Array is the JavaScript built-in function in the global scope; $ is the built-in function implemented in XENON and can reference the same object in multiple places; while date is the packaging of the Date constructor.
In the implementation of XENON, I didn't allow it to support the new operator to create new objects. I didn't find any reason to use new instead of extended functions.

About the name: it was originally intended to be called xJson, but later I thought it was a bit inferior. After being changed to eXtensible ECMAScript Object Notation, I found it to be an Intel registered trademark, therefore, adding N in the middle becomes XENON (eXtensible Native ECMAScript Object Notation ). I checked the dictionary. It is the name of a chemical element ...... Let's just make it happen.
About Security: In the process of designing the verification method, I tried my best to test the character combination I expected and tried to avoid the injection problem. However, due to lack of practical tests, I am not good at grammar analysis and other things, so it may not be absolutely safe. If anyone discovers a security vulnerability, notify me to improve it.
In the future, I will make a simple function to convert from the ECMAScript object to XENON. If there is plenty of time, maybe I will implement the conversion process that includes the class name and constructor.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.