ObjectMapper objectMapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addDeserializer(CellPhone.class, new CellPhoneDeserializer()); objectMapper.registerModule(simpleModule);
final Long id = jsonNode.get("id").asLong(); final String name = jsonNode.get("name").asText(); final CellPhone cellPhone = objectMapper.convertValue(jsonNode.get("cellPhone"), CellPhone.class); final Set<FamilyMember> children = objectMapper.convertValue(jsonNode.get("children"), new TypeReference<LinkedHashSet<FamilyMember>>() { });
publicFamilyMemberDeserializer(){ this.objectMapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addDeserializer(CellPhone.class, new CellPhoneDeserializer()); this.objectMapper.registerModule(simpleModule); }
@Override public FamilyMember deserialize(JsonParser p, DeserializationContext ctxt)throws IOException, JsonProcessingException {
final Long id = jsonNode.get("id").asLong(); final String name = jsonNode.get("name").asText(); final CellPhone cellPhone = objectMapper.convertValue(jsonNode.get("cellPhone"), CellPhone.class); final Set<FamilyMember> children = objectMapper.convertValue(jsonNode.get("children"), new TypeReference<LinkedHashSet<FamilyMember>>() { });
ObjectMapper를 deserialize() 안에서 반복해서 생성하면 성능에 악영향을 미치므로 FamilyMember 생성 시 한 번만 생성해서 재사용하는 것이 좋다. customDeserializer에 필요한 SimpleModule과 CustomDeserializer도 마찬가지다.