Ejemplo n.º 1
0
 @SuppressWarnings("unchecked")
 public static final <T> T parseObject(
     String input, Type clazz, ParseProcess processor, Feature... features) {
   return (T)
       parseObject(
           input, clazz, ParserConfig.getGlobalInstance(), DEFAULT_PARSER_FEATURE, features);
 }
Ejemplo n.º 2
0
  public static final 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;
  }
Ejemplo n.º 3
0
 @SuppressWarnings("unchecked")
 public static final <T> T parseObject(String text, TypeReference<T> type, Feature... features) {
   return (T)
       parseObject(
           text,
           type.getType(),
           ParserConfig.getGlobalInstance(),
           DEFAULT_PARSER_FEATURE,
           features);
 }
Ejemplo n.º 4
0
  public static final 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;
  }
Ejemplo n.º 5
0
  public static final List<Object> parseArray(String text, Type[] types) {
    if (text == null) {
      return null;
    }

    List<Object> list;

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    Object[] objectArray = parser.parseArray(types);
    if (objectArray == null) {
      list = null;
    } else {
      list = Arrays.asList(objectArray);
    }

    parser.handleResovleTask(list);

    parser.close();

    return list;
  }
Ejemplo n.º 6
0
  @SuppressWarnings("unchecked")
  public static final <T> T parseObject(char[] input, int length, Type clazz, Feature... features) {
    if (input == null || input.length == 0) {
      return null;
    }

    int featureValues = DEFAULT_PARSER_FEATURE;
    for (Feature featrue : features) {
      featureValues = Feature.config(featureValues, featrue, true);
    }

    DefaultJSONParser parser =
        new DefaultJSONParser(input, length, ParserConfig.getGlobalInstance(), featureValues);
    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
  }
Ejemplo n.º 7
0
  @SuppressWarnings("unchecked")
  public static final <T> T parseObject(
      String input, Type clazz, int featureValues, Feature... features) {
    if (input == null) {
      return null;
    }

    for (Feature featrue : features) {
      featureValues = Feature.config(featureValues, featrue, true);
    }

    DefaultJSONParser parser =
        new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues);
    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
  }
Ejemplo n.º 8
0
  public static final <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;
  }
Ejemplo n.º 9
0
  public static final 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;
  }
Ejemplo n.º 10
0
 public static final <T> T toJavaObject(JSON json, Class<T> clazz) {
   return TypeUtils.cast(json, clazz, ParserConfig.getGlobalInstance());
 }
Ejemplo n.º 11
0
  @SuppressWarnings("unchecked")
  public static final Object toJSON(Object javaObject, ParserConfig mapping) {
    if (javaObject == null) {
      return null;
    }

    if (javaObject instanceof JSON) {
      return (JSON) javaObject;
    }

    if (javaObject instanceof Map) {
      Map<Object, Object> map = (Map<Object, Object>) javaObject;

      JSONObject json = new JSONObject(map.size());

      for (Map.Entry<Object, Object> entry : map.entrySet()) {
        Object key = entry.getKey();
        String jsonKey = TypeUtils.castToString(key);
        Object jsonValue = toJSON(entry.getValue());
        json.put(jsonKey, jsonValue);
      }

      return json;
    }

    if (javaObject instanceof Collection) {
      Collection<Object> collection = (Collection<Object>) javaObject;

      JSONArray array = new JSONArray(collection.size());

      for (Object item : collection) {
        Object jsonValue = toJSON(item);
        array.add(jsonValue);
      }

      return array;
    }

    Class<?> clazz = javaObject.getClass();

    if (clazz.isEnum()) {
      return ((Enum<?>) javaObject).name();
    }

    if (clazz.isArray()) {
      int len = Array.getLength(javaObject);

      JSONArray array = new JSONArray(len);

      for (int i = 0; i < len; ++i) {
        Object item = Array.get(javaObject, i);
        Object jsonValue = toJSON(item);
        array.add(jsonValue);
      }

      return array;
    }

    if (mapping.isPrimitive(clazz)) {
      return javaObject;
    }

    try {
      List<FieldInfo> getters = TypeUtils.computeGetters(clazz, null);

      JSONObject json = new JSONObject(getters.size());

      for (FieldInfo field : getters) {
        Object value = field.get(javaObject);
        Object jsonValue = toJSON(value);

        json.put(field.getName(), jsonValue);
      }

      return json;
    } catch (IllegalAccessException e) {
      throw new JSONException("toJSON error", e);
    } catch (InvocationTargetException e) {
      throw new JSONException("toJSON error", e);
    }
  }
Ejemplo n.º 12
0
 // ///////
 public static final Object toJSON(Object javaObject) {
   return toJSON(javaObject, ParserConfig.getGlobalInstance());
 }
Ejemplo n.º 13
0
 @SuppressWarnings("unchecked")
 public static final <T> T parseObject(String text, Class<T> clazz, Feature... features) {
   return (T)
       parseObject(
           text, (Type) clazz, ParserConfig.getGlobalInstance(), DEFAULT_PARSER_FEATURE, features);
 }