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() == 8) { lexer.nextToken(); list = null; } else { Collection list2 = new ArrayList(); parser.parseArray((Class) clazz, list2); parser.handleResovleTask(list2); } parser.close(); return list; }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected Object deserialze(DefaultJSONParser parser, Type type, Object fieldName, Map map) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (String.class == keyType) { return parseMap(parser, (Map<String, Object>) map, valueType, fieldName); } else { return parseMap(parser, map, keyType, valueType, fieldName); } } else { return parser.parseObject(map, fieldName); } }
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; }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { Object value = DateCodec.instance.deserialze(parser, type, fieldName); if (value instanceof Calendar) { return (T) value; } Date date = (Date) value; if (date == null) { return null; } JSONLexer lexer = parser.lexer; Calendar calendar = Calendar.getInstance(lexer.getTimeZone(), lexer.getLocale()); calendar.setTime(date); if (type == XMLGregorianCalendar.class) { return (T) createXMLGregorianCalendar((GregorianCalendar) calendar); } return (T) calendar; }
@Override public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) { if (fieldValueDeserilizer == null) { fieldValueDeserilizer = parser.getConfig().getDeserializer(fieldInfo); } if (objectType instanceof ParameterizedType) { ParseContext objContext = parser.getContext(); objContext.setType(objectType); } Object value = fieldValueDeserilizer.deserialze(parser, getFieldType(), fieldInfo.getName()); if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) { ResolveTask task = parser.getLastResolveTask(); task.setFieldDeserializer(this); task.setOwnerContext(parser.getContext()); parser.setResolveStatus(DefaultJSONParser.NONE); } else { if (object == null) { fieldValues.put(fieldInfo.getName(), value); } else { setValue(object, value); } } }
public void test_0() throws Exception { List<?> res = Arrays.asList(1, 2, 3); String[] tests = { "[1,2,3]", "[1,,2,3]", "[1,2,,,3]", "[1 2,,,3]", "[1 2 3]", "[1, 2, 3,,]", "[,,1, 2, 3,,]", }; for (String t : tests) { DefaultJSONParser ext = new DefaultJSONParser(t); ext.config(Feature.AllowArbitraryCommas, true); List<Object> extRes = ext.parseArray(Object.class); Assert.assertEquals(res, extRes); DefaultJSONParser basic = new DefaultJSONParser(t); basic.config(Feature.AllowArbitraryCommas, true); List<Object> basicRes = new ArrayList<Object>(); basic.parseArray(basicRes); Assert.assertEquals(res, basicRes); } }
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.lexer; int token = lexer.token(); if (token == JSONToken.NULL) { lexer.nextToken(); list = null; } else if (token == JSONToken.EOF && lexer.isBlankInput()) { list = null; } else { list = new ArrayList<T>(); parser.parseArray(clazz, list); parser.handleResovleTask(list); } parser.close(); return list; }
public void test_scanFloat() throws Exception { StringBuffer buf = new StringBuffer(); buf.append('['); for (int i = 0; i < 1024; ++i) { if (i != 0) { buf.append(','); } buf.append("{\"id\":" + i + ".0}"); } buf.append(']'); Reader reader = new StringReader(buf.toString()); JSONReaderScanner scanner = new JSONReaderScanner(reader); DefaultJSONParser parser = new DefaultJSONParser(scanner); List<VO> array = parser.parseArray(VO.class); for (int i = 0; i < array.size(); ++i) { Assert.assertTrue(Integer.toString(i), (double) i == array.get(i).getId()); } parser.close(); }
public void test_scanInt() throws Exception { StringBuffer buf = new StringBuffer(); buf.append('['); for (int i = 0; i < 1024; ++i) { if (i != 0) { buf.append(','); } buf.append(i + ".0"); } buf.append(']'); Reader reader = new StringReader(buf.toString()); JSONReaderScanner scanner = new JSONReaderScanner(reader); DefaultJSONParser parser = new DefaultJSONParser(scanner); JSONArray array = (JSONArray) parser.parse(); for (int i = 0; i < array.size(); ++i) { BigDecimal value = new BigDecimal(i + ".0"); Assert.assertEquals(value, array.get(i)); } }
@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; }
public static <T> T deserialze(DefaultJSONParser parser) { JSONLexer lexer = parser.getLexer(); if (lexer.token() == 2) { String val = lexer.numberString(); lexer.nextToken(16); return Float.valueOf(Float.parseFloat(val)); } else if (lexer.token() == 3) { float val2 = lexer.floatValue(); lexer.nextToken(16); return Float.valueOf(val2); } else { Object value = parser.parse(); if (value == null) { return null; } return TypeUtils.castToFloat(value); } }
public void test_1() throws Exception { String input = "{,,,\"value\":null,\"id\":123,,,,}"; int featureValues = 0; featureValues |= Feature.AllowArbitraryCommas.getMask(); featureValues |= Feature.IgnoreNotMatch.getMask(); DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues); Entity object = new Entity(); parser.parseObject(object); }
public void test_parse() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("1"); Assert.assertEquals(Integer.valueOf(1), parser.parse()); Exception error = null; try { parser.parse(); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public void test_date_2() throws Exception { int features = JSON.DEFAULT_PARSER_FEATURE; DefaultJSONParser parser = new DefaultJSONParser("new Date(1294552193254)", ParserConfig.getGlobalInstance(), features); java.sql.Date date = parser.parseObject(java.sql.Date.class); Assert.assertEquals(new java.sql.Date(1294552193254L), date); parser.close(); }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type).getGenericComponentType(); if (componentType instanceof TypeVariable) { TypeVariable<?> componentVar = (TypeVariable<?>) componentType; componentType = componentVar.getBounds()[0]; } List<Object> list = new ArrayList<Object>(); parser.parseArray(componentType, list); Class<?> componentClass; if (componentType instanceof Class) { componentClass = (Class<?>) componentType; Object[] array = (Object[]) Array.newInstance(componentClass, list.size()); list.toArray(array); return (T) array; } else { return (T) list.toArray(); } } if (type instanceof Class && type != Object.class && type != Serializable.class) { return (T) parser.parseObject(type); } return (T) parser.parse(fieldName); }
public void test_error_4() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("['1','2'}"); parser.config(Feature.AllowISO8601DateFormat, false); Exception error = null; try { parser.parseArray(new Type[] {}); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { String text = (String) parser.parse(); if (text != null) { text = text.replaceAll("元", ""); } return (T) Integer.valueOf(Integer.parseInt(text)); }
@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); } }
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { if (!(type instanceof GenericArrayType)) { return parser.parse(fieldName); } Type componentType = ((GenericArrayType) type).getGenericComponentType(); if (componentType instanceof TypeVariable) { componentType = ((TypeVariable) componentType).getBounds()[0]; } Object list = new ArrayList(); parser.parseArray(componentType, (Collection) list); if (!(componentType instanceof Class)) { return list.toArray(); } Class<?> componentClass = (Class) componentType; if (componentClass == Boolean.TYPE) { return TypeUtils.cast(list, boolean[].class, parser.getConfig()); } if (componentClass == Short.TYPE) { return TypeUtils.cast(list, short[].class, parser.getConfig()); } if (componentClass == Integer.TYPE) { return TypeUtils.cast(list, int[].class, parser.getConfig()); } if (componentClass == Long.TYPE) { return TypeUtils.cast(list, long[].class, parser.getConfig()); } if (componentClass == Float.TYPE) { return TypeUtils.cast(list, float[].class, parser.getConfig()); } if (componentClass == Double.TYPE) { return TypeUtils.cast(list, double[].class, parser.getConfig()); } Object[] array = (Object[]) Array.newInstance(componentClass, list.size()); list.toArray(array); return array; }
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { String uri = (String) parser.parse(); if (uri == null) { return null; } return URI.create(uri); }
@Override public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName){ if (type instanceof Class && JSONString.class.isAssignableFrom((Class<?>) type)) { String jsonString = parser.parseObject(String.class); return (T) new JSONString(jsonString); } return (T) parser.parse(fieldName); }
public boolean parseField(DefaultJSONParser parser, String key, Object object, Type objectType, Map<String, Object> fieldValues) { JSONLexer lexer = parser.getLexer(); // xxx FieldDeserializer fieldDeserializer = feildDeserializerMap.get(key); if (fieldDeserializer == null) { for (Map.Entry<String, FieldDeserializer> entry : feildDeserializerMap.entrySet()) { if (entry.getKey().equalsIgnoreCase(key)) { fieldDeserializer = entry.getValue(); break; } } } if (fieldDeserializer == null) { parseExtra(parser, object, key); return false; } lexer.nextTokenWithColon(fieldDeserializer.getFastMatchToken()); fieldDeserializer.parseField(parser, object, objectType, fieldValues); return true; }
public void test_error_0() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("['1','2'}"); parser.config(Feature.AllowISO8601DateFormat, false); Exception error = null; try { List<Object> list = new ArrayList<Object>(); parser.parseArray(Integer.class, list); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public void test_1() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{\"value\":3,\"id\":1}"); parser.config(Feature.AllowArbitraryCommas, false); Entity entity = new Entity(); parser.parseObject(entity); Assert.assertEquals(3, entity.getValue()); }
public void test_error_6() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{'a':3}"); parser.config(Feature.AllowSingleQuotes, false); parser.parseObject(A.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { String value = parser.parse(); if (value == null) { return null; } return new File(value); }
public void test_error2() { Exception error = null; try { DefaultJSONParser parser = new DefaultJSONParser("ttr"); parser.parse(); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
protected Object deserialze(DefaultJSONParser parser, Type type, Object fieldName, Map map) { if (!(type instanceof ParameterizedType)) { return parser.parseObject(map, fieldName); } ParameterizedType parameterizedType = (ParameterizedType) type; Class keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (String.class == keyType) { return parseMap(parser, map, valueType, fieldName); } return parseMap(parser, map, keyType, valueType, fieldName); }
protected <T> T cast(DefaultJSONParser parser, Type clazz, Object fieldName, Object val) { Calendar calendar = null; if (val == null) { return null; } if (val instanceof Date) { return val; } if (val instanceof Number) { return new Date(((Number) val).longValue()); } if (val instanceof String) { String strVal = (String) val; if (strVal.length() == 0) { return null; } JSONScanner dateLexer = new JSONScanner(strVal); try { if (dateLexer.scanISO8601DateIfMatch(false)) { calendar = dateLexer.getCalendar(); if (clazz == Calendar.class) { return calendar; } val = calendar.getTime(); dateLexer.close(); return val; } dateLexer.close(); try { return parser.getDateFormat().parse(strVal); } catch (ParseException e) { return new Date(Long.parseLong(strVal)); } } finally { dateLexer.close(); } } else { throw new JSONException("parse error"); } }
public void test_2() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("['1','2','3']"); parser.config(Feature.AllowArbitraryCommas, true); List list = new ArrayList(); parser.parseArray(String.class, list); Assert.assertEquals("[1, 2, 3]", list.toString()); Assert.assertEquals("1", list.get(0)); }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { try { return (T) deserialze(parser); } catch (Exception ex) { throw new JSONException("parseDecimal error, field : " + fieldName, ex); } }
public void test_error_6() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("['1' 1 '2'}"); parser.config(Feature.AllowISO8601DateFormat, false); Exception error = null; try { parser.parseArray(new Type[] {Integer.class}); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public void test_error_1() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("[{}]"); parser.parseArray(Class.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public void test_date_1() throws Exception { int features = JSON.DEFAULT_PARSER_FEATURE; features = Feature.config(features, Feature.AllowISO8601DateFormat, true); DefaultJSONParser parser = new DefaultJSONParser("\"2011-01-09T13:49:53.254\"", ParserConfig.getGlobalInstance(), features); java.util.Date date = parser.parseObject(java.util.Date.class); Assert.assertEquals(new java.util.Date(1294552193254L), date); parser.close(); }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { Object value = parser.parse(); return value == null // ? null // : (T) TypeUtils.castToChar(value); }
public void test_date_4() throws Exception { int features = JSON.DEFAULT_PARSER_FEATURE; features = Feature.config(features, Feature.AllowISO8601DateFormat, true); DefaultJSONParser parser = new DefaultJSONParser("\"2011-01-09\"", ParserConfig.getGlobalInstance(), features); java.util.Date date = parser.parseObject(java.util.Date.class); Assert.assertEquals(new java.util.Date(1294502400000L), date); parser.close(); }
public void test_error2() throws Exception { { Exception error = null; try { String text = "{}"; DefaultJSONParser parser = new DefaultJSONParser(text); parser.parseArray(User.class); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); } }
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONLexer lexer = parser.getLexer(); if (lexer.token() == 4) { String text = lexer.stringVal(); lexer.nextToken(); if (type == LocalDateTime.class) { return LocalDateTime.parse(text); } if (type == LocalDate.class) { return LocalDate.parse(text); } if (type == LocalTime.class) { return LocalTime.parse(text); } if (type == ZonedDateTime.class) { return ZonedDateTime.parse(text); } if (type == OffsetDateTime.class) { return OffsetDateTime.parse(text); } if (type == OffsetTime.class) { return OffsetTime.parse(text); } if (type == ZoneId.class) { return ZoneId.of(text); } if (type == Period.class) { return Period.parse(text); } if (type == Duration.class) { return Duration.parse(text); } if (type == Instant.class) { return Instant.parse(text); } return null; } throw new UnsupportedOperationException(); }
public void test() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{\"date\":\"2012/04-01\"}", ParserConfig.getGlobalInstance(), 0); parser.setDateFormat("yyyy/MM-dd"); VO vo = parser.parseObject(VO.class); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM-dd", JSON.defaultLocale); dateFormat.setTimeZone(JSON.defaultTimeZone); Assert.assertEquals(dateFormat.parse("2012/04-01"), vo.getDate()); parser.close(); }
public void test_date_3() throws Exception { int features = JSON.DEFAULT_PARSER_FEATURE; features = Feature.config(features, Feature.AllowISO8601DateFormat, true); DefaultJSONParser parser = new DefaultJSONParser("\"2011-01-09T13:49:53\"", ParserConfig.getGlobalInstance(), features); java.sql.Date date = parser.parseObject(java.sql.Date.class); Assert.assertEquals(new java.sql.Date(1294552193000L), date); parser.close(); }