2014-09-24 39 views
0

我們正試圖評估傑克遜作爲Gson的替代品,並且我遇到了解泛型的問題。我覺得它是一個簡單的問題,我完全錯過了答案,並沒有通過谷歌或文檔找到examples/info。我希望有人能指出我的錯誤,或指出我的正確實施。從Gson移植到傑克遜,混淆了仿製藥和定製解串器

說我有一類這樣的:

public class Response<T extends BaseTypeInterface > implements BaseTypeInterface { 
    private Meta meta; 
    private T mResult; 
    private Group<Notification> notifications; 
} 

我希望做一個自定義序列化它,但沒有我這樣做讓我在解串器的T類型信息。 理論上我想做SimpleModule.addDeserializer(Response.class, new ResponseV2Serializer<BaseTypeInterface>()); 然後在解串器中,我想讀取從ObjectMapper.readValue(string, TypeReference<Response<SomeClassExtBaseTypeInterface>)傳來的類型信息。

使用我當前的自定義解串器,我實現了ContextualDeserializer,希望從BeanProperty中提取類型信息,但每次其調用BeanProperty爲空時。

public static class ResponseSerializer<T extends BaseTypeType> 
     extends JsonDeserializer<Response<T>> 
     implements ContextualDeserializer { 

    @Override 
    public ResponseV2<T> deserialize(JsonParser jp, DeserializationContext ctxt) 
     throws IOException, JsonProcessingException { 
     return null; 
    } 
    @Override 
    public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) 
     throws JsonMappingException { 
     return this; 
    } 
} 

樣品的Json是這樣的:

{ 
    meta: { 
     code: 200, 
     requestId: "541fb2dc498e306d526f7e4c" 
    }, 
    response: { 
     //content 
    } 
} 

回答

0

讀你的問題和需求,我undestood你whant有幾個datarepresentations和改變它。例如,響應可以根據請求有時候列出舊列表並且重新計算值。如果這是你想做的事,我認爲你可以直接去解決這樣做:

  • 響應1:格式化爲你想
  • 響應2:正呈現動物名單

我們用遺產來解決您的問題。 該解決方案的優點是您不需要編寫序列化/解串器。儘量避免這樣做:它的錯誤和容易出錯...並且往往會導致緩慢/複雜的序列化。 :)

package com.rizze.labs; 

import static org.junit.Assert.assertTrue; 

import java.io.IOException; 
import java.util.ArrayList; 

import org.junit.Test; 

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 
import com.fasterxml.jackson.databind.ObjectMapper; 

    /** 
    * 
    * @author [email protected] 
    * 
    */ 

    public class SOFW26026792 { 


     @Test 
    public void fromJsonToJackson() throws IOException{ 

     TheBean bean = new TheBean(); 
     ObjectMapper mapper = new ObjectMapper(); 

     //toJson 
     String beanJson=mapper.writeValueAsString(bean); 
     System.out.println("Response1 "+beanJson); 

     //fromJson 
     TheBean beanCopy = mapper.readValue(beanJson, TheBean.class); 
     assertTrue(bean.meta.requestId.equals(beanCopy.meta.requestId)); 
     System.out.println("Response1 is OK"); 
    } 


    @Test 
    public void response2() throws IOException{ 

     TheBean2 animals = new TheBean2(); 
     animals.fillDatas(); 

     ObjectMapper mapper = new ObjectMapper(); 

     //toJson 
     String animalsJson=mapper.writeValueAsString(animals); 
     System.out.println("Response2 (Animals Response) "+animalsJson); 

     //fromJson 
     TheBean2 animalsCopy = mapper.readValue(animalsJson, TheBean2.class); 
     assertTrue(animals.meta.requestId.equals(animalsCopy.meta.requestId)); 
     System.out.println("Response2 (Animals Response) is OK"); 
    } 



    public static class TheBean{ 
     public RRMeta meta ;   
     public Object response; 

     public TheBean(){ 
      meta = new RRMeta(); 
      response = new RResponse(); 
      meta.code=200; 
      meta.requestId="id:12312342325252345234234"; 
     } 

    } 


    public static class RRMeta{ 
     public int code; 
     public String requestId; 
     public RRMeta(){ 

     } 
    } 

    public static class TheBean2 extends TheBean{ 

     public TheBean2(){ 
      meta = new RRMeta(); 
      response = new RResponseAnimals(); 
     } 
     public void fillDatas(){ 

      meta.code=200; 
      meta.requestId="id:animal:4"; 

      ((RResponseAnimals)response).fillDatas(); 
     } 
    } 

    public static class RResponse{ 

     public String val1; 
     public String val2; 

     public RResponse(){ 
      val1="example"; 
      val2="yummi"; 

     } 
    } 



    public static class RResponseAnimals { 

     public ArrayList<String> animals= new ArrayList<String>(); 

     public RResponseAnimals addAnimal(String animal){ 
      if(animals == null)animals=new ArrayList<String>(); 
      animals.add(animal); 
      return this; 
     } 

     public RResponseAnimals(){} 

     public void fillDatas(){ 
      addAnimal("snow monkey") 
      .addAnimal("royal ant") 
      .addAnimal("pink panther") 
      .addAnimal("Blue elephant"); 
     } 
    } 



} 

下面是這個junits的執行控制檯輸出:

Response1 {"meta":{"code":200,"requestId":"id:12312342325252345234234"},"response":{"val1":"example","val2":"yummi"}} 
    Response1 is OK 
    Response2 (Animals Response) {"meta":{"code":200,"requestId":"id:animal:4"},"response":{"animals":["snow monkey","royal ant","pink panther","Blue elephant"]}} 
    Response2 (Animals Response) is OK 

要點完整代碼: https://gist.github.com/jeorfevre/c0b4674a6a6bcd82630f