public void test_string_null() throws Exception { String input = "null"; DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); StringCodec deser = new StringCodec(); Assert.assertNull(deser.deserialze(parser, null, null)); }
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.out; if (object == null) { out.writeNull(); return; } StringCodec.instance.write(serializer, ((EnumType) object).name().toLowerCase(), fieldName, fieldType, features); }
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { String uncasedSensitive = StringCodec.instance.deserialze(parser, type, fieldName); return (T) EnumType.valueOf(uncasedSensitive.toUpperCase()); }
public void parseArray(Type type, Collection array, Object fieldName) { if (this.lexer.token() == 21 || this.lexer.token() == 22) { this.lexer.nextToken(); } if (this.lexer.token() != 14) { throw new JSONException("exepct '[', but " + JSONToken.name(this.lexer.token())); } ObjectDeserializer deserializer; if (Integer.TYPE == type) { deserializer = IntegerCodec.instance; this.lexer.nextToken(2); } else if (String.class == type) { deserializer = StringCodec.instance; this.lexer.nextToken(4); } else { deserializer = this.config.getDeserializer(type); this.lexer.nextToken(deserializer.getFastMatchToken()); } ParseContext context = getContext(); setContext(array, fieldName); int i = 0; while (true) { if (isEnabled(Feature.AllowArbitraryCommas)) { while (this.lexer.token() == 16) { this.lexer.nextToken(); } } try { if (this.lexer.token() == 15) { break; } if (Integer.TYPE == type) { array.add(IntegerCodec.instance.deserialze(this, null, null)); } else if (String.class == type) { String value; if (this.lexer.token() == 4) { value = this.lexer.stringVal(); this.lexer.nextToken(16); } else { Object obj = parse(); if (obj == null) { value = null; } else { value = obj.toString(); } } array.add(value); } else { Object obj2; if (this.lexer.token() == 8) { this.lexer.nextToken(); obj2 = null; } else { obj2 = deserializer.deserialze(this, type, Integer.valueOf(i)); } array.add(obj2); checkListResolve(array); } if (this.lexer.token() == 16) { this.lexer.nextToken(deserializer.getFastMatchToken()); } i++; } finally { setContext(context); } } this.lexer.nextToken(16); }
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()); } 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 { Class<?> fieldClass = fieldDeser.getFieldClass(); Type fieldType = fieldDeser.getFieldType(); 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; } } }