Advertisement
tinyevil

Untitled

Nov 18th, 2018
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     use namespace AS3;
  4.    
  5.     [native(classgc="exact",methods="auto",cls="JSONClass",construct="none")]
  6.     [API("674")]
  7.     public final class JSON
  8.     {
  9.         public function JSON()
  10.         {
  11.             super();
  12.         }
  13.        
  14.         native private static function parseCore(param1:String) : Object;
  15.        
  16.         native private static function stringifySpecializedToString(param1:Object, param2:Array, param3:Function, param4:String) : String;
  17.        
  18.         public static function parse(text:String, reviver:Function = null) : Object
  19.         {
  20.             if( text == null )
  21.             {
  22.                 Error.throwError(SyntaxError,1132);
  23.             }
  24.             var unfiltered:Object = parseCore(text);
  25.             if(reviver === null)
  26.             {
  27.                 return unfiltered;
  28.             }
  29.             return new Walker(reviver).walk({"":unfiltered},"");
  30.         }
  31.        
  32.         public static function stringify(value:Object, replacer:* = null, space:* = null) : String
  33.         {
  34.             if(!(replacer === null || replacer is Function || replacer is Array))
  35.             {
  36.                 Error.throwError(TypeError,1131);
  37.             }
  38.             var isfloat:Boolean = false;
  39.             if(!(space === null || space is String || space is Number || isfloat))
  40.             {
  41.                 space = null;
  42.             }
  43.             var gap:* = "";
  44.             if(space is String)
  45.             {
  46.                 gap = space.length > 10?space.substring(0,10):space;
  47.             }
  48.             else if(space is Number || isfloat)
  49.             {
  50.                 gap = "          ".substring(0,Math.min(10,Math.floor(space)));
  51.             }
  52.             if(replacer === null)
  53.             {
  54.                 return stringifySpecializedToString(value,null,null,gap);
  55.             }
  56.             if(replacer is Array)
  57.             {
  58.                 return stringifySpecializedToString(value,computePropertyList(replacer),null,gap);
  59.             }
  60.             return stringifySpecializedToString(value,null,replacer,gap);
  61.         }
  62.        
  63.         private static function computePropertyList(r:Array) : Array
  64.         {
  65.             var v:* = undefined;
  66.             var item:String = null;
  67.             var isfloat:Boolean = false;
  68.             var propertyList:* = [];
  69.             var alreadyAdded:* = {};
  70.             for(var i:uint = 0, ilim:uint = r.length; i < ilim; i++)
  71.             {
  72.                 if(r.hasOwnProperty(i))
  73.                 {
  74.                     v = r[i];
  75.                     item = null;
  76.                     isfloat = false;
  77.                     if(v is String)
  78.                     {
  79.                         item = v;
  80.                     }
  81.                     else if(v is Number || isfloat)
  82.                     {
  83.                         item = String(v);
  84.                     }
  85.                     if(item !== null && !alreadyAdded[item])
  86.                     {
  87.                         alreadyAdded[item] = true;
  88.                         propertyList[propertyList.length] = item;
  89.                     }
  90.                 }
  91.             }
  92.             return propertyList;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement