@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); } }
public static 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; }
public void test_0() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{\"id\":3, \"name\":\"xx\"}", ParserConfig.getGlobalInstance()); Entity entity = new Entity(); parser.parseObject(entity); }
public void test_error_4() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{\"a\"3}"); parser.parseObject(A.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
@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"); }
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 void test_error_0() throws Exception { Exception error = null; try { String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123]"; DefaultJSONParser parser = new DefaultJSONParser(text); parser.parseArray(User.class); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public void test_error_3() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{"); parser.config(Feature.AllowUnQuotedFieldNames, true); parser.parseObject(A.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public void test_getInput() { String text = "{,,}"; char[] chars = text.toCharArray(); DefaultJSONParser parser = new DefaultJSONParser(chars, chars.length, ParserConfig.getGlobalInstance(), 0); JSONException error = null; try { parser.parseObject(); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
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; }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { final JSONLexer lexer = parser.getLexer(); if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); return null; } Map<Object, Object> map = createMap(type); ParseContext context = parser.getContext(); try { parser.setContext(context, map, fieldName); return (T) deserialze(parser, type, fieldName, map); } finally { parser.setContext(context); } }
@SuppressWarnings("unchecked") public static <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 feature : features) { featureValues = Feature.config(featureValues, feature, true); } DefaultJSONParser parser = new DefaultJSONParser(input, length, ParserConfig.getGlobalInstance(), featureValues); T value = (T) parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return (T) value; }
@SuppressWarnings("unchecked") public static <T> T parseObject( String input, Type clazz, int featureValues, Feature... features) { if (input == null) { return null; } for (Feature feature : features) { featureValues = Feature.config(featureValues, feature, 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 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; }
@SuppressWarnings("unchecked") public static <T> T parseObject( String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) { if (input == null) { return null; } for (Feature feature : features) { featureValues = Feature.config(featureValues, feature, 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; }
public void test_1() throws Exception { JSONObject res = new JSONObject(); res.put("a", 1); res.put("b", 2); res.put("c", 3); String[] tests = { "{ 'a':1, 'b':2, 'c':3 }", "{ 'a':1,,'b':2, 'c':3 }", "{,'a':1, 'b':2, 'c':3 }", "{'a':1, 'b':2, 'c':3,,}", "{,,'a':1,,,,'b':2,'c':3,,,,,}", }; for (String t : tests) { DefaultJSONParser ext = new DefaultJSONParser(t); ext.config(Feature.AllowArbitraryCommas, true); JSONObject extRes = ext.parseObject(); Assert.assertEquals(res, extRes); DefaultJSONParser basic = new DefaultJSONParser(t); basic.config(Feature.AllowArbitraryCommas, true); JSONObject basicRes = basic.parseObject(); Assert.assertEquals(res, basicRes); } }
public void test_2() throws Exception { A res = new A(); res.setA(1); res.setB(2); res.setC(3); String[] tests = { "{ 'a':1, 'b':2, 'c':3 }", "{ 'a':1,,'b':2, 'c':3 }", "{,'a':1, 'b':2, 'c':3 }", "{'a':1, 'b':2, 'c':3,,}", "{,,'a':1,,,,'b':2,,'c':3,,,,,}", }; for (String t : tests) { DefaultJSONParser ext = new DefaultJSONParser(t); ext.config(Feature.AllowArbitraryCommas, true); A extRes = ext.parseObject(A.class); Assert.assertEquals(res, extRes); } }
@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); }
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; }
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.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({"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); } }
@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 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 Object deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONLexerBase lexer = (JSONLexerBase) parser.getLexer(); if (lexer.isEnabled(Feature.SortFeidFastMatch)) { return super.deserialze(parser, type, fieldName); } if (isSupportArrayToBean(lexer)) { // deserialzeArrayMapping } if (lexer.scanType("Department") == JSONLexerBase.NOT_MATCH) { return super.deserialze(parser, type, fieldName); } ParseContext mark_context = parser.getContext(); int matchedCount = 0; Department instance = new Department(); ParseContext context = parser.getContext(); ParseContext childContext = parser.setContext(context, instance, fieldName); if (lexer.matchStat == JSONLexerBase.END) { return instance; } int matchStat = 0; int _asm_flag_0 = 0; int id_gen = 0; com.alibaba.json.test.codegen.Employee leader_gen = null; java.util.List members_gen = null; String name_gen; if (lexer.isEnabled(Feature.InitStringFieldAsEmpty)) { name_gen = lexer.stringDefaultValue(); _asm_flag_0 |= 8; } else { name_gen = null; } boolean root_gen = false; com.alibaba.json.test.codegen.DepartmentType type_gen = null; boolean endFlag = false, restFlag = false; if ((!endFlag) && (!restFlag)) { id_gen = lexer.scanFieldInt(id_gen_prefix__); if (lexer.matchStat > 0) { _asm_flag_0 |= 1; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { if (lexer.matchField(leader_gen_prefix__)) { _asm_flag_0 |= 2; matchedCount++; if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) { ResolveTask resolveTask = parser.getLastResolveTask(); resolveTask.ownerContext = parser.getContext(); resolveTask.fieldDeserializer = this.getFieldDeserializer("leader"); parser.setResolveStatus(DefaultJSONParser.NONE); } } if (lexer.matchStat > 0) { _asm_flag_0 |= 2; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { if (lexer.matchField(members_gen_prefix__)) { _asm_flag_0 |= 4; if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); } else { if (lexer.token() == JSONToken.LBRACKET) { if (members_gen_list_item_deser__ == null) { members_gen_list_item_deser__ = parser.getConfig().getDeserializer(com.alibaba.json.test.codegen.Employee.class); } final int fastMatchToken = members_gen_list_item_deser__.getFastMatchToken(); lexer.nextToken(fastMatchToken); members_gen = new java.util.ArrayList(); ParseContext listContext = parser.getContext(); parser.setContext(members_gen, "members"); for (int i = 0; ; ++i) { if (lexer.token() == JSONToken.RBRACKET) { break; } com.alibaba.json.test.codegen.Employee itemValue = members_gen_list_item_deser__.deserialze(parser, members_gen_list_item_type__, i); members_gen.add(itemValue); parser.checkListResolve(members_gen); if (lexer.token() == JSONToken.COMMA) { lexer.nextToken(fastMatchToken); } } parser.setContext(listContext); if (lexer.token() != JSONToken.RBRACKET) { restFlag = true; } lexer.nextToken(JSONToken.COMMA); } else { restFlag = true; } } } if (lexer.matchStat > 0) { _asm_flag_0 |= 4; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { name_gen = lexer.scanFieldString(name_gen_prefix__); if (lexer.matchStat > 0) { _asm_flag_0 |= 8; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { root_gen = lexer.scanFieldBoolean(root_gen_prefix__); if (lexer.matchStat > 0) { _asm_flag_0 |= 16; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { String type_gen_enum_name = lexer.scanFieldSymbol(type_gen_prefix__, parser.getSymbolTable()); if (type_gen_enum_name == null) { type_gen = com.alibaba.json.test.codegen.DepartmentType.valueOf(type_gen_enum_name); } if (lexer.matchStat > 0) { _asm_flag_0 |= 32; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat != JSONLexerBase.END) { restFlag = true; } } if (restFlag) { return super.parseRest(parser, type, fieldName, instance); } return instance; }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { type = TypeUtils.unwrapOptional(type); Object value = parser.parseObject(type); return (T) Optional.of(value); }
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; }
@SuppressWarnings("rawtypes") public static Map parseMap( DefaultJSONParser parser, Map<String, Object> map, Type valueType, Object fieldName) { JSONLexer lexer = parser.getLexer(); if (lexer.token() != JSONToken.LBRACE) { throw new JSONException("syntax error, expect {, actual " + lexer.token()); } ParseContext context = parser.getContext(); try { for (; ; ) { lexer.skipWhitespace(); char ch = lexer.getCurrent(); if (parser.isEnabled(Feature.AllowArbitraryCommas)) { while (ch == ',') { lexer.next(); lexer.skipWhitespace(); ch = lexer.getCurrent(); } } String key; if (ch == '"') { key = lexer.scanSymbol(parser.getSymbolTable(), '"'); lexer.skipWhitespace(); ch = lexer.getCurrent(); if (ch != ':') { throw new JSONException("expect ':' at " + lexer.pos()); } } else if (ch == '}') { lexer.next(); lexer.resetStringPosition(); lexer.nextToken(JSONToken.COMMA); return map; } else if (ch == '\'') { if (!parser.isEnabled(Feature.AllowSingleQuotes)) { throw new JSONException("syntax error"); } key = lexer.scanSymbol(parser.getSymbolTable(), '\''); lexer.skipWhitespace(); ch = lexer.getCurrent(); if (ch != ':') { throw new JSONException("expect ':' at " + lexer.pos()); } } else { if (!parser.isEnabled(Feature.AllowUnQuotedFieldNames)) { throw new JSONException("syntax error"); } key = lexer.scanSymbolUnQuoted(parser.getSymbolTable()); lexer.skipWhitespace(); ch = lexer.getCurrent(); if (ch != ':') { throw new JSONException("expect ':' at " + lexer.pos() + ", actual " + ch); } } lexer.next(); lexer.skipWhitespace(); ch = lexer.getCurrent(); lexer.resetStringPosition(); if (key == JSON.DEFAULT_TYPE_KEY) { String typeName = lexer.scanSymbol(parser.getSymbolTable(), '"'); Class<?> clazz = TypeUtils.loadClass(typeName); if (clazz == map.getClass()) { lexer.nextToken(JSONToken.COMMA); if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); return map; } continue; } ObjectDeserializer deserializer = parser.getConfig().getDeserializer(clazz); lexer.nextToken(JSONToken.COMMA); parser.setResolveStatus(DefaultJSONParser.TypeNameRedirect); if (context != null && !(fieldName instanceof Integer)) { parser.popContext(); } return (Map) deserializer.deserialze(parser, clazz, fieldName); } Object value; lexer.nextToken(); if (lexer.token() == JSONToken.NULL) { value = null; lexer.nextToken(); } else { value = parser.parseObject(valueType); } map.put(key, value); parser.checkMapResolve(map, key); parser.setContext(context, value, key); final int tok = lexer.token(); if (tok == JSONToken.EOF || tok == JSONToken.RBRACKET) { return map; } if (tok == JSONToken.RBRACE) { lexer.nextToken(); return map; } } } finally { parser.setContext(context); } }
public void test_4() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{a:3}"); parser.config(Feature.AllowUnQuotedFieldNames, true); LinkedHashMap a = parser.parseObject(LinkedHashMap.class); Assert.assertEquals(3, a.get("a")); }
public void test_1() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{a:3}"); parser.config(Feature.AllowUnQuotedFieldNames, true); A a = parser.parseObject(A.class); Assert.assertEquals(3, a.getA()); }
public void test_0() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{'a':3}"); parser.config(Feature.AllowSingleQuotes, true); A a = parser.parseObject(A.class); Assert.assertEquals(3, a.getA()); }