2017-01-25 1 views
0

私は2.6.4でfasterxmlを使用して、私は与えられた出力に影響を及ぼさないその上の外部サービスで、次のJSONを取得:デシリアライズフラットJSON

{ 
    "name": "dunnosName", 
    "widthValue": 46.1, 
    "heightValue": 56.1, 
    "depthValue": 66.1, 
    "unit": "mm" 
} 

し、それをマップしたいですPOJOを次

public class Dunno { 
    private String name; 
    private ValueWithUnit width; 
    private ValueWithUnit height; 
    private ValueWithUnit depth; 
} 

public class ValueWithUnit { 
    private Float value; 
    private String unit; 
} 

私の除外マッピングは次のようになります。

name -> Dunno.name 

widthValue -> Dunno.width.value 

heightValue -> Dunno.height.value 

depthValue -> Dunno.depth.value 

unit -> Dunno.width.unit 

unit -> Dunno.height.unit 

unit -> Dunno.depth.unit 

されていますfasterxmlを使用して予想されるマッピングを実現することは可能ですか?もしそうなら、このマッピングを実現するために、fasterxml注釈やクラスを実装する必要がありますか?

+0

eドキュメント、その可能性を完全には確信していない(それは、私は完全に見ていない可能性があります)。レスポンスをプロキシし、自動的に解析できるものを得るために、レスポンスをプロキシする必要があるかもしれません。例えば、各 "value"に値と単位を含むように変換するなどです。 – Brendan

答えて

1

移行の必要はありませんTempDunnoCustom Deserializerが必要です。これは、あなたが1つを使用する教科書の例です。 Dunnoクラスに以下の注釈を追加:

@JsonDeserialize(using = DunnoDeserializer.class) 

を、ここでそれは同様にして、入力の検証と、次のとおりです。

@SuppressWarnings("serial") 
public class DunnoDeserializer extends StdDeserializer<Dunno> 
{ 
    public DunnoDeserializer() 
    { 
     this(null); 
    } 

    public DunnoDeserializer(Class<?> vc) 
    { 
     super(vc); 
    } 

    @Override 
    public Dunno deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException 
    { 
     Dunno dunno = new Dunno(); 
     // first parse the input into a map, which is more convenient to work with 
     @SuppressWarnings("unchecked") 
     Map<String, Object> values = jp.getCodec().readValue(jp, Map.class); 
     dunno.name = values.containsKey("name") ? values.get("name").toString() : "empty"; 
     String unit = values.containsKey("unit") ? values.get("unit").toString() : "default-units"; 
     if (values.containsKey("widthValue")) { 
      dunno.width = new ValueWithUnit(); 
      dunno.width.value = ((Number)values.get("widthValue")).floatValue(); 
      dunno.width.unit = unit; 
     } 
     if (values.containsKey("heightValue")) { 
      dunno.height = new ValueWithUnit(); 
      dunno.height.value = ((Number)values.get("heightValue")).floatValue(); 
      dunno.height.unit = unit; 
     } 
     if (values.containsKey("depthValue")) { 
      dunno.depth = new ValueWithUnit(); 
      dunno.depth.value = ((Number)values.get("depthValue")).floatValue(); 
      dunno.depth.unit = unit; 
     } 
     System.out.println(values); 
     values.values().forEach(v -> System.out.println(v.getClass())); 
     return dunno; 
    } 
} 

試験方法:

public static void main(String[] args) 
{ 
    String jsonString = "{ \"name\": \"dunnosName\"," + "\"widthValue\": 46.1," + "\"heightValue\": 56.1," 
      + "\"depthValue\": 66.1," + "\"unit\": \"mm\"}"; 

    ObjectMapper mapper = new ObjectMapper(); 
    try { 
     Dunno d = (Dunno)mapper.readValue(jsonString, Dunno.class); 
     System.out.format("%s: %.2f(%s) %.2f(%s) %.2f(%s)", 
       d.name, d.width.value, d.width.unit, d.height.value, d.height.unit, d.depth.value, d.depth.unit); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

期待される出力を与える:

dunnosName: 46.10(mm) 56.10(mm) 66.10(mm) 
+0

アイデアは良いですが、提案されたソリューションは個人のフィールドと正確には動作しませんので、セッターを使用する必要があります – maloney

+0

あなたは10ヶ月前から投稿を議論していることを認識していますか?とにかく、** NO **の解決策はプライベートフィールドで動作します。これはここでは問題ではありません。 getterとsetterが用意されていると推測されます。 –

+0

さらに、「提案された」解決策が承認されたので、私はOP –

-1
public class Dunno { 
    private String name; 
    private ValueWithUnit width; 
    private ValueWithUnit height; 
    private ValueWithUnit depth; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public ValueWithUnit getWidth() { 
     return width; 
    } 

    public void setWidth(ValueWithUnit width) { 
     this.width = width; 
    } 

    public ValueWithUnit getHeight() { 
     return height; 
    } 

    public void setHeight(ValueWithUnit height) { 
     this.height = height; 
    } 

    public ValueWithUnit getDepth() { 
     return depth; 
    } 

    public void setDepth(ValueWithUnit depth) { 
     this.depth = depth; 
    } 

    @Override 
    public String toString() { 
     return "Dunno{" + 
       "name='" + name + '\'' + 
       ", width=" + width + 
       ", height=" + height + 
       ", depth=" + depth + 
       '}'; 
    } 
} 

public class ValueWithUnit { 
    private Float value; 
    private String unit; 

    public ValueWithUnit(Float value, String unit) { 
     this.value = value; 
     this.unit = unit; 
    } 

    public Float getValue() { 
     return value; 
    } 

    public void setValue(Float value) { 
     this.value = value; 
    } 

    public String getUnit() { 
     return unit; 
    } 

    public void setUnit(String unit) { 
     this.unit = unit; 
    } 

    @Override 
    public String toString() { 
     return "ValueWithUnit{" + 
       "value=" + value + 
       ", unit='" + unit + '\'' + 
       '}'; 
    } 
} 

public class TempDunno { 
    private String name; 
    private Float widthValue; 
    private Float heightValue; 
    private Float depthValue; 
    private String unit; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Float getWidthValue() { 
     return widthValue; 
    } 

    public void setWidthValue(Float widthValue) { 
     this.widthValue = widthValue; 
    } 

    public Float getHeightValue() { 
     return heightValue; 
    } 

    public void setHeightValue(Float heightValue) { 
     this.heightValue = heightValue; 
    } 

    public Float getDepthValue() { 
     return depthValue; 
    } 

    public void setDepthValue(Float depthValue) { 
     this.depthValue = depthValue; 
    } 

    public String getUnit() { 
     return unit; 
    } 

    public void setUnit(String unit) { 
     this.unit = unit; 
    } 

    @Override 
    public String toString() { 
     return "TempDunno{" + 
       "name='" + name + '\'' + 
       ", widthValue=" + widthValue + 
       ", heightValue=" + heightValue + 
       ", depthValue=" + depthValue + 
       ", unit='" + unit + '\'' + 
       '}'; 
    } 
} 

public class Main { 

    public static void main(String[] args) throws IOException { 
     String json = "{\n" + 
       "  \"name\": \"dunnosName\",\n" + 
       "  \"widthValue\": 46.1,\n" + 
       "  \"heightValue\": 56.1,\n" + 
       "  \"depthValue\": 66.1,\n" + 
       "  \"unit\": \"mm\"\n" + 
       "}"; 

     System.out.println(getDunno(json)); 
    } 

    private static Dunno getDunno(String json) throws IOException { 
     ObjectMapper mapper = new ObjectMapper(); 
     TempDunno tmp = mapper.readValue(json, TempDunno.class); 
     Dunno dunno = new Dunno(); 
     dunno.setName(tmp.getName()); 
     dunno.setHeight(new ValueWithUnit(tmp.getHeightValue(), tmp.getUnit())); 
     dunno.setWidth(new ValueWithUnit(tmp.getWidthValue(), tmp.getUnit())); 
     dunno.setDepth(new ValueWithUnit(tmp.getDepthValue(), tmp.getUnit())); 
     return dunno; 
    } 
}