Author: Truly
Date: 2007.7.29
As in front of meArticleJSON occupies an important position in modern JavaScript programming. If you have read large class librariesCode, Such as ajaxpro, ms asp. NET Ajax, etc, you will find that in the Organization and interaction data and are generally using the JSON method. In my previous article "Application of design patterns in JavaScript (1)", simple data ing is used to avoid time zone differences, I simply represent the date in JSON format as follows:
" Createdate " : New Date ('wed, 25 Jul 2007 09 : 59 : 00 GMT + 8 ')
Such date representation is simple and readable in small applications, but for large applicationsProgramIt is more important to better organize data, compress data, and reduce traffic. Today, we will discuss JSON processing of date types in Microsoft ASP. NET Ajax.
First, let's take a look at the processing of date-type JSON serialization on the ASP. NET Ajax server: (for details, seeJavascriptserializer. CS)
Internal Static Readonly Long Datetimemintimeticks = ( New Datetime ( 1970 , 1 , 1 , 0 , 0 , 0 , Datetimekind. UTC). ticks;
Private Static Void Serializedatetime (datetime, stringbuilder SB ){
SB. append ( @" "" \/Date ( " );
SB. append (datetime. touniversaltime (). ticks - Datetimemintimeticks) / 10000 );
SB. append ( @" )\/"" " );
}
At the earliest stage, Microsoft used the following method for serialization:
The Ajax JSON serialization program in ASP. Next encodes the datetime instance into a JSON string. In the pre-release period, ASP. NET Ajax uses the format"@ Ticks @Ticks indicates the number of milliseconds since January 1, January 1, 1970 in Universal Coordinated Time (UTC. The date and time in UTC (for example, November 29, 1989 4:55:30 AM) are encoded as"@ 62831853071 @". Although easy to understand, this format cannot distinguish the serialized date and time value from the value that looks like the serialized date but does not need to be deserialized. Therefore, the ASP. NET Ajax team modified the final version and solved this problem by adopting the \/date (ticks) \/format.
The new style uses a small trick to reduce the possibility of misunderstanding. In JSON, the forward slash (/) character in the string can be escaped using the backslash (\) (even if this is not strictly required ). The ASP. NET Ajax team used this to modify javascriptserializer and write the datetime instance as a string"\/Date (ticks )\/". The escape of the two forward slashes is superficial, but it is critical to javascriptserializer. According to the JSON rule,\/Date (ticks )\/"Technically equivalent to"/Date (ticks )/", But javascriptserializer will deserialize the former to datetime and deserialize the latter to string. Therefore,@ Ticks @"Format, the possibility of obfuscation is greatly reduced.
Note:This text is from the msdn articleIntroduction to JavaScript Object Notation (JSON) in JavaScript and. net
Here I want to add a trick. As you know, If <SCRIPT> and </SCRIPT> appear in the document, the step interpreter will parse the text, therefore, if you write the following code, an error will be prompted:
< Script >
Document. Write ( " <SCRIPT> alert (1); </SCRIPT> " );
</ Script >
Because the code "</SCRIPT>" appears in the middle, we can use:
< Script >
Document. Write ( " <SCRIPT> alert (1); </scr " + " IPT> " );
</ Script >
To avoid this error, we can also use "\" to escape:
< Script >
Document. Write ( " <SCRIPT> alert (1); <\/SCRIPT> " );
</ Script >
Next we will look at the deserialization code of the date in the ASP. NET Ajax script Library:
SYS. serialization. javascriptserializer. deserialize = Function Sys $ serialization $ javascriptserializer $ deserialize (data ){
// /<Param name = "data" type = "string"> </param>
// /<Returns> </returns>
VaR E = Function. _ validateparams (arguments ,[
{Name: " Data " , Type: string}
]);
If (E) Throw E;
If (Data. Length === 0 ) Throw Error. Argument ('data', SYS. res. cannotdeserializeemptystring );
Try {
VaR Exp = Data. Replace ( New Regexp ('( ^ | [ ^ \]) \ " \\\\/Date \\((-? [0-9] + )\\)\\\\/\\ " ', 'G '), " $ 1new date ($2) " );
Return Eval ('(' + Exp + ')');
}
Catch (E ){
Throw Error. Argument ('data', SYS. res. cannotdeserializeinvalidjson );
}
}
I have sorted out the relevant code of this serializer into the SYS. serialization. js file. If you are interested, you can download and view it.
Summary: This method is of great reference value and is worth our reference when encapsulating data. For JavaScript data compression, I have a simple practice: compressing JavaScript macros. and more advancedObfuscationCompression will be left for the next article to focus on.
Thank you for reading.