获取将由Jackson序列化的字段列表

huangapple go评论76阅读模式
英文:

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&lt;Object&gt; serializer = mapper.getSerializerProviderInstance().findValueSerializer(Result.class);
		serializer.properties().forEachRemaining(p -&gt; {
			System.out.println(p);
		});
	}
}

class Result {

	@JsonIgnore
	private int id = 1;
	private String name = &quot;Jack&quot;;

	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(&quot;id&quot;)
	public String getIdAsString() {
		return &quot;ID:&quot; + id;
	}
}

Above code prints:

property &#39;name&#39; (via method com.celoxity.Result#getName, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)
property &#39;id&#39; (via method com.celoxity.Result#getIdAsString, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)

huangapple
  • 本文由 发表于 2020年10月1日 16:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64151577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定