2011-08-08 14 views
10

:私はジャクソンを使用してContainerクラスをシリアル化するときジャクソンJSONネストされたオブジェクトの属性をラップしていない私は、次のクラス持っ

public class Container { 
    private String name; 
    private Data data; 
} 

public class Data { 
    private Long id; 
} 

を私は

{"name":"Some name","data":{"id":1}} 

を取得しかし、私はあることを結果が必要になります。

{"name":"Some name","id":1} 

Container.getDataId()メソッドを追加することはできますか?もしそうなら、それを行う方法?


更新

私は、カスタムJsonSerializer<Data>を作成しようとしたが、

public class JsonDataSerializer extends JsonSerializer<Data> { 

    private static Logger logger = Logger.getLogger(JsonDataSerializer.class); 

    @Override 
    public void serialize(Data value, JsonGenerator jgen, 
      SerializerProvider provider) 
      throws IOException,JsonProcessingException { 

     Long id = (value.getId() == null) ? 0l : value.getId(); 
     jgen.writeStartObject(); 
     jgen.writeNumberField("id", id); 
     jgen.writeEndObject(); 
     logger.debug("Data id " + id + " serialized to JSON.");  
    } 
} 

前に、私もDataクラス上@JsonSerialize注釈を追加しようとしたとして、結果は、同じでしたContainerクラスの上のゲッター。前に私が何の成功もなく言及したように。シリアライザが使用され、ロガーはメッセージを記録します。


更新2

私は​​とwriteEndObject()を削除すると、その後何のJSONは、唯一HTTP Status 500エラーreturnesdされず、例外は私がデバッグ出力に見つけたものを除いてスローされません。

DEBUG: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [[email protected]]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.jackson.JsonGenerationException: Can not write a field name, expecting a value 
DEBUG: org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [[email protected]]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.jackson.JsonGenerationException: Can not write a field name, expecting a value 
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [[email protected]]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.jackson.JsonGenerationException: Can not write a field name, expecting a value 
+0

writeStartObject()とwriteEndObject()の行をスキップするとどうなりますか? –

+1

あなたはおそらくちょうど値を書くべきです:writeNumber(id) –

+0

はい、ちょうど 'writeNumber(id)'を使った書き込みは少なくともいくつかの場合に働きました。出力は '' data ':1'でした。しかし、もし私が 'Data'クラスで属性を2つ以上直列化したいのであれば?私は 'Write {Start、End} Object'の有無にかかわらず' write * Field'メソッドを追加しようとしました – kurochenko

答えて

9

ジャクソン1.9は、あなたが探しているものを行いJsonUnwrapped注釈を導入しています。

0

データ型のカスタムシリアライザを作成して登録する必要があると思います。

Dataクラスで@JsonSerializeアノテーションを使用できます。

は、シリアライザクラスを指定します。

@JsonSerialize(using = MyDataSerializer.class) 
public class Data { 
    ... 
} 

public static class MyDataSerializer extends JsonSerializer<Data> { 

    @Override 
    public void serialize(Data value, JsonGenerator jgen, 
      SerializerProvider provider) throws IOException,JsonProcessingException { 
     jgen.writeNumberField("id", value.id); 
    } 
+0

お返事ありがとう!私はあなたが書いたこと(更新された質問を参照)をしようとしましたが、残念ながら結果は以前と同じです。 – kurochenko

関連する問題