简体   繁体   中英

Object Deserialization and Serialization with errors

i tried to do Object Deserialization and Serialization with both abstract class and interface but the Serialization does not work.

public class Zoo {


@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "sound")
@JsonSubTypes({ @JsonSubTypes.Type(value = SoundDog.class, name = "sdog"),
        @JsonSubTypes.Type(value = SoundCat.class, name = "scat"), })
public interface Sound{
    String getSound();
}

@JsonTypeName("sdog")
public static class SoundDog implements Sound{

    @Override
    public String getSound() {
        return "gau gau";
    }

}

@JsonTypeName("scat")
public static class SoundCat implements Sound{

    @Override
    public String getSound() {
        return "meo meo";
    }

}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = Dog.class, name = "dog"),
        @JsonSubTypes.Type(value = Cat.class, name = "cat"), })
public static abstract class Animal {
    public Animal() {
    }

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

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

    public Animal getParent() {
        return this.parent;
    }

    public void setParent(Animal p) {
        this.parent = p;
    }

    public Sound getSound() {
        return sound;
    }

    public void setSound(Sound sound) {
        this.sound = sound;
    }

    private String name;
    private Animal parent;
    private Sound sound;
}

@JsonTypeName("dog")
public static class Dog extends Animal {
    public Dog() {
    }

    public Dog(String name) {
        super(name);
    }
}

@JsonTypeName("cat")
public static class Cat extends Animal {
    public Cat() {
    }

    public Cat(String name) {
        super(name);
    }
}

public static void main(String... args) throws Exception {
    Zoo zoo = createZoo();

    ObjectMapper mapper = new ObjectMapper();
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

    String json1 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(zoo);
    System.out.println("+++>>>" + json1);

    zoo = mapper.readValue(json1, Zoo.class);

    String json2 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(zoo);

    System.out.println("Equals: " + json1.equals(json2));
}

private static Zoo createZoo() {
    Zoo zoo = new Zoo();

    Dog dog1 = new Dog("Dog1");
    dog1.setSound(new SoundDog());
    Dog dog2 = new Dog("Dog2");
    dog2.setSound(new SoundDog());
    Cat cat1 = new Cat("Cat1");
    Cat cat2 = new Cat("Cat2");

    dog2.setParent(dog1);
    cat2.setParent(cat1);

    zoo.add(dog1);
    zoo.add(dog2);
    zoo.add(cat1);
    zoo.add(cat2);

    return zoo;
}

public void add(Animal animal) {
    animals.add(animal);
}

public void setAnimals(List<Animal> list) {
    this.animals = list;
}

public List<Animal> getAnimals() {
    return this.animals;
}

private List<Animal> animals = new ArrayList<Animal>();
}

i got the error like the following

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "sound" (class eu.cea.ladis.util.Zoo$SoundDog), not marked as ignorable (0 known properties: ])
 at [Source: java.io.StringReader@26b8320f; line: 8, column: 18] (through reference chain: eu.cea.ladis.util.Zoo["animals"]->eu.cea.ladis.util.Dog["sound"]->eu.cea.ladis.util.SoundDog["sound"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
    at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:671)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:761)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1297)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1275)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:247)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:155)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:118)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:87)
    at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:132)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:155)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:118)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:87)
    at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:132)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:229)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
    at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:115)
    at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:56)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserializeWithType(CollectionDeserializer.java:242)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
    at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:115)
    at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromObject(AsArrayTypeDeserializer.java:66)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:921)
    at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:41)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2986)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2091)
    at eu.cea.ladis.util.Zoo.main(Zoo.java:112)

The generated JSON as the following:

+++>>>[ "eu.cea.ladis.util.Zoo", {
  "animals" : [ "java.util.ArrayList", [ {
"type" : "dog",
"name" : "Dog1",
"parent" : null,
"sound" : {
  "sound" : "sdog",
  "sound" : "gau gau"
}
  }, {
"type" : "dog",
"name" : "Dog2",
"parent" : {
  "type" : "dog",
  "name" : "Dog1",
  "parent" : null,
  "sound" : {
    "sound" : "sdog",
    "sound" : "gau gau"
  }
},
"sound" : {
  "sound" : "sdog",
  "sound" : "gau gau"
}}, {
    "type" : "cat",
    "name" : "Cat1",
    "parent" : null,
"sound" : null
  }, {
"type" : "cat",
"name" : "Cat2",
"parent" : {
  "type" : "cat",
  "name" : "Cat1",
  "parent" : null,
  "sound" : null
},
"sound" : null
  } ] ]
} ]

Anyone can guide me how to resolve my problem ?

Thanks you

我通过使用抽象类而不是接口解决了我的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM