它是混合使用注释和简单模块进行反序列化的一种方法吗?

时间:2019-06-05 13:21:08

标签: java json annotations deserialization

我需要反序列化具有3个différents属性的json文件。

编辑:或者,如何在AudienceDeserialization(未知大小)中反序列化List<User> optional and mandatory

听力等级:

public class Audience {
    Entity entity;
    private Integer stageBor;
    @JsonDeserialize(using = MandatoryDeserialize.class)
    private List<User> mandatory;
    @JsonDeserialize(using = OptionalDeserialize.class)
    private List<User> optional;
// ....

Json文件

[
  {
    "entity": "BFA",
    "stageBor": "1",
    "mandatory": [
        { "email": "11test@test.com"},
        { "email": "11test@test.com"}
    ],
    "optional": [
        { "email": "12test@test.com"},
        { "email": "12test@test.com"},
        { "email": "12test@test.com"},
        { "email": "12test@test.com"}
    ]
  },
    {
    "entity": "EX",
    "stageBor": "2",
    "mandatory": [
         { "email": "21test@test.com"},
        { "email": "21test@test.com"},
        { "email": "21test@test.com"}
    ],
    "optional": [
        { "email": "22test@test.com"},
        { "email": "22test@test.com"},
        { "email": "22test@test.com"}
    ]

  },
  {
    "entity": "IT",
    "stageBor": "3",
    "mandatory": [
         { "email": "31test@test.com"},
        { "email": "31test@test.com"},
        { "email": "31test@test.com"}
    ],
    "optional": [
         { "email": "33test@test.com"},
        { "email": "33test@test.com"},
        { "email": "33test@test.com"}
    ]
  }
]

强制反序列化

public class MandatoryDeserialize extends JsonDeserializer<List<User>> {

    @Override
    public List<User> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, IOException {
        InnerItems innerItems = jp.readValueAs(InnerItems.class);

        return innerItems. mandatory;
    }

    private static class InnerItems {
        public List<User>  mandatory;
    }
}

OptionalDeserialize

public class OptionalDeserialize extends JsonDeserializer<List<User>> {
    @Override
    public List<User> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, IOException {
        InnerItems innerItems = jp.readValueAs(InnerItems.class);

        return innerItems.optional;
    }

    private static class InnerItems {
        public List<User> optional;
    }

反序列化

public class AudienceDeserialization extends StdDeserializer<Audience> {

    public AudienceDeserialization() {
        this(null);
    }
    private AudienceDeserialization(Class<?> vc)
    {
        super(vc);
    }

    public Audience deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonNode node = jp.getCodec().readTree(jp);
        Entity entity = new Entity();
        String nameEntity = node.get("entity").asText();
        entity = entity.searchEntity(ListEntities.entities,nameEntity);
        Integer stageBor =  node.get("stageBor").asInt();
       // String email = node.get("email").asText();

        return new Audience(entity,stageBor, null, null);


主要

   public static void main(String[] args) throws FileNotFoundException {
        com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Audience.class, new AudienceDeserialization());
        mapper.registerModule(module);

        TypeReference<List<Audience>> typeReferenceAudience = new TypeReference<>() {};
        FileInputStream inputStreamAudience = new FileInputStream("C:\\Users\\oraph\\Desktop\\audience.json");
        try {
            List<Audience> audiences = mapper.readValue(inputStreamAudience, typeReferenceAudience);

            System.out.println("Users Saved!");
        } catch (IOException e) {
            System.out.println("Unable to save users: " + e.getMessage());
        }
    }

如我们所见,我使用带注释的两类反序列化 一类具有简单的模块。 但是只有简单的Module起作用,而我的注释不起作用。

是否可以同时使用两者?

0 个答案:

没有答案