英文:
Getting the list of fields that will be serialized by Jackson
问题
我正在尝试以编程方式获取在使用Jackson序列化器进行序列化的类中字段的列表。
我可以基于反射编写代码,遍历类中的字段,以查看哪些字段是公共的,具有getter/setter,或具有Jackson特定的注解等,并根据Jackson遵循的规则列出字段列表,以决定是否对字段进行序列化。但是实现很可能会不完整。
是否有使用Jackson库可以实现这一点的方法?
英文:
I am trying to programmatically get the list of fields in a class that will be serialized while using Jackson serializer.
I can write code based on reflection to go over the fields in the class to see which ones are public, have getter/setter, or have Jackson specific annotations, etc. and come up with a list as per the rules followed by Jackson to decide whether a field will be serialized. But there is a good chance that the implementation will be incomplete.
Is there any way using the Jackson library that I can achieve this?
答案1
得分: 3
你可以尝试使用 findValueSerializer 方法找到默认的 Bean 序列化器,并列出所有属性。以下是示例代码:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class JsonApp {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonSerializer<Object> serializer = mapper.getSerializerProviderInstance().findValueSerializer(Result.class);
        serializer.properties().forEachRemaining(p -> {
            System.out.println(p);
        });
    }
}
class Result {
    @JsonIgnore
    private int id = 1;
    private String name = "Jack";
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @JsonProperty("id")
    public String getIdAsString() {
        return "ID:" + id;
    }
}
以上代码会输出:
property 'name' (via method com.celoxity.Result#getName, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)
property 'id' (via method com.celoxity.Result#getIdAsString, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)
英文:
You can try to find default bean serialiser using findValueSerializer and list all properties. See below example:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class JsonApp {
	public static void main(String[] args) throws IOException {
		ObjectMapper mapper = new ObjectMapper();
		JsonSerializer<Object> serializer = mapper.getSerializerProviderInstance().findValueSerializer(Result.class);
		serializer.properties().forEachRemaining(p -> {
			System.out.println(p);
		});
	}
}
class Result {
	@JsonIgnore
	private int id = 1;
	private String name = "Jack";
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@JsonProperty("id")
	public String getIdAsString() {
		return "ID:" + id;
	}
}
Above code prints:
property 'name' (via method com.celoxity.Result#getName, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)
property 'id' (via method com.celoxity.Result#getIdAsString, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论