简体   繁体   English

Java Object使用Jackson将列表扩展到Json

[英]Java Object extending a list to Json with Jackson

I want to convert a object that is extending a list to Json using Jackson. 我想使用Jackson转换一个将列表扩展到Json的对象。 Example: 例:

public class TryToSerialize extends ArrayList<String>
{
    private int number;
    private String word;

    public TryToSerialize(){
        number = 0;
        word = "";
    }

    @JsonProperty
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
    @JsonProperty
    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    @JsonIgnore
    public String getAsJSON() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(new File("c:\\tts.json"), this);
        return mapper.writeValueAsString(this) ; 
    }
}

When I do serialize this I will only get the Array portion. 当我进行序列化时,我只会得到Array部分。

    TryToSerialize tts = new TryToSerialize();
    tts.setNumber(10);
    tts.setWord("Working");
    tts.add("first");
    tts.add("second");
    tts.add("third");
    String json = tts.getAsJSON();

Json: JSON:

["first","second","third"]

I am looking for a way to include the other variables of the object. 我正在寻找一种方法来包括对象的其他变量。

As your object extends one of the classes for which a default serialization behavior exists, your extra fields are just ignored. 当您的对象扩展存在默认序列化行为的类之一时,多余的字段将被忽略。

Now, one thing which is unclear to me is what should be the expected result. 现在,我不清楚的一件事是预期的结果。

If you are expecting a result such as: 如果您期望得到如下结果:

{"number":10,"word":"Working","values":["first","second","third"]}

Then you should be able to do it thanks to a custom serializer such as the following: 然后,借助以下自定义序列化程序,您应该能够做到这一点:

public class MySerializer extends JsonSerializer<TryToSerialize> {
    @Override
    public void serialize(TryToSerialize toSerialize, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeNumberField("number", toSerialize.getNumber());
        jgen.writeStringField("word", toSerialize.getWord());
        provider.defaultSerializeField("values", toSerialize.iterator(), jgen);
        jgen.writeEndObject();
    }
}

If you are expecting a result such as: 如果您期望得到如下结果:

[10,"Working","first","second","third"]

Then, serializer will look like the following: 然后,序列化器将如下所示:

public class MySerializer extends JsonSerializer<TryToSerialize> {
    @Override
    public void serialize(TryToSerialize toSerialize, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeStartArray();
        jgen.writeNumber(toSerialize.getNumber());
        jgen.writeString(toSerialize.getWord());
        for (Iterator<String> iterator = toSerialize.iterator(); iterator.hasNext();) {
            jgen.writeString((String) iterator.next());         
        }
        jgen.writeEndArray();
    }
}

Then, in both cases annotate your class with: 然后,在两种情况下都用以下注释您的班级:

@JsonSerialize(using = MySerializer.class)

There may be a smarter ways but this should work... 可能有更聪明的方法,但是应该可以...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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