Beispiel #1
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);
    }
  }
  @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");
  }
Beispiel #3
0
  public static Object parse(String text, int features) {
    if (text == null) {
      return null;
    }

    DefaultJSONParser parser =
        new DefaultJSONParser(text, ParserConfig.getGlobalInstance(), features);
    Object value = parser.parse();

    parser.handleResovleTask(value);

    parser.close();

    return value;
  }
Beispiel #4
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);
  }
Beispiel #5
0
  public static Object parse(
      byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) {
    charsetDecoder.reset();

    int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte());
    char[] chars = ThreadLocalCache.getChars(scaleLength);

    ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len);
    CharBuffer charBuf = CharBuffer.wrap(chars);
    IOUtils.decode(charsetDecoder, byteBuf, charBuf);

    int position = charBuf.position();

    DefaultJSONParser parser =
        new DefaultJSONParser(chars, position, ParserConfig.getGlobalInstance(), features);
    Object value = parser.parse();

    parser.handleResovleTask(value);

    parser.close();

    return value;
  }
Beispiel #6
0
  @SuppressWarnings("unchecked")
  public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
      String val = lexer.numberString();
      lexer.nextToken(JSONToken.COMMA);
      return (T) Float.valueOf(Float.parseFloat(val));
    }

    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
      float val = lexer.floatValue();
      lexer.nextToken(JSONToken.COMMA);
      return (T) Float.valueOf(val);
    }

    Object value = parser.parse();

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

    return (T) TypeUtils.castToFloat(value);
  }
  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;
      }
    }
  }
Beispiel #8
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;
      }
    }
  }
 public final Object decode(String text) {
   ParserConfig config = new ParserConfig();
   DefaultJSONParser parser = new DefaultJSONParser(text, config);
   parser.config(Feature.DisableCircularReferenceDetect, true);
   return parser.parse();
 }