Example #1
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;
  }
Example #2
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;
  }
Example #3
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;
  }
Example #4
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;
  }
Example #5
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;
  }
Example #6
0
  @SuppressWarnings("unchecked")
  public static final <T> T parseObject(
      String input,
      Type clazz,
      ParserConfig config,
      ParseProcess processor,
      int featureValues,
      Feature... features) {
    if (input == null) {
      return null;
    }

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

    DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);

    if (processor instanceof ExtraTypeProvider) {
      parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
    }

    if (processor instanceof ExtraProcessor) {
      parser.getExtraProcessors().add((ExtraProcessor) processor);
    }

    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
  }
Example #7
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;
  }
Example #8
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;
  }
Example #9
0
  @SuppressWarnings("unchecked")
  public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {

    String url = (String) parser.parse();

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

    try {
      return (T) new URL(url);
    } catch (MalformedURLException e) {
      throw new JSONException("create url error", e);
    }
  }
Example #10
0
  @SuppressWarnings("unchecked")
  public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.getLexer();

    Boolean boolObj;
    if (lexer.token() == JSONToken.TRUE) {
      lexer.nextToken(JSONToken.COMMA);
      boolObj = Boolean.TRUE;
    } else if (lexer.token() == JSONToken.FALSE) {
      lexer.nextToken(JSONToken.COMMA);
      boolObj = Boolean.FALSE;
    } else if (lexer.token() == JSONToken.LITERAL_INT) {
      int intValue = lexer.intValue();
      lexer.nextToken(JSONToken.COMMA);

      if (intValue == 1) {
        boolObj = Boolean.TRUE;
      } else {
        boolObj = Boolean.FALSE;
      }
    } else {
      Object value = parser.parse();

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

      boolObj = TypeUtils.castToBoolean(value);
    }

    if (clazz == AtomicBoolean.class) {
      return (T) new AtomicBoolean(boolObj.booleanValue());
    }

    return (T) boolObj;
  }