Esempio n. 1
0
  public void parseExtra(Object object, String key) {
    final JSONLexer lexer = this.lexer; // xxx
    lexer.nextTokenWithColon();
    Type type = null;

    if (extraTypeProviders != null) {
      for (ExtraTypeProvider extraProvider : extraTypeProviders) {
        type = extraProvider.getExtraType(object, key);
      }
    }
    Object value =
        type == null //
            ? parse() // skip
            : parseObject(type);

    if (object instanceof ExtraProcessable) {
      ExtraProcessable extraProcessable = ((ExtraProcessable) object);
      extraProcessable.processExtra(key, value);
      return;
    }

    if (extraProcessors != null) {
      for (ExtraProcessor process : extraProcessors) {
        process.processExtra(object, key, value);
      }
    }
  }
Esempio n. 2
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");
  }
Esempio n. 3
0
  public void acceptType(String typeName) {
    JSONLexer lexer = this.lexer;

    lexer.nextTokenWithColon();

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

    if (typeName.equals(lexer.stringVal())) {
      lexer.nextToken();
      if (lexer.token() == JSONToken.COMMA) {
        lexer.nextToken();
      }
    } else {
      throw new JSONException("type not match error");
    }
  }
  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;
      }
    }
  }
Esempio n. 5
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;
  }
Esempio n. 6
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;
      }
    }
  }