jacksonシーケンス化所与のClassマッピングと抽象クラスによるマッピング

1159 ワード



//   TypeReference 
//List<MyBean> result = mapper.readValue(src, new TypeReference<List<MyBean>>() { });


// TypeFactory 
//apper.readValue(src, TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, MyBean.class));



//   
 @JsonDeserialize(as=Impl.class)  


//  List  
For java.util.List values you would use @JsonDeserialize(contentAs=ValueTypeImpl.class) instead, and for java.util.Map keys, @JsonDeserialize(keyAs=KeyTypeImpl.class).


// SimpleModule  
		SimpleModule module = new SimpleModule();
		module.addSerializer(Date.class, new JsonSerializer<Date>() {
			@Override
			public void serialize(Date arg0, JsonGenerator jgen,
					SerializerProvider arg2) throws IOException,
					JsonProcessingException {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				jgen.writeString(format.format(arg0));
			}
		});
		mapper.registerModule(module);