home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / com / adobe / serialization / json / JSONDecoder.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  3.6 KB  |  129 lines

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