Ejemplo n.º 1
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  public final void parseArray(final Collection array, Object fieldName) {
    final JSONLexer lexer = getLexer();

    if (lexer.token() == JSONToken.SET || lexer.token() == JSONToken.TREE_SET) {
      lexer.nextToken();
    }

    if (lexer.token() != JSONToken.LBRACKET) {
      throw new JSONException(
          "syntax error, expect [, actual "
              + JSONToken.name(lexer.token())
              + ", pos "
              + lexer.pos());
    }

    lexer.nextToken(JSONToken.LITERAL_STRING);

    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;
          }
        }

        Object value;
        switch (lexer.token()) {
          case LITERAL_INT:
            value = lexer.integerValue();
            lexer.nextToken(JSONToken.COMMA);
            break;
          case LITERAL_FLOAT:
            if (lexer.isEnabled(Feature.UseBigDecimal)) {
              value = lexer.decimalValue(true);
            } else {
              value = lexer.decimalValue(false);
            }
            lexer.nextToken(JSONToken.COMMA);
            break;
          case LITERAL_STRING:
            String stringLiteral = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);

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

            break;
          case TRUE:
            value = Boolean.TRUE;
            lexer.nextToken(JSONToken.COMMA);
            break;
          case FALSE:
            value = Boolean.FALSE;
            lexer.nextToken(JSONToken.COMMA);
            break;
          case LBRACE:
            JSONObject object = new JSONObject();
            value = parseObject(object, i);
            break;
          case LBRACKET:
            Collection items = new JSONArray();
            parseArray(items, i);
            value = items;
            break;
          case NULL:
            value = null;
            lexer.nextToken(JSONToken.LITERAL_STRING);
            break;
          case RBRACKET:
            lexer.nextToken(JSONToken.COMMA);
            return;
          case EOF:
            throw new JSONException("unclosed jsonArray");
          default:
            value = parse();
            break;
        }

        array.add(value);
        checkListResolve(array);

        if (lexer.token() == JSONToken.COMMA) {
          lexer.nextToken(JSONToken.LITERAL_STRING);
          continue;
        }
      }
    } finally {
      this.setContext(context);
    }
  }
Ejemplo n.º 2
0
  public Object parse(Object fieldName) {
    final JSONLexer lexer = getLexer();
    switch (lexer.token()) {
      case SET:
        lexer.nextToken();
        HashSet<Object> set = new HashSet<Object>();
        parseArray(set, fieldName);
        return set;
      case TREE_SET:
        lexer.nextToken();
        TreeSet<Object> treeSet = new TreeSet<Object>();
        parseArray(treeSet, fieldName);
        return treeSet;
      case LBRACKET:
        JSONArray array = new JSONArray();
        parseArray(array, fieldName);
        return array;
      case LBRACE:
        JSONObject object = new JSONObject();
        return parseObject(object, fieldName);
      case LITERAL_INT:
        Number intValue = lexer.integerValue();
        lexer.nextToken();
        return intValue;
      case LITERAL_FLOAT:
        Object value = lexer.decimalValue(isEnabled(Feature.UseBigDecimal));
        lexer.nextToken();
        return value;
      case LITERAL_STRING:
        String stringLiteral = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);

        if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
          JSONScanner iso8601Lexer = new JSONScanner(stringLiteral);
          try {
            if (iso8601Lexer.scanISO8601DateIfMatch()) {
              return iso8601Lexer.getCalendar().getTime();
            }
          } finally {
            iso8601Lexer.close();
          }
        }

        return stringLiteral;
      case NULL:
        lexer.nextToken();
        return null;
      case TRUE:
        lexer.nextToken();
        return Boolean.TRUE;
      case FALSE:
        lexer.nextToken();
        return Boolean.FALSE;
      case NEW:
        lexer.nextToken(JSONToken.IDENTIFIER);

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

        accept(JSONToken.LPAREN);
        long time = ((Number) lexer.integerValue()).longValue();
        accept(JSONToken.LITERAL_INT);

        accept(JSONToken.RPAREN);

        return new Date(time);
      case EOF:
        if (lexer.isBlankInput()) {
          return null;
        }
        throw new JSONException("unterminated json string, pos " + lexer.getBufferPosition());
      case ERROR:
      default:
        throw new JSONException("syntax error, pos " + lexer.getBufferPosition());
    }
  }
Ejemplo n.º 3
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);
    }
  }
Ejemplo n.º 4
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);
    }
  }