2016-05-11 17 views
0

Ich habe eine json Struktur wieJson desirialization mit jackson mit Liste der Abstrakte Klasse Jackson

{"myobj": { "ford" : [ {"color":"blue","ford_property":"A" } ], "audi": [ {"color":"red", "audi_property":"B"}, {"color":"black", "audi_property":"C"} ] } } 

Klassenstruktur ist

abstract class Car implements Serializable { 
    private String color; 
    // getter setter here 
} 
class Ford extends Car { 
    private String fordProperty; 
    // getter setter here 
} 
class Audi extends Car { 
    private String audiProperty; 
    // getter setter here 
} 

Meine Antwort Klasse

class Response implements Serializable { 
    private Map<String, List<Car>> myObj; 
    // getter setters 
} 

Versuchte @JsonSubTypes mit auf Car Klasse aber das erwartet die type von class Name als Teil des Objekts {"color":"blue","ford_property":"A" }.

Dank

+0

Ich weiß nicht, ob ich dein Problem bekomme, aber ich würde dies trotzdem vorschlagen [Polymorphe JSON mit Jackson] (http://programermerbruce.blogspot.it/2011/05/deserialize-json-with-jackson-into .html) – mauros

+0

Ich habe versucht, dass polymorphe JSON mit Jackson das Problem ist, dass ich nicht 'Typ' als Teil der Klasse habe. Aber der 'Typ' ist der Schlüssel der Liste der Klasse. also anstelle von '[{Typ: Hund, Name: foo}]' Ich habe '{Hund: [{Name: Foo}]}' – Rishabh

+0

Ok, würde ich mit einem benutzerdefinierten Deserializer gehen, wenn ich du wäre. [Jackson - Custom Deserializer] (http://www.baeldung.com/jackson-deserialization) – mauros

Antwort

0

Option 1

Sie könnten wahrscheinlich dies mit einer Änderung in Ihrem Response Code erreichen und SerializationFeature.WRAP_ROOT_VALUE + DeserializationFeature.UNWRAP_ROOT_VALUE Einstellungen aktiviert

Hier Demo:

public static void main(String[] args) throws IOException { 
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); 
    mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); 
    Response resp = mapper.readValue(json, Response.class); 
} 

public abstract static class Car implements Serializable { 
    public String color; 
} 

public static class Ford extends Car { 
    @JsonProperty("ford_property") 
    public String fordProperty; 
} 

public static class Audi extends Car { 
    @JsonProperty("audi_property") 
    public String audiProperty; 
} 

@JsonRootName("myobj") 
public static class Response implements Serializable { 
    public List<Audi> audi; 
    public List<Ford> ford; 
} 

Option 2

Sie nichts in Ihrer Struktur ändern, ohne die Notwendigkeit für WRAP_ROOT_VALUE, aber das wird 3 Objekte 2mal

public static class Response implements Serializable { 
    private Map<String, List<Car>> myObj; 

    @JsonAnySetter 
    private void setter(String key, JsonNode value) { 
     try { 
      if (key.equals("ford")) { 

       myObj.put(key, mapper.readValue(value.toString(), 
            new TypeReference<List<Ford>>() {})); 
      } else if (key.equals("audi")){ 
       myObj.put(key, mapper.readValue(value.toString(), 
            new TypeReference<List<Audi>>() {})); 
      } 

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

Option

Individuelle Deserializer für Response fast die gleiche wie Deserialisieren in @JsonAnySetter Code von oben, aber kann es in Deserializer optimieren.