Example #1
0
  public static JSONArray parseArray(String text) {
    if (text == null) {
      return null;
    }

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());

    JSONArray array;

    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken();
      array = null;
    } else if (lexer.token() == JSONToken.EOF) {
      array = null;
    } else {
      array = new JSONArray();
      parser.parseArray(array);

      parser.handleResovleTask(array);
    }

    parser.close();

    return array;
  }
Example #2
0
  @Override
  public void parseField(
      DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    Integer value;

    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_INT) {
      int val = lexer.intValue();
      lexer.nextToken(JSONToken.COMMA);
      if (object == null) {
        fieldValues.put(fieldInfo.getName(), val);
      } else {
        setValue(object, val);
      }
      return;
    } else if (lexer.token() == JSONToken.NULL) {
      value = null;
      lexer.nextToken(JSONToken.COMMA);
    } else {
      Object obj = parser.parse();

      value = TypeUtils.castToInt(obj);
    }

    if (value == null && getFieldClass() == int.class) {
      // skip
      return;
    }

    if (object == null) {
      fieldValues.put(fieldInfo.getName(), value);
    } else {
      setValue(object, value);
    }
  }
Example #3
0
  @SuppressWarnings("unchecked")
  public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    JSONLexer lexer = parser.getLexer();

    if (lexer.token() == JSONToken.COMMA) {
      lexer.nextToken(JSONToken.LITERAL_STRING);

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

      lexer.nextTokenWithColon(JSONToken.LITERAL_INT);

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

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

      return (T) new java.sql.Time(time);
    }

    Object val = parser.parse();

    if (val == null) {
      return null;
    }

    if (val instanceof java.sql.Time) {
      return (T) val;
    } else if (val instanceof Number) {
      return (T) new java.sql.Time(((Number) val).longValue());
    } else if (val instanceof String) {
      String strVal = (String) val;
      if (strVal.length() == 0) {
        return null;
      }

      long longVal;
      JSONScanner dateLexer = new JSONScanner(strVal);
      if (dateLexer.scanISO8601DateIfMatch()) {
        longVal = dateLexer.getCalendar().getTimeInMillis();
      } else {
        longVal = Long.parseLong(strVal);
      }
      return (T) new java.sql.Time(longVal);
    }

    throw new JSONException("parse error");
  }
Example #4
0
  @SuppressWarnings("unchecked")
  public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken(JSONToken.COMMA);
      return null;
    }

    Map<Object, Object> map = createMap(type);

    ParseContext context = parser.getContext();

    try {
      parser.setContext(context, map, fieldName);
      return (T) deserialze(parser, type, fieldName, map);
    } finally {
      parser.setContext(context);
    }
  }
Example #5
0
  @SuppressWarnings("unchecked")
  public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_INT) {
      long val = lexer.longValue();
      lexer.nextToken(JSONToken.COMMA);
      return (T) new BigDecimal(val);
    }

    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
      BigDecimal val = lexer.decimalValue();
      lexer.nextToken(JSONToken.COMMA);
      return (T) val;
    }

    Object value = parser.parse();

    if (value == null) {
      return null;
    }

    return (T) TypeUtils.castToBigDecimal(value);
  }
Example #6
0
  public static <T> List<T> parseArray(String text, Class<T> clazz) {
    if (text == null) {
      return null;
    }

    List<T> list;

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken();
      list = null;
    } else {
      list = new ArrayList<T>();
      parser.parseArray(clazz, list);

      parser.handleResovleTask(list);
    }

    parser.close();

    return list;
  }
Example #7
0
  @SuppressWarnings("rawtypes")
  public static Map parseMap(
      DefaultJSONParser parser, Map<String, Object> map, Type valueType, Object fieldName) {
    JSONLexer lexer = parser.getLexer();

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

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

        String key;
        if (ch == '"') {
          key = lexer.scanSymbol(parser.getSymbolTable(), '"');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos());
          }
        } else if (ch == '}') {
          lexer.next();
          lexer.resetStringPosition();
          lexer.nextToken(JSONToken.COMMA);
          return map;
        } else if (ch == '\'') {
          if (!parser.isEnabled(Feature.AllowSingleQuotes)) {
            throw new JSONException("syntax error");
          }

          key = lexer.scanSymbol(parser.getSymbolTable(), '\'');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos());
          }
        } else {
          if (!parser.isEnabled(Feature.AllowUnQuotedFieldNames)) {
            throw new JSONException("syntax error");
          }

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

        lexer.next();
        lexer.skipWhitespace();
        ch = lexer.getCurrent();

        lexer.resetStringPosition();

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

          if (clazz == map.getClass()) {
            lexer.nextToken(JSONToken.COMMA);
            if (lexer.token() == JSONToken.RBRACE) {
              lexer.nextToken(JSONToken.COMMA);
              return map;
            }
            continue;
          }

          ObjectDeserializer deserializer = parser.getConfig().getDeserializer(clazz);

          lexer.nextToken(JSONToken.COMMA);

          parser.setResolveStatus(DefaultJSONParser.TypeNameRedirect);

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

          return (Map) deserializer.deserialze(parser, clazz, fieldName);
        }

        Object value;
        lexer.nextToken();

        if (lexer.token() == JSONToken.NULL) {
          value = null;
          lexer.nextToken();
        } else {
          value = parser.parseObject(valueType);
        }

        map.put(key, value);
        parser.checkMapResolve(map, key);

        parser.setContext(context, value, key);

        final int tok = lexer.token();
        if (tok == JSONToken.EOF || tok == JSONToken.RBRACKET) {
          return map;
        }

        if (tok == JSONToken.RBRACE) {
          lexer.nextToken();
          return map;
        }
      }
    } finally {
      parser.setContext(context);
    }
  }
Example #8
0
  public static Object parseMap(
      DefaultJSONParser parser,
      Map<Object, Object> map,
      Type keyType,
      Type valueType,
      Object fieldName) {
    JSONLexer lexer = parser.getLexer();

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

    ObjectDeserializer keyDeserializer = parser.getConfig().getDeserializer(keyType);
    ObjectDeserializer valueDeserializer = parser.getConfig().getDeserializer(valueType);
    lexer.nextToken(keyDeserializer.getFastMatchToken());

    ParseContext context = parser.getContext();
    try {
      for (; ; ) {
        if (lexer.token() == JSONToken.RBRACE) {
          lexer.nextToken(JSONToken.COMMA);
          break;
        }

        if (lexer.token() == JSONToken.LITERAL_STRING && lexer.isRef()) {
          Object object = null;

          lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
          if (lexer.token() == JSONToken.LITERAL_STRING) {
            String ref = lexer.stringVal();
            if ("..".equals(ref)) {
              ParseContext parentContext = context.getParentContext();
              object = parentContext.getObject();
            } else if ("$".equals(ref)) {
              ParseContext rootContext = context;
              while (rootContext.getParentContext() != null) {
                rootContext = rootContext.getParentContext();
              }

              object = rootContext.getObject();
            } else {
              parser.addResolveTask(new ResolveTask(context, ref));
              parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
            }
          } else {
            throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
          }

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

          // parser.setContext(context, map, fieldName);
          // parser.setContext(context);

          return object;
        }

        if (map.size() == 0 //
            && lexer.token() == JSONToken.LITERAL_STRING //
            && JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal())) {
          lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
          lexer.nextToken(JSONToken.COMMA);
          if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            return map;
          }
          lexer.nextToken(keyDeserializer.getFastMatchToken());
        }

        Object key = keyDeserializer.deserialze(parser, keyType, null);

        if (lexer.token() != JSONToken.COLON) {
          throw new JSONException("syntax error, expect :, actual " + lexer.token());
        }

        lexer.nextToken(valueDeserializer.getFastMatchToken());

        Object value = valueDeserializer.deserialze(parser, valueType, key);

        map.put(key, value);

        if (lexer.token() == JSONToken.COMMA) {
          lexer.nextToken(keyDeserializer.getFastMatchToken());
        }
      }
    } finally {
      parser.setContext(context);
    }

    return map;
  }
Example #9
0
  public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexerBase lexer = (JSONLexerBase) parser.getLexer();

    if (lexer.isEnabled(Feature.SortFeidFastMatch)) {
      return super.deserialze(parser, type, fieldName);
    }

    if (isSupportArrayToBean(lexer)) {
      // deserialzeArrayMapping
    }

    if (lexer.scanType("Department") == JSONLexerBase.NOT_MATCH) {
      return super.deserialze(parser, type, fieldName);
    }

    ParseContext mark_context = parser.getContext();
    int matchedCount = 0;
    Department instance = new Department();

    ParseContext context = parser.getContext();
    ParseContext childContext = parser.setContext(context, instance, fieldName);

    if (lexer.matchStat == JSONLexerBase.END) {
      return instance;
    }

    int matchStat = 0;
    int _asm_flag_0 = 0;
    int id_gen = 0;
    com.alibaba.json.test.codegen.Employee leader_gen = null;
    java.util.List members_gen = null;
    String name_gen;
    if (lexer.isEnabled(Feature.InitStringFieldAsEmpty)) {
      name_gen = lexer.stringDefaultValue();
      _asm_flag_0 |= 8;
    } else {
      name_gen = null;
    }
    boolean root_gen = false;
    com.alibaba.json.test.codegen.DepartmentType type_gen = null;
    boolean endFlag = false, restFlag = false;

    if ((!endFlag) && (!restFlag)) {
      id_gen = lexer.scanFieldInt(id_gen_prefix__);
      if (lexer.matchStat > 0) {
        _asm_flag_0 |= 1;
        matchedCount++;
      }
      if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
        restFlag = true;
      }
      if (lexer.matchStat == JSONLexerBase.END) {
        endFlag = true;
      }
    }
    if ((!endFlag) && (!restFlag)) {
      if (lexer.matchField(leader_gen_prefix__)) {
        _asm_flag_0 |= 2;
        matchedCount++;
        if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) {
          ResolveTask resolveTask = parser.getLastResolveTask();
          resolveTask.ownerContext = parser.getContext();
          resolveTask.fieldDeserializer = this.getFieldDeserializer("leader");
          parser.setResolveStatus(DefaultJSONParser.NONE);
        }
      }
      if (lexer.matchStat > 0) {
        _asm_flag_0 |= 2;
        matchedCount++;
      }
      if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
        restFlag = true;
      }
      if (lexer.matchStat == JSONLexerBase.END) {
        endFlag = true;
      }
    }
    if ((!endFlag) && (!restFlag)) {
      if (lexer.matchField(members_gen_prefix__)) {
        _asm_flag_0 |= 4;
        if (lexer.token() == JSONToken.NULL) {
          lexer.nextToken(JSONToken.COMMA);
        } else {
          if (lexer.token() == JSONToken.LBRACKET) {
            if (members_gen_list_item_deser__ == null) {
              members_gen_list_item_deser__ =
                  parser.getConfig().getDeserializer(com.alibaba.json.test.codegen.Employee.class);
            }
            final int fastMatchToken = members_gen_list_item_deser__.getFastMatchToken();
            lexer.nextToken(fastMatchToken);
            members_gen = new java.util.ArrayList();
            ParseContext listContext = parser.getContext();
            parser.setContext(members_gen, "members");

            for (int i = 0; ; ++i) {
              if (lexer.token() == JSONToken.RBRACKET) {
                break;
              }
              com.alibaba.json.test.codegen.Employee itemValue =
                  members_gen_list_item_deser__.deserialze(parser, members_gen_list_item_type__, i);
              members_gen.add(itemValue);
              parser.checkListResolve(members_gen);
              if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(fastMatchToken);
              }
            }
            parser.setContext(listContext);
            if (lexer.token() != JSONToken.RBRACKET) {
              restFlag = true;
            }
            lexer.nextToken(JSONToken.COMMA);

          } else {
            restFlag = true;
          }
        }
      }
      if (lexer.matchStat > 0) {
        _asm_flag_0 |= 4;
        matchedCount++;
      }
      if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
        restFlag = true;
      }
      if (lexer.matchStat == JSONLexerBase.END) {
        endFlag = true;
      }
    }
    if ((!endFlag) && (!restFlag)) {
      name_gen = lexer.scanFieldString(name_gen_prefix__);
      if (lexer.matchStat > 0) {
        _asm_flag_0 |= 8;
        matchedCount++;
      }
      if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
        restFlag = true;
      }
      if (lexer.matchStat == JSONLexerBase.END) {
        endFlag = true;
      }
    }
    if ((!endFlag) && (!restFlag)) {
      root_gen = lexer.scanFieldBoolean(root_gen_prefix__);
      if (lexer.matchStat > 0) {
        _asm_flag_0 |= 16;
        matchedCount++;
      }
      if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
        restFlag = true;
      }
      if (lexer.matchStat == JSONLexerBase.END) {
        endFlag = true;
      }
    }
    if ((!endFlag) && (!restFlag)) {
      String type_gen_enum_name = lexer.scanFieldSymbol(type_gen_prefix__, parser.getSymbolTable());
      if (type_gen_enum_name == null) {
        type_gen = com.alibaba.json.test.codegen.DepartmentType.valueOf(type_gen_enum_name);
      }
      if (lexer.matchStat > 0) {
        _asm_flag_0 |= 32;
        matchedCount++;
      }
      if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
        restFlag = true;
      }
      if (lexer.matchStat != JSONLexerBase.END) {
        restFlag = true;
      }
    }

    if (restFlag) {
      return super.parseRest(parser, type, fieldName, instance);
    }

    return instance;
  }