简体   繁体   中英

How to implement dynamic partial JSON serialization with Jackson?

I have a POJO with custom setter methods for all properties that track whether the property was explicitly set. The setter stores to fieldNameSet boolean fields and exposes isFieldNameSet getters for those flags. I want Jackson to dynamically serialize the class with only those fields that have isFieldNameSet as true .

Background:

I started writing a custom JsonFilter implementation but it doesn't give any context as to the current object instance being serialized so obviously I can't read the current values of the isFieldNameSet properties.

Quickly hacked from a Jackson example

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

public class JacksonExample {
    public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();

        User user = createDummyUser();

        try {

            //Its age here , this is conditional based on your fieldset
            SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("age");
            FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);

            // Convert object to JSON string
            String jsonInString = jsonInString = mapper.writer(filters).writeValueAsString(user);
            System.out.println(jsonInString);

            // Convert object to JSON string and pretty print

            //System.out.println(jsonInString);

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

    }

    private static User createDummyUser() {

        User user = new User();

        user.setName("mkyong");
        user.setAge(33);

        List<String> msg = new ArrayList<>();
        msg.add("hello jackson 1");
        msg.add("hello jackson 2");
        msg.add("hello jackson 3");

        user.setMessages(msg);

        return user;

    }
}


package org.soproject;

import java.util.List;

import org.codehaus.jackson.map.annotate.JsonFilter;

@JsonFilter("myFilter")
public class User {

    private String name;
    private int age;
    private List<String> messages;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<String> getMessages() {
        return messages;
    }

    public void setMessages(List<String> messages) {
        this.messages = messages;
    }

    // getters and setters

}

Ignores age as you see :

{"name":"mkyong","messages":["hello jackson 1","hello jackson 2","hello jackson 3"]}

Note jackson source is from : https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

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