Explanation of example code of JS realizing JSON. stringify

  • 2021-07-18 06:33:43
  • OfStack

JSON. stringify is a method for converting Objtect object of JS into JSON string in the high version of browser. However, there is no JSON object under IE6. Therefore, when using this method, it is necessary to write a set of compatible codes. JSON. stringify 1 some rules and note: When the object is a number, null, boolean, directly converted to the corresponding string can be. However, string, function, undefined, object, array, etc. need special treatment.

1. undefined, when this type is processed by JSON. stringify, if the object is undefined, it will output "undefined", and if the object is an array element, it will become null, such as [undefined], and stringify will become "[null]"; If the object is an element of object, the attribute will be treated as nonexistent and not output, such as {a: 1, b: undefined}, followed by "{\" a\ ": 1}, and the B attribute will be discarded directly.

2. When splicing strings, you need to dispose of the internal double quotation marks

The code is as follows


/** 
 * JSON stringify Implementation of  
 * @author norkts<norkts@gmail.com> 
 * @version 1.0 2015-11-24 15:11  The basic functions are realized  
 * @version 1.1 2015-11-24 15:19 norkts  Increased JSON.stringify Implementation of dual-purpose code based on  
 * @version 1.2 2015-11-24 15:49 norkts  Modify Array indexOf In IE Incompatible writing under , Modified undefined Special treatment of values  
 */ 
(function(NS){ 
 
 // Simple type  
 var simpleTypes = ["number", "boolean", "undefined", "string", "function"]; 
  
 //JSON.stringify Main function of  
 function stringify(object){ 
  var type = typeof object; 
   
  // If it is a simple type, the result of the simple type is returned directly  
  if(indexOf(simpleTypes, type) > -1){ 
   return parseSimpleObject(object); 
  } 
 
  // Of the array object  
  if(object instanceof Array){ 
   var len = object.length; 
   var resArr = []; 
   for(var i = 0; i < len; i++){ 
    var itemType = typeof object[i]; 
    if(indexOf(simpleTypes, itemType) > -1){ 
 
     //undefined Special processing, the array becomes null 
     if(itemType != "undefined"){ 
      resArr.push(parseSimpleObject(object[i])); 
     }else{ 
      resArr.push("null"); 
     } 
      
    }else{ 
     // Recursive processing JS Complex elements in arrays  
     resArr.push(stringify(object[i])); 
    } 
   } 
    
   return "[" + resArr.join(",") + "]"; 
  } 
   
  // Ordinary object Object  
  if(object instanceof Object){ 
   if(object == null){ 
    return "null"; 
   } 
    
   var resArr = []; 
    
   for(var name in object){ 
    var itemType = typeof object[name]; 
    if(indexOf(simpleTypes, itemType) > -1){ 
     //undefined Special treatment, object Not coded in  
     if(itemType != "undefined"){ 
      resArr.push("\"" + name + "\":" + parseSimpleObject(object[name]));  
     } 
    }else{ 
     resArr.push("\"" + name + "\":" + stringify(object[name])); 
    } 
   } 
    
   return "{" + resArr.join(",") + "}"; 
  } 
 } 
  
 function parseSimpleObject(object){ 
  var type = typeof object; 
  if(type == "string" || type == "function"){ 
   return "\"" + object.toString().replace("\"", "\\\"") + "\""; 
  } 
   
  if(type == "number" || type == "boolean"){ 
   return object.toString(); 
  } 
   
  if(type == "undefined"){ 
   return "undefined"; 
  } 
   
  return "\"" + object.toString().replace("\"", "\\\"") + "\""; 
 } 
  
 function indexOf(arr, val){ 
  for(var i = 0; i < arr.length; i++){ 
   if(arr[i] === val){ 
    return i; 
   } 
  } 
   
  return -1; 
 } 
  
 /** 
  *  Will stringify Do 2 Sub-encapsulation  
  * @param object  Object to be processed  
  * 
  */ 
 NS.stringify = function(object, isEncodeZh){ 
  var res = stringify(object); 
  if(isEncodeZh){ 
   var encodeRes = ""; 
   for(var i = 0; i < res.length; i++){ 
    if(res.charCodeAt(i) < 0xff){ 
     encodeRes += res[i]; 
    }else{ 
     encodeRes += "\\u" + res.charCodeAt(i).toString(16); 
    } 
   } 
   res = encodeRes; 
  } 
   
  return res; 
 }; 
})(window); 

Related articles: