2015-09-25 138 views
1

我遇到傑克遜Json映射器的問題,我無法弄清楚如何解決。休息傑森傑克遜映射器自定義對象映射器

我正在使用Spring MVC Rest應用程序,並且使用Jackson將端點轉換爲Json。

某些結果對象包含我想在轉換前篡改的類型。

更具體地說,結果對象可能看起來像這樣。

ResultObject 
    - getDoubleMap() : DoubleMap 
     - getDoubleEntries() : List<DoubleEntry> 
     - toMap() : Map<String, Double> 

我想要做的是沒有傑克遜轉換DoubleMap實例,但更願意重寫它像這樣

Object someJacksonMapInterceptor(Object object) { 
    if(object instanceof DoubleMap) { 
     return ((DoubleMap) object).toMap(); 
    } 
    return object; 
} 

我現在已經摺磨谷歌相當長一段時間,而不是一個簡單的解決方案。希望有人能提供建議。

非常感謝提前。

回答

0

在一個應用程序中,我們是自定義的deserealizing日期,可能您可以將它用於您的自定義deserealization。

public class VitalSign { 

    public static final String DATE_FORMAT1 = "yyyy-MM-dd'T'HH:mm:ssZ"; 
    public static final String DATE_FORMAT2 = "yyyy-MM-dd'T'HH:mm:ss"; 
    //public static final String DATE_FORMAT3 = "yyyy-MM-dd'T'HH:mm:ssTDZ"; 
    public static final String DATE_FORMAT4 = "MMM dd, yyyy h:mm:ss aa"; 


    @NotNull 
    @Column(name = "observed") 
    @Temporal(TemporalType.TIMESTAMP) 
    @DateTimeFormat(style = "M-") 
    @JsonDeserialize(using = CustomJsonDateDeserializer.class) 
    private Date timestamp; 



    public static class CustomJsonDateDeserializer extends JsonDeserializer<Date> { 


     public CustomJsonDateDeserializer() { 
      super();    
     } 

     @Override 
     public Date deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException, JsonProcessingException { 

      SimpleDateFormat[] formats = { new SimpleDateFormat(DATE_FORMAT1), new SimpleDateFormat(DATE_FORMAT2), new SimpleDateFormat(DATE_FORMAT4, Locale.US) }; 
      String date = jsonparser.getText(); 
      for (SimpleDateFormat format : formats) { 
       try { 
        return format.parse(date); 
       } catch (ParseException e) { 
       }    
      } 
      throw new RuntimeException("Unparseable date " + date); 

     } 

    } 
} 
0

序列化,你可以用@JsonValue註解你toMap()方法。對於反序列化,如果您有一個靜態工廠從Map<String, Double>創建DoubleMap,則可以使用@JsonCreator對其進行註釋。

private final ObjectMapper mapper = new ObjectMapper(); 

@Test 
public void serialize_doublemap() throws Exception { 
    DoubleMap map = new DoubleMap(); 
    map.put("red", 0.5); 
    map.put("orange", 0.7); 
    assertThat(mapper.writeValueAsString(map), equivalentTo("{ red: 0.5, orange: 0.7 }")); 
} 

@Test 
public void deserialize_doublemap() throws Exception { 
    assertThat(mapper.readValue("{ \"red\": 0.5, \"orange\": 0.7 }", DoubleMap.class).toMap(), 
      equalTo(ImmutableMap.of("red", 0.5, "orange", 0.7))); 
} 

public static class DoubleMap { 
    public List<DoubleEntry> entries = new ArrayList<>(); 

    public void put(String label, double value) { 
     entries.add(new DoubleEntry(label, value)); 
    } 

    @JsonCreator 
    public static DoubleMap fromJson(Map<String, Double> input) { 
     DoubleMap map = new DoubleMap(); 
     input.forEach(map::put); 
     return map; 
    } 

    public List<DoubleEntry> getDoubleEntries() { 
     return entries; 
    } 

    @JsonValue 
    public Map<String, Double> toMap() { 
     return entries.stream().collect(Collectors.toMap(e -> e.label, e -> e.value)); 
    } 
} 

public static final class DoubleEntry { 
    public final String label; 
    public final double value; 

    public DoubleEntry(String label, double value) { 
     this.label = label; 
     this.value = value; 
    } 
}