2011-09-21 5 views
4

내가 한 달마 티아 클래스 "동물"에 잭슨 주석을 사용하여 자바 객체로 JSON 개체를 역 직렬화하려고 여기잭슨 직렬화 예기치 않은 토큰 (END_OBJECT),

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") 
@JsonSubTypes({@Type(value = Dog.class, name = "chien"), 
@Type(value = Cat.class, name= "chat")}) 

및 샘플 JSON 문자열입니다 :

{ 
    "name": "Chihuahua", 
    "type": { 
       "code": "chien", 
       "description": "Chien mechant" 
      } 
} 

문제는 JSON 개체의 "type"속성도 개체라는 것입니다. 내가 deserialize하려고하면 예외가 :

Caused by: org.codehaus.jackson.map.JsonMappingException: Could not resolve type id '{' into a subtype of [simple type, class Animal] 

"속성"값으로 아무것도 사용하려고 시도했지만 아무 것도. Exeption is this

Caused by: org.codehaus.jackson.map.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type.code' that is to contain type id (for class Animal) 

잘못된 것이 있습니다. 고맙습니다.

답변

-3

나는 잭슨과 초보자 해요하지만이 문제에 대한 해결책을 발견 한 후이이 밖으로 던지는 here

+0

관련 없음 ... – FazoM

+0

원하는 경우 :). – FazoM

0

설명처럼 당신이 트리의 구문 분석을 검색해야 생각합니다. 나는 이것에 비틀 거리는 사람들에게 흥미가 있다면 내 자신의 스타일을 생각해 냈습니다. 다른 방법을 찾으면 자신의 솔루션을 추가하십시오.

이 열거 형에서이 문제를 해결하기 위해 구현 한 방법은 열거 형 키 값의 문자열 표현을 검색 할 수있는 findByType 메서드를 추가하는 것입니다. 그래서 예를 들어 당신은

pubilc enum MyEnum { 
... 
CHIEN("chien", "Chien mechant") 
... 
} 
// Map used to hold mappings between the event key and description 
private static final Map<String, String> MY_MAP = new HashMap<String, String>(); 

// Statically fills the #MY_MAP. 
static { 
    for (final MyEnum myEnum: MyEnum.values()) { 
     MY_MAP.put(myEnum.getKey(), myEnum); 
    } 
} 

을 같은 키/값 쌍을 열거를 다음 당신은 검색 키의 유형을 반환 공공 방법 findByTypeCode 것 :

public static MyEnum findByKey(String pKey) { 
    final MyEnum match = MY_MAP.get(pKey); 
    if (match == null) { 
     throw new SomeNotFoundException("No match found for the given key: " + pKey); 
    } 
    return match; 
} 

도움이 되었기를 바랍니다. 내가 말했듯이,이 문제를 직접 해결할 수있는 해결책이 있을지 모르겠지만 해결책을 찾지는 못했지만 해결책이 충분할 때 더 많은 시간을 낭비 할 필요가 없습니다.

+0

이것을 추가하면 @JsonIgnoreProperties (ignoreUnknown = true)가 작동합니다. –

관련 문제