2015-05-05 204 views
0

我試圖找到一個示例,但無法如何將javafx.scene.paint.Color JSON重構爲一個POJO。如何使用Jackson將javafx.scene.paint.Color轉換爲JSON並從中轉換

當我創建了JSON的Color.RED變成這樣:

{ 
    "annotationText" : "5/5/2015 12:18 PM", 
    "pageNumber" : 0, 
    "textColor" : { 
    "red" : 1.0, 
    "green" : 0.0, 
    "blue" : 0.0, 
    "opacity" : 1.0, 
    "opaque" : true, 
    "brightness" : 1.0, 
    "hue" : 0.0, 
    "saturation" : 1.0 
    }, 
"fillColor" : null 
} 

我不知道如何解析,早在使得它把Color.RED迴文字顏色上的場我的POJO。

任何指針,將不勝感激。

謝謝!

+0

看一看[這個博客(http://www.baeldung.com/jackson-deserialization)上自定義反序列化。您可以只抓住「red」,「green」,「blue」和「opacity」這兩個值,並將它們傳遞給['Color'構造函數](http://docs.oracle.com/javase/8/ javafx/api/javafx/scene/paint/Color.html#Color-double-double-double-double-) –

+0

謝謝 - 完美的工作! –

回答

0

基於上述評論 - 我能夠讓它工作,像這樣:

public class AnnotationDeserializer extends JsonDeserializer<AnnotationDetail>{ 

    @Override 
    public AnnotationDetail deserialize(JsonParser jp, DeserializationContext ctxt) 
     throws IOException, JsonProcessingException { 
     AnnotationDetail detail = new AnnotationDetail(); 

     JsonNode node = jp.getCodec().readTree(jp); 

     detail.setAnnotationText(node.get("annotationText").asText()); 
     detail.setPageNumber((Integer) ((IntNode) node.get("pageNumber")).numberValue()); 

     JsonNode textColorNode = node.get("textColor"); 
     double red =(Double) ((DoubleNode) textColorNode.get("red")).numberValue(); 
     double green = (Double) ((DoubleNode) textColorNode.get("green")).numberValue(); 
     double blue = (Double) ((DoubleNode) textColorNode.get("blue")).numberValue(); 
     double opacity = (Double) ((DoubleNode) textColorNode.get("opacity")).numberValue(); 

     detail.setTextColor(new Color(red, green, blue, opacity)); 
     return detail; 
    } 
} 
相關問題