Home > front end >  JS implementing JSON. Parse and JSON. Stringify
JS implementing JSON. Parse and JSON. Stringify

Time:11-28

Write asp recently discovered a problem, the old asp lack of support for the safety of the json data, method does not transfer object character, character object although can pass eval (" (" + json + ") "), the way of json parsing, but easy to be bad people as a Trojan horse entrance, you can expect is a normal json string, but somebody else coming is likely (function () {Response. Write (" Hacked "); Js statement execution}) (), so use js implements the parse and stringify method:
 var JSON={stringify: tojson, parse: fromjson}; 
WSH. Echo (JSON. Stringify (JSON parse (JSON. Stringify ({a: "1 \ \ 2 \ r3 \ n4 interchange " 5 \ t6} "}))));

The function fromjson (STR) {
Var regTag=/[\ {\ [\ "NTF \ d \]/, I=0, len=STR. Length;
The function newParse () {
Var s=waitStr (regTag);
if(! S) return;
The switch (s) {
A case of "{" : return findObj ();
A case of "[" : return findArr ();
A case of "t" : return findTrue ();
Case "f" : return findFalse ();
A case of "n" : return findNull ();
Case '"' : return findStr searches ();
}
Return findNum (s);
}

The function findObj () {
Var obj=new Object;
Var s=waitStr (/\ s/);
If (s=="} ") return obj.
While (I & lt; Len) {
Var key=findStr searches ();
WaitStr (/\ :/);
Obj [key]=newParse ();
S=waitStr (/[\ \}]);
Break the if (s=="} ");
WaitStr (///");
}
Return obj.
}

The function findArr () {
Var arr=new Array;
Var s=waitStr (/\ s/);
If (s=="] ") return arr.
i--;
While (I & lt; Len) {
Arr. Push (newParse ());
S=waitStr (/[\ \]]).
If (s=="] ") break;
}
return arr;
}

The function findTrue () {I +=3; return true; }
The function findFalse () {I +=4; return false; }
The function findNull () {I +=3; return null; }

The function findStr searches () {
Var s="";
While (I & lt; Len) {
Var _s=STR. CharAt (i++);
If (_s=='"' & amp; & (s.m atch (/\ \ * $/) | | (" ")) [0]. Length % 2 & lt; 1) break;
S +=_s;
}
Return s.r eplace (/(\ \ +) ([rn \])/g, "" function (rs, $1, $2) {
If ($1. Length % 2 & lt; 1) return $1 + $2;
Return the $1. Slice (0, 1) + ({n: "\ n", "r" : "\ r", '"' : '"'}) [$2];
}). Replace (/\ \ \ \/g, "\ ");
}

The function findNum (s) {
While (I & lt; Len) {
Var _s=STR. CharAt (i++);
if(!/[\ d \]/. The test (_s)) break;
S +=_s;
}
I -;
The return of s - 0;
}

The function waitStr (reg) {
While (I & lt; Len) {
Var s=STR. CharAt (i++);
If (reg. The test (s)) return s;
}
return "";
}
Return newParse ();
}

The function tojson (obj) {
The switch (typeof obj) {
Case "string" : return toStr ();
A case of "number" : return toNum ();
The case "object" : return toObj (obj);
Case "Boolean" : return toBool ();
Case "date" : return toTime ();
A case of "function" : return obj.
Case "undefined" : return '" undefined "';
Default: return "unknown".
}
The function toStr () {return '"' + obj. Replace (/[\ \ \]/g, "" function (STR) {return" \ \ "+ STR; }). Replace (/\ r/g, \ \ "" r"). The replace (\ n/g, "\ \ n") + '"'; }
The function toNum () {return obj. }
The function toBool () {return obj? "True" : "false"; }
The function toObj () {
if(! Obj) return "null".
If (obj instanceof Array) return toArr ();
Var arr=new Array;
For (var x in obj) arr. Push (tojson (x + "") +" : "+ tojson (obj [x]));
Return "{" + arr. The join ()", "+"} ";
}
The function toArr () {
Var arr=new Array;
for(var i=0; I & lt; Obj. Length; I++) arr. Push (tojson (obj [I]));
Return "[" + arr. The join ()", "+"] ".
}
The function toTime () {
Var t=new Date (obj - 0);
Var STR=t.g etFullYear () + "-".
STR +=z (t.g etMonth () + 1) + "-".
STR +=z (t.g etDate ()) + "";
STR +=z (t.g etHours ()) + ":";
STR +=z (t.g etMinutes ()) + ":";
STR +=z (t.g etSeconds ());
Return '"' + STR + "';
}
The function z (num) {return num & lt; 10? "0" + num: num; }
}
due to transfer from character object parse complicated, so there is no do format check, only to realize the safety resolution, therefore, the format of the error does not lead to parse is unusual, also won't affect the normal, normal daily use is no problem, performance, because it is according to parse json string length word for word, each character basically processing time, therefore, the length of the string the performance impact has been the lowest,

CodePudding user response:

CodePudding user response:

 function findStr searches () {
Var s="";
While (I & lt; Len) {
Var _s=STR. CharAt (i++);
If (_s=='"' & amp; & (s.m atch (/\ \ * $/) | | (" ")) [0]. Length % 2 & lt; 1) break;
S +=_s;
}
Return s.r eplace (/(\ \ +) (\ [r/n \])/g, "" function (rs, $1, $2) {
If ($1. Length % 2 & lt; 1) return the rs;
Return the $1. Slice (0, 1) + ({n: "\ n", "r" : "\ r", '"' : '"', "/" : "/"}) [$2];
}). Replace (/\ \ \ \/g, "\ ");
}
modify the findStr searches, solve \/parse error bug,

CodePudding user response:

 var s=new Array, dic={n: "\ n", r: \ r ", "b:" \ b, "f:" \ "f, v:" \ x0b "}; 
Var dec=function (c) {
The switch (c) {
Case "x" : I +=2; Return unescape (" % "+ STR. Substr (I - 2, 2));
Case "u", I +=4; Return unescape (" % u "+ STR. Substr (I - 4, 4));
}
Return c in dic? Dic [c] : c;
};
While (I & lt; Len) {
Var _s=STR. CharAt (i++);
If (_s=='") break;
nullnullnullnull
  •  Tags:  
  • ASP
  • Related