home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Puzzle / Clusterz / Clusterz.swf / scripts / it / gotoandplay / smartfoxserver / json / JSONDecoder.as < prev    next >
Encoding:
Text File  |  2008-09-12  |  3.4 KB  |  128 lines

  1. package it.gotoandplay.smartfoxserver.json
  2. {
  3.    public class JSONDecoder
  4.    {
  5.        
  6.       
  7.       private var tokenizer:JSONTokenizer;
  8.       
  9.       private var token:JSONToken;
  10.       
  11.       private var obj:Object;
  12.       
  13.       public function JSONDecoder(param1:String)
  14.       {
  15.          super();
  16.          tokenizer = new JSONTokenizer(param1);
  17.          nextToken();
  18.          obj = parseValue();
  19.       }
  20.       
  21.       private function parseValue() : Object
  22.       {
  23.          switch(token.type)
  24.          {
  25.             case JSONTokenType.LEFT_BRACE:
  26.                return parseObject();
  27.             case JSONTokenType.LEFT_BRACKET:
  28.                return parseArray();
  29.             case JSONTokenType.STRING:
  30.             case JSONTokenType.NUMBER:
  31.             case JSONTokenType.TRUE:
  32.             case JSONTokenType.FALSE:
  33.             case JSONTokenType.NULL:
  34.                return token.value;
  35.             default:
  36.                tokenizer.parseError("Unexpected " + token.value);
  37.                return null;
  38.          }
  39.       }
  40.       
  41.       private function nextToken() : JSONToken
  42.       {
  43.          return token = tokenizer.getNextToken();
  44.       }
  45.       
  46.       private function parseObject() : Object
  47.       {
  48.          var _loc1_:Object = null;
  49.          var _loc2_:String = null;
  50.          _loc1_ = new Object();
  51.          nextToken();
  52.          if(token.type == JSONTokenType.RIGHT_BRACE)
  53.          {
  54.             return _loc1_;
  55.          }
  56.          while(true)
  57.          {
  58.             if(token.type == JSONTokenType.STRING)
  59.             {
  60.                _loc2_ = String(token.value);
  61.                nextToken();
  62.                if(token.type == JSONTokenType.COLON)
  63.                {
  64.                   nextToken();
  65.                   _loc1_[_loc2_] = parseValue();
  66.                   nextToken();
  67.                   if(token.type == JSONTokenType.RIGHT_BRACE)
  68.                   {
  69.                      break;
  70.                   }
  71.                   if(token.type == JSONTokenType.COMMA)
  72.                   {
  73.                      nextToken();
  74.                   }
  75.                   else
  76.                   {
  77.                      tokenizer.parseError("Expecting } or , but found " + token.value);
  78.                   }
  79.                }
  80.                else
  81.                {
  82.                   tokenizer.parseError("Expecting : but found " + token.value);
  83.                }
  84.             }
  85.             else
  86.             {
  87.                tokenizer.parseError("Expecting string but found " + token.value);
  88.             }
  89.          }
  90.          return _loc1_;
  91.       }
  92.       
  93.       private function parseArray() : Array
  94.       {
  95.          var _loc1_:Array = null;
  96.          _loc1_ = new Array();
  97.          nextToken();
  98.          if(token.type == JSONTokenType.RIGHT_BRACKET)
  99.          {
  100.             return _loc1_;
  101.          }
  102.          while(true)
  103.          {
  104.             _loc1_.push(parseValue());
  105.             nextToken();
  106.             if(token.type == JSONTokenType.RIGHT_BRACKET)
  107.             {
  108.                break;
  109.             }
  110.             if(token.type == JSONTokenType.COMMA)
  111.             {
  112.                nextToken();
  113.             }
  114.             else
  115.             {
  116.                tokenizer.parseError("Expecting ] or , but found " + token.value);
  117.             }
  118.          }
  119.          return _loc1_;
  120.       }
  121.       
  122.       public function getObject() : Object
  123.       {
  124.          return obj;
  125.       }
  126.    }
  127. }
  128.