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

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

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 序列化器,并列出所有属性。以下是示例代码:

  1. import com.fasterxml.jackson.annotation.JsonIgnore;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.databind.JsonSerializer;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import java.io.IOException;
  6. public class JsonApp {
  7. public static void main(String[] args) throws IOException {
  8. ObjectMapper mapper = new ObjectMapper();
  9. JsonSerializer<Object> serializer = mapper.getSerializerProviderInstance().findValueSerializer(Result.class);
  10. serializer.properties().forEachRemaining(p -> {
  11. System.out.println(p);
  12. });
  13. }
  14. }
  15. class Result {
  16. @JsonIgnore
  17. private int id = 1;
  18. private String name = "Jack";
  19. public int getId() {
  20. return id;
  21. }
  22. public void setId(int id) {
  23. this.id = id;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. @JsonProperty("id")
  32. public String getIdAsString() {
  33. return "ID:" + id;
  34. }
  35. }

以上代码会输出:

  1. property 'name' (via method com.celoxity.Result#getName, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)
  2. 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:

  1. import com.fasterxml.jackson.annotation.JsonIgnore;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.databind.JsonSerializer;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import java.io.IOException;
  6. public class JsonApp {
  7. public static void main(String[] args) throws IOException {
  8. ObjectMapper mapper = new ObjectMapper();
  9. JsonSerializer&lt;Object&gt; serializer = mapper.getSerializerProviderInstance().findValueSerializer(Result.class);
  10. serializer.properties().forEachRemaining(p -&gt; {
  11. System.out.println(p);
  12. });
  13. }
  14. }
  15. class Result {
  16. @JsonIgnore
  17. private int id = 1;
  18. private String name = &quot;Jack&quot;;
  19. public int getId() {
  20. return id;
  21. }
  22. public void setId(int id) {
  23. this.id = id;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. @JsonProperty(&quot;id&quot;)
  32. public String getIdAsString() {
  33. return &quot;ID:&quot; + id;
  34. }
  35. }

Above code prints:

  1. property &#39;name&#39; (via method com.celoxity.Result#getName, static serializer of type com.fasterxml.jackson.databind.ser.std.StringSerializer)
  2. 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:

确定