Exemplo n.º 1
0
  @SuppressWarnings("unchecked")
  public <T> T parseObject(Type type, Object fieldName) {
    int token = lexer.token();
    if (token == JSONToken.NULL) {
      lexer.nextToken();
      return null;
    }

    if (token == JSONToken.LITERAL_STRING) {
      if (type == byte[].class) {
        byte[] bytes = lexer.bytesValue();
        lexer.nextToken();
        return (T) bytes;
      }

      if (type == char[].class) {
        String strVal = lexer.stringVal();
        lexer.nextToken();
        return (T) strVal.toCharArray();
      }
    }

    ObjectDeserializer derializer = config.getDeserializer(type);

    try {
      return (T) derializer.deserialze(this, type, fieldName);
    } catch (JSONException e) {
      throw e;
    } catch (Throwable e) {
      throw new JSONException(e.getMessage(), e);
    }
  }
Exemplo n.º 2
0
  public DefaultJSONParser(final Object input, final JSONLexer lexer, final ParserConfig config) {
    this.lexer = lexer;
    this.input = input;
    this.config = config;
    this.symbolTable = config.getSymbolTable();

    lexer.nextToken(JSONToken.LBRACE); // prime the pump
  }
Exemplo n.º 3
0
  /**
   * Setups up the parallel parsing environment and blocks until parsing has finished (successfully
   * or with an error).
   */
  public void startParsing(Set<BuildTarget> toExplore, ParserConfig parserConfig, Executor executor)
      throws InterruptedException {
    Preconditions.checkArgument(
        completionNotifier.getCount() == 1,
        "Only one invocation of `startParsing` allowed until completion");
    addBuildTargetsToProcess(toExplore);

    // Create the worker threads.
    for (int i = parserConfig.getNumParsingThreads(); i > 0; i--) {
      executor.execute(new BuildTargetParserWorker());
    }

    // Now we wait for parsing to complete.
    completionNotifier.await();
  }
Exemplo n.º 4
0
  @SuppressWarnings("unchecked")
  public <T> T parseObject(Type type) {
    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken();
      return null;
    }

    ObjectDeserializer derializer = config.getDeserializer(type);

    try {
      return (T) derializer.deserialze(this, type, null);
    } catch (JSONException e) {
      throw e;
    } catch (Throwable e) {
      throw new JSONException(e.getMessage(), e);
    }
  }
Exemplo n.º 5
0
  public void parseObject(Object object) {
    Class<?> clazz = object.getClass();
    Map<String, FieldDeserializer> setters = config.getFieldDeserializers(clazz);

    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
      throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }

    final Object[] args = new Object[1];

    for (; ; ) {
      // lexer.scanSymbol
      String key = lexer.scanSymbol(symbolTable);

      if (key == null) {
        if (lexer.token() == JSONToken.RBRACE) {
          lexer.nextToken(JSONToken.COMMA);
          break;
        }
        if (lexer.token() == JSONToken.COMMA) {
          if (isEnabled(Feature.AllowArbitraryCommas)) {
            continue;
          }
        }
      }

      FieldDeserializer fieldDeser = setters.get(key);
      if (fieldDeser == null) {
        if (!isEnabled(Feature.IgnoreNotMatch)) {
          throw new JSONException(
              "setter not found, class " + clazz.getName() + ", property " + key);
        }

        lexer.nextTokenWithColon();
        parse(); // skip

        if (lexer.token() == JSONToken.RBRACE) {
          lexer.nextToken();
          return;
        }

        continue;
      } else {
        Method method = fieldDeser.getMethod();
        Class<?> fieldClass = method.getParameterTypes()[0];
        Type fieldType = method.getGenericParameterTypes()[0];
        if (fieldClass == int.class) {
          lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
          args[0] = IntegerDeserializer.instance.deserialze(this, fieldType, null);
        } else if (fieldClass == String.class) {
          lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
          args[0] = StringDeserializer.deserialze(this);
        } else if (fieldClass == long.class) {
          lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
          args[0] = LongDeserializer.instance.deserialze(this, fieldType, null);
        } else {
          ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType);

          lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken());
          args[0] = fieldValueDeserializer.deserialze(this, fieldType, null);
        }

        try {
          method.invoke(object, args);
        } catch (Exception e) {
          throw new JSONException("set proprety error, " + method.getName(), e);
        }
      }

      if (lexer.token() == JSONToken.COMMA) {
        continue;
      }

      if (lexer.token() == JSONToken.RBRACE) {
        lexer.nextToken(JSONToken.COMMA);
        return;
      }
    }
  }
Exemplo n.º 6
0
  public Object[] parseArray(Type[] types) {
    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken(JSONToken.COMMA);
      return null;
    }

    if (lexer.token() != JSONToken.LBRACKET) {
      throw new JSONException("syntax error : " + lexer.tokenName());
    }

    Object[] list = new Object[types.length];
    if (types.length == 0) {
      lexer.nextToken(JSONToken.RBRACKET);

      if (lexer.token() != JSONToken.RBRACKET) {
        throw new JSONException("syntax error");
      }

      lexer.nextToken(JSONToken.COMMA);
      return new Object[0];
    }

    lexer.nextToken(JSONToken.LITERAL_INT);

    for (int i = 0; i < types.length; ++i) {
      Object value;

      if (lexer.token() == JSONToken.NULL) {
        value = null;
        lexer.nextToken(JSONToken.COMMA);
      } else {
        Type type = types[i];
        if (type == int.class || type == Integer.class) {
          if (lexer.token() == JSONToken.LITERAL_INT) {
            value = Integer.valueOf(lexer.intValue());
            lexer.nextToken(JSONToken.COMMA);
          } else {
            value = this.parse();
            value = TypeUtils.cast(value, type, config);
          }
        } else if (type == String.class) {
          if (lexer.token() == JSONToken.LITERAL_STRING) {
            value = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);
          } else {
            value = this.parse();
            value = TypeUtils.cast(value, type, config);
          }
        } else {
          boolean isArray = false;
          Class<?> componentType = null;
          if (i == types.length - 1) {
            if (type instanceof Class) {
              Class<?> clazz = (Class<?>) type;
              isArray = clazz.isArray();
              componentType = clazz.getComponentType();
            }
          }

          // support varArgs
          if (isArray && lexer.token() != JSONToken.LBRACKET) {
            List<Object> varList = new ArrayList<Object>();

            ObjectDeserializer derializer = config.getDeserializer(componentType);
            int fastMatch = derializer.getFastMatchToken();

            if (lexer.token() != JSONToken.RBRACKET) {
              for (; ; ) {
                Object item = derializer.deserialze(this, type, null);
                varList.add(item);

                if (lexer.token() == JSONToken.COMMA) {
                  lexer.nextToken(fastMatch);
                } else if (lexer.token() == JSONToken.RBRACKET) {
                  break;
                } else {
                  throw new JSONException("syntax error :" + JSONToken.name(lexer.token()));
                }
              }
            }

            value = TypeUtils.cast(varList, type, config);
          } else {
            ObjectDeserializer derializer = config.getDeserializer(type);
            value = derializer.deserialze(this, type, null);
          }
        }
      }
      list[i] = value;

      if (lexer.token() == JSONToken.RBRACKET) {
        break;
      }

      if (lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error :" + JSONToken.name(lexer.token()));
      }

      if (i == types.length - 1) {
        lexer.nextToken(JSONToken.RBRACKET);
      } else {
        lexer.nextToken(JSONToken.LITERAL_INT);
      }
    }

    if (lexer.token() != JSONToken.RBRACKET) {
      throw new JSONException("syntax error");
    }

    lexer.nextToken(JSONToken.COMMA);

    return list;
  }
Exemplo n.º 7
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  public void parseArray(Type type, Collection array, Object fieldName) {
    if (lexer.token() == JSONToken.SET || lexer.token() == JSONToken.TREE_SET) {
      lexer.nextToken();
    }

    if (lexer.token() != JSONToken.LBRACKET) {
      throw new JSONException("exepct '[', but " + JSONToken.name(lexer.token()));
    }

    ObjectDeserializer deserializer = null;
    if (int.class == type) {
      deserializer = IntegerDeserializer.instance;
      lexer.nextToken(JSONToken.LITERAL_INT);
    } else if (String.class == type) {
      deserializer = StringDeserializer.instance;
      lexer.nextToken(JSONToken.LITERAL_STRING);
    } else {
      deserializer = config.getDeserializer(type);
      lexer.nextToken(deserializer.getFastMatchToken());
    }

    ParseContext context = this.getContext();
    this.setContext(array, fieldName);
    try {
      for (int i = 0; ; ++i) {
        if (isEnabled(Feature.AllowArbitraryCommas)) {
          while (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken();
            continue;
          }
        }

        if (lexer.token() == JSONToken.RBRACKET) {
          break;
        }

        if (int.class == type) {
          Object val = IntegerDeserializer.instance.deserialze(this, null, null);
          array.add(val);
        } else if (String.class == type) {
          String value;
          if (lexer.token() == JSONToken.LITERAL_STRING) {
            value = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);
          } else {
            Object obj = this.parse();
            if (obj == null) {
              value = null;
            } else {
              value = obj.toString();
            }
          }

          array.add(value);
        } else {
          Object val;
          if (lexer.token() == JSONToken.NULL) {
            lexer.nextToken();
            val = null;
          } else {
            val = deserializer.deserialze(this, type, i);
          }
          array.add(val);
          checkListResolve(array);
        }

        if (lexer.token() == JSONToken.COMMA) {
          lexer.nextToken(deserializer.getFastMatchToken());
          continue;
        }
      }
    } finally {
      this.setContext(context);
    }

    lexer.nextToken(JSONToken.COMMA);
  }
Exemplo n.º 8
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  public final Object parseObject(final Map object, Object fieldName) {
    final JSONLexer lexer = this.lexer;

    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
      throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }

    ParseContext context = this.getContext();
    try {
      boolean setContextFlag = false;
      for (; ; ) {
        lexer.skipWhitespace();
        char ch = lexer.getCurrent();
        if (isEnabled(Feature.AllowArbitraryCommas)) {
          while (ch == ',') {
            lexer.next();
            lexer.skipWhitespace();
            ch = lexer.getCurrent();
          }
        }

        boolean isObjectKey = false;
        Object key;
        if (ch == '"') {
          key = lexer.scanSymbol(symbolTable, '"');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos() + ", name " + key);
          }
        } else if (ch == '}') {
          lexer.next();
          lexer.resetStringPosition();
          lexer.nextToken();
          return object;
        } else if (ch == '\'') {
          if (!isEnabled(Feature.AllowSingleQuotes)) {
            throw new JSONException("syntax error");
          }

          key = lexer.scanSymbol(symbolTable, '\'');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos());
          }
        } else if (ch == EOI) {
          throw new JSONException("syntax error");
        } else if (ch == ',') {
          throw new JSONException("syntax error");
        } else if ((ch >= '0' && ch <= '9') || ch == '-') {
          lexer.resetStringPosition();
          lexer.scanNumber();
          if (lexer.token() == JSONToken.LITERAL_INT) {
            key = lexer.integerValue();
          } else {
            key = lexer.decimalValue(true);
          }
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos() + ", name " + key);
          }
        } else if (ch == '{' || ch == '[') {
          lexer.nextToken();
          key = parse();
          isObjectKey = true;
        } else {
          if (!isEnabled(Feature.AllowUnQuotedFieldNames)) {
            throw new JSONException("syntax error");
          }

          key = lexer.scanSymbolUnQuoted(symbolTable);
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos() + ", actual " + ch);
          }
        }

        if (!isObjectKey) {
          lexer.next();
          lexer.skipWhitespace();
        }

        ch = lexer.getCurrent();

        lexer.resetStringPosition();

        if (key == JSON.DEFAULT_TYPE_KEY) {
          String typeName = lexer.scanSymbol(symbolTable, '"');
          Class<?> clazz = TypeUtils.loadClass(typeName);

          if (clazz == null) {
            object.put(JSON.DEFAULT_TYPE_KEY, typeName);
            continue;
          }

          lexer.nextToken(JSONToken.COMMA);
          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            try {
              Object instance = null;
              ObjectDeserializer deserializer = this.config.getDeserializer(clazz);
              if (deserializer instanceof JavaBeanDeserializer) {
                instance = ((JavaBeanDeserializer) deserializer).createInstance(this, clazz);
              }

              if (instance == null) {
                if (clazz == Cloneable.class) {
                  instance = new HashMap();
                } else {
                  instance = clazz.newInstance();
                }
              }

              return instance;
            } catch (Exception e) {
              throw new JSONException("create instance error", e);
            }
          }

          this.setResolveStatus(TypeNameRedirect);

          if (this.context != null && !(fieldName instanceof Integer)) {
            this.popContext();
          }

          ObjectDeserializer deserializer = config.getDeserializer(clazz);
          return deserializer.deserialze(this, clazz, fieldName);
        }

        if (key == "$ref") {
          lexer.nextToken(JSONToken.LITERAL_STRING);
          if (lexer.token() == JSONToken.LITERAL_STRING) {
            String ref = lexer.stringVal();
            lexer.nextToken(JSONToken.RBRACE);

            Object refValue = null;
            if ("@".equals(ref)) {
              if (this.getContext() != null) {
                refValue = this.getContext().getObject();
              }
            } else if ("..".equals(ref)) {
              ParseContext parentContext = context.getParentContext();
              if (parentContext.getObject() != null) {
                refValue = parentContext.getObject();
              } else {
                addResolveTask(new ResolveTask(parentContext, ref));
                setResolveStatus(DefaultJSONParser.NeedToResolve);
              }
            } else if ("$".equals(ref)) {
              ParseContext rootContext = context;
              while (rootContext.getParentContext() != null) {
                rootContext = rootContext.getParentContext();
              }

              if (rootContext.getObject() != null) {
                refValue = rootContext.getObject();
              } else {
                addResolveTask(new ResolveTask(rootContext, ref));
                setResolveStatus(DefaultJSONParser.NeedToResolve);
              }
            } else {
              addResolveTask(new ResolveTask(context, ref));
              setResolveStatus(DefaultJSONParser.NeedToResolve);
            }

            if (lexer.token() != JSONToken.RBRACE) {
              throw new JSONException("syntax error");
            }
            lexer.nextToken(JSONToken.COMMA);

            return refValue;
          } else {
            throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
          }
        }

        if (!setContextFlag) {
          setContext(object, fieldName);
          setContextFlag = true;

          // fix Issue #40
          if (this.context != null && !(fieldName instanceof Integer)) {
            this.popContext();
          }
        }

        Object value;
        if (ch == '"') {
          lexer.scanString();
          String strValue = lexer.stringVal();
          value = strValue;

          if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
            JSONScanner iso8601Lexer = new JSONScanner(strValue);
            if (iso8601Lexer.scanISO8601DateIfMatch()) {
              value = iso8601Lexer.getCalendar().getTime();
            }
            iso8601Lexer.close();
          }

          if (object.getClass() == JSONObject.class) {
            object.put(key.toString(), value);
          } else {
            object.put(key, value);
          }
        } else if (ch >= '0' && ch <= '9' || ch == '-') {
          lexer.scanNumber();
          if (lexer.token() == JSONToken.LITERAL_INT) {
            value = lexer.integerValue();
          } else {
            value = lexer.numberValue();
          }

          object.put(key, value);
        } else if (ch == '[') { // 减少嵌套,兼容android
          lexer.nextToken();
          JSONArray list = new JSONArray();
          this.parseArray(list, key);
          value = list;
          object.put(key, value);

          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            return object;
          } else if (lexer.token() == JSONToken.COMMA) {
            continue;
          } else {
            throw new JSONException("syntax error");
          }
        } else if (ch == '{') { // 减少嵌套,兼容android
          lexer.nextToken();
          Object obj = this.parseObject(new JSONObject(), key);
          checkMapResolve(object, key.toString());

          if (object.getClass() == JSONObject.class) {
            object.put(key.toString(), obj);
          } else {
            object.put(key, obj);
          }

          setContext(context, obj, key);

          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();

            setContext(context);
            return object;
          } else if (lexer.token() == JSONToken.COMMA) {
            continue;
          } else {
            throw new JSONException("syntax error, " + lexer.tokenName());
          }
        } else {
          lexer.nextToken();
          value = parse();
          object.put(key, value);

          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            return object;
          } else if (lexer.token() == JSONToken.COMMA) {
            continue;
          } else {
            throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
          }
        }

        lexer.skipWhitespace();
        ch = lexer.getCurrent();
        if (ch == ',') {
          lexer.next();
          continue;
        } else if (ch == '}') {
          lexer.next();
          lexer.resetStringPosition();
          lexer.nextToken();

          this.setContext(object, fieldName);

          return object;
        } else {
          throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
        }
      }
    } finally {
      this.setContext(context);
    }
  }
Exemplo n.º 9
0
 public DefaultJSONParser(final JSONLexer lexer) {
   this(lexer, ParserConfig.getGlobalInstance());
 }
Exemplo n.º 10
0
 public DefaultJSONParser(String input) {
   this(input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE);
 }
Exemplo n.º 11
0
  public void parseObject(Object object) {
    Class<?> clazz = object.getClass();
    JavaBeanDeserializer beanDeser = null;
    ObjectDeserializer deserizer = config.getDeserializer(clazz);
    if (deserizer instanceof JavaBeanDeserializer) {
      beanDeser = (JavaBeanDeserializer) deserizer;
    }

    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
      throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }

    for (; ; ) {
      // lexer.scanSymbol
      String key = lexer.scanSymbol(symbolTable);

      if (key == null) {
        if (lexer.token() == JSONToken.RBRACE) {
          lexer.nextToken(JSONToken.COMMA);
          break;
        }
        if (lexer.token() == JSONToken.COMMA) {
          if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
            continue;
          }
        }
      }

      FieldDeserializer fieldDeser = null;
      if (beanDeser != null) {
        fieldDeser = beanDeser.getFieldDeserializer(key);
      }

      if (fieldDeser == null) {
        if (!lexer.isEnabled(Feature.IgnoreNotMatch)) {
          throw new JSONException(
              "setter not found, class " + clazz.getName() + ", property " + key);
        }

        lexer.nextTokenWithColon();
        parse(); // skip

        if (lexer.token() == JSONToken.RBRACE) {
          lexer.nextToken();
          return;
        }

        continue;
      } else {
        Class<?> fieldClass = fieldDeser.fieldInfo.fieldClass;
        Type fieldType = fieldDeser.fieldInfo.fieldType;
        Object fieldValue;
        if (fieldClass == int.class) {
          lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
          fieldValue = IntegerCodec.instance.deserialze(this, fieldType, null);
        } else if (fieldClass == String.class) {
          lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
          fieldValue = StringCodec.deserialze(this);
        } else if (fieldClass == long.class) {
          lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
          fieldValue = LongCodec.instance.deserialze(this, fieldType, null);
        } else {
          ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType);

          lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken());
          fieldValue = fieldValueDeserializer.deserialze(this, fieldType, null);
        }

        fieldDeser.setValue(object, fieldValue);
      }

      if (lexer.token() == JSONToken.COMMA) {
        continue;
      }

      if (lexer.token() == JSONToken.RBRACE) {
        lexer.nextToken(JSONToken.COMMA);
        return;
      }
    }
  }
Exemplo n.º 12
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  public final Object parseObject(final Map object, Object fieldName) {
    final JSONLexer lexer = this.lexer;

    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken();
      return null;
    }

    if (lexer.token() == JSONToken.RBRACE) {
      lexer.nextToken();
      return object;
    }

    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
      throw new JSONException(
          "syntax error, expect {, actual " + lexer.tokenName() + ", " + lexer.info());
    }

    ParseContext context = this.context;
    try {
      boolean setContextFlag = false;
      for (; ; ) {
        lexer.skipWhitespace();
        char ch = lexer.getCurrent();
        if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
          while (ch == ',') {
            lexer.next();
            lexer.skipWhitespace();
            ch = lexer.getCurrent();
          }
        }

        boolean isObjectKey = false;
        Object key;
        if (ch == '"') {
          key = lexer.scanSymbol(symbolTable, '"');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos() + ", name " + key);
          }
        } else if (ch == '}') {
          lexer.next();
          lexer.resetStringPosition();
          lexer.nextToken();
          return object;
        } else if (ch == '\'') {
          if (!lexer.isEnabled(Feature.AllowSingleQuotes)) {
            throw new JSONException("syntax error");
          }

          key = lexer.scanSymbol(symbolTable, '\'');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos());
          }
        } else if (ch == EOI) {
          throw new JSONException("syntax error");
        } else if (ch == ',') {
          throw new JSONException("syntax error");
        } else if ((ch >= '0' && ch <= '9') || ch == '-') {
          lexer.resetStringPosition();
          lexer.scanNumber();
          try {
            if (lexer.token() == JSONToken.LITERAL_INT) {
              key = lexer.integerValue();
            } else {
              key = lexer.decimalValue(true);
            }
          } catch (NumberFormatException e) {
            throw new JSONException("parse number key error" + lexer.info());
          }
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("parse number key error" + lexer.info());
          }
        } else if (ch == '{' || ch == '[') {
          lexer.nextToken();
          key = parse();
          isObjectKey = true;
        } else {
          if (!lexer.isEnabled(Feature.AllowUnQuotedFieldNames)) {
            throw new JSONException("syntax error");
          }

          key = lexer.scanSymbolUnQuoted(symbolTable);
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos() + ", actual " + ch);
          }
        }

        if (!isObjectKey) {
          lexer.next();
          lexer.skipWhitespace();
        }

        ch = lexer.getCurrent();

        lexer.resetStringPosition();

        if (key == JSON.DEFAULT_TYPE_KEY && !lexer.isEnabled(Feature.DisableSpecialKeyDetect)) {
          String typeName = lexer.scanSymbol(symbolTable, '"');
          Class<?> clazz = TypeUtils.loadClass(typeName, config.getDefaultClassLoader());

          if (clazz == null) {
            object.put(JSON.DEFAULT_TYPE_KEY, typeName);
            continue;
          }

          lexer.nextToken(JSONToken.COMMA);
          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            try {
              Object instance = null;
              ObjectDeserializer deserializer = this.config.getDeserializer(clazz);
              if (deserializer instanceof JavaBeanDeserializer) {
                instance = ((JavaBeanDeserializer) deserializer).createInstance(this, clazz);
              }

              if (instance == null) {
                if (clazz == Cloneable.class) {
                  instance = new HashMap();
                } else if ("java.util.Collections$EmptyMap".equals(typeName)) {
                  instance = Collections.emptyMap();
                } else {
                  instance = clazz.newInstance();
                }
              }

              return instance;
            } catch (Exception e) {
              throw new JSONException("create instance error", e);
            }
          }

          this.setResolveStatus(TypeNameRedirect);

          if (this.context != null && !(fieldName instanceof Integer)) {
            this.popContext();
          }

          if (object.size() > 0) {
            Object newObj = TypeUtils.cast(object, clazz, this.config);
            this.parseObject(newObj);
            return newObj;
          }

          ObjectDeserializer deserializer = config.getDeserializer(clazz);
          return deserializer.deserialze(this, clazz, fieldName);
        }

        if (key == "$ref" && !lexer.isEnabled(Feature.DisableSpecialKeyDetect)) {
          lexer.nextToken(JSONToken.LITERAL_STRING);
          if (lexer.token() == JSONToken.LITERAL_STRING) {
            String ref = lexer.stringVal();
            lexer.nextToken(JSONToken.RBRACE);

            Object refValue = null;
            if ("@".equals(ref)) {
              if (this.context != null) {
                ParseContext thisContext = this.context;
                Object thisObj = thisContext.object;
                if (thisObj instanceof Object[] || thisObj instanceof Collection<?>) {
                  refValue = thisObj;
                } else if (thisContext.parent != null) {
                  refValue = thisContext.parent.object;
                }
              }
            } else if ("..".equals(ref)) {
              if (context.object != null) {
                refValue = context.object;
              } else {
                addResolveTask(new ResolveTask(context, ref));
                setResolveStatus(DefaultJSONParser.NeedToResolve);
              }
            } else if ("$".equals(ref)) {
              ParseContext rootContext = context;
              while (rootContext.parent != null) {
                rootContext = rootContext.parent;
              }

              if (rootContext.object != null) {
                refValue = rootContext.object;
              } else {
                addResolveTask(new ResolveTask(rootContext, ref));
                setResolveStatus(DefaultJSONParser.NeedToResolve);
              }
            } else {
              addResolveTask(new ResolveTask(context, ref));
              setResolveStatus(DefaultJSONParser.NeedToResolve);
            }

            if (lexer.token() != JSONToken.RBRACE) {
              throw new JSONException("syntax error");
            }
            lexer.nextToken(JSONToken.COMMA);

            return refValue;
          } else {
            throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
          }
        }

        if (!setContextFlag) {
          ParseContext contextR = setContext(object, fieldName);
          if (context == null) {
            context = contextR;
          }
          setContextFlag = true;
        }

        if (object.getClass() == JSONObject.class) {
          key = (key == null) ? "null" : key.toString();
        }

        Object value;
        if (ch == '"') {
          lexer.scanString();
          String strValue = lexer.stringVal();
          value = strValue;

          if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
            JSONScanner iso8601Lexer = new JSONScanner(strValue);
            if (iso8601Lexer.scanISO8601DateIfMatch()) {
              value = iso8601Lexer.getCalendar().getTime();
            }
            iso8601Lexer.close();
          }

          object.put(key, value);
        } else if (ch >= '0' && ch <= '9' || ch == '-') {
          lexer.scanNumber();
          if (lexer.token() == JSONToken.LITERAL_INT) {
            value = lexer.integerValue();
          } else {
            value = lexer.decimalValue(lexer.isEnabled(Feature.UseBigDecimal));
          }

          object.put(key, value);
        } else if (ch == '[') { // 减少嵌套,兼容android
          lexer.nextToken();
          JSONArray list = new JSONArray();
          this.parseArray(list, key);

          if (lexer.isEnabled(Feature.UseObjectArray)) {
            value = list.toArray();
          } else {
            value = list;
          }
          object.put(key, value);

          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            return object;
          } else if (lexer.token() == JSONToken.COMMA) {
            continue;
          } else {
            throw new JSONException("syntax error");
          }
        } else if (ch == '{') { // 减少嵌套,兼容android
          lexer.nextToken();

          final boolean parentIsArray = fieldName != null && fieldName.getClass() == Integer.class;

          JSONObject input = new JSONObject(lexer.isEnabled(Feature.OrderedField));
          ParseContext ctxLocal = null;

          if (!parentIsArray) {
            ctxLocal = setContext(context, input, key);
          }

          Object obj = null;
          boolean objParsed = false;
          if (fieldTypeResolver != null) {
            String resolveFieldName = key != null ? key.toString() : null;
            Type fieldType = fieldTypeResolver.resolve(object, resolveFieldName);
            if (fieldType != null) {
              ObjectDeserializer fieldDeser = config.getDeserializer(fieldType);
              obj = fieldDeser.deserialze(this, fieldType, key);
              objParsed = true;
            }
          }
          if (!objParsed) {
            obj = this.parseObject(input, key);
          }

          if (ctxLocal != null && input != obj) {
            ctxLocal.object = object;
          }

          checkMapResolve(object, key.toString());

          if (object.getClass() == JSONObject.class) {
            object.put(key.toString(), obj);
          } else {
            object.put(key, obj);
          }

          if (parentIsArray) {
            // setContext(context, obj, key);
            setContext(obj, key);
          }

          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();

            setContext(context);
            return object;
          } else if (lexer.token() == JSONToken.COMMA) {
            if (parentIsArray) {
              this.popContext();
            }
            continue;
          } else {
            throw new JSONException("syntax error, " + lexer.tokenName());
          }
        } else {
          lexer.nextToken();
          value = parse();

          if (object.getClass() == JSONObject.class) {
            key = key.toString();
          }
          object.put(key, value);

          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            return object;
          } else if (lexer.token() == JSONToken.COMMA) {
            continue;
          } else {
            throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
          }
        }

        lexer.skipWhitespace();
        ch = lexer.getCurrent();
        if (ch == ',') {
          lexer.next();
          continue;
        } else if (ch == '}') {
          lexer.next();
          lexer.resetStringPosition();
          lexer.nextToken();

          // this.setContext(object, fieldName);
          this.setContext(value, key);

          return object;
        } else {
          throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
        }
      }
    } finally {
      this.setContext(context);
    }
  }