是否可以在不使用注解的情况下指定ObjectNode属性的顺序?

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

Is it possible to specify the order of ObjectNode properties without using annotations?

问题

Jackson允许用户使用@JsonProperty(index = ?)注解为ObjectNode属性指定显式序列化顺序。这意味着,对于属性a,b,c,我可以指示Jackson按照以下顺序进行序列化:b,a,c。我可以控制确切的序列化顺序。

是否可以在不使用注解的情况下实现相同的效果?

我不想进行字母排序,我想指定显式排序顺序。

英文:

Jackson allows users to specify an explicit serialization order for ObjectNode properties using the @JsonProperty(index = ?) annotation. Meaning, given properties a, b, c I can instruct Jackson to serialize them in the following order: b, a, c. I control the exact serialization order.

Is it possible to achieve the same without the use of annotations?

I do not want alphabetic sorting. I want to specify an explicit sorting order.

答案1

得分: 0

你可以扩展 com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector 类,并通过重写 findSerializationPropertyOrder 方法为任何你想要的类提供顺序。

  1. import com.fasterxml.jackson.databind.SerializationFeature;
  2. import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
  3. import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
  4. import com.fasterxml.jackson.databind.json.JsonMapper;
  5. import lombok.Data;
  6. public class JacksonAnnotationIntrospectorApp {
  7. private final static JsonMapper JSON_MAPPER = JsonMapper.builder()
  8. .enable(SerializationFeature.INDENT_OUTPUT)
  9. .annotationIntrospector(new DynamicOrderJacksonAnnotationIntrospector())
  10. .build();
  11. public static void main(String[] args) throws Exception {
  12. var pojo = new DynamicOrderPojo();
  13. pojo.setA("A");
  14. pojo.setB(1);
  15. pojo.setC(3.33);
  16. JSON_MAPPER.writeValue(System.out, pojo);
  17. }
  18. }
  19. class DynamicOrderJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {
  20. @Override
  21. public String[] findSerializationPropertyOrder(AnnotatedClass ac) {
  22. if (ac.getRawType() == DynamicOrderPojo.class) {
  23. return new String[]{"b", "a", "c"};
  24. }
  25. return super.findSerializationPropertyOrder(ac);
  26. }
  27. }
  28. @Data
  29. class DynamicOrderPojo {
  30. private String a;
  31. private Integer b;
  32. private Double c;
  33. }

以上代码会输出:

  1. {
  2. "b" : 1,
  3. "a" : "A",
  4. "c" : 3.33
  5. }

也可以参考:

英文:

You can extend com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector class and provide your order for any class you want by overriding findSerializationPropertyOrder method.

  1. import com.fasterxml.jackson.databind.SerializationFeature;
  2. import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
  3. import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
  4. import com.fasterxml.jackson.databind.json.JsonMapper;
  5. import lombok.Data;
  6. public class JacksonAnnotationIntrospectorApp {
  7. private final static JsonMapper JSON_MAPPER = JsonMapper.builder()
  8. .enable(SerializationFeature.INDENT_OUTPUT)
  9. .annotationIntrospector(new DynamicOrderJacksonAnnotationIntrospector())
  10. .build();
  11. public static void main(String[] args) throws Exception {
  12. var pojo = new DynamicOrderPojo();
  13. pojo.setA("A");
  14. pojo.setB(1);
  15. pojo.setC(3.33);
  16. JSON_MAPPER.writeValue(System.out, pojo);
  17. }
  18. }
  19. class DynamicOrderJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {
  20. @Override
  21. public String[] findSerializationPropertyOrder(AnnotatedClass ac) {
  22. if (ac.getRawType() == DynamicOrderPojo.class) {
  23. return new String[]{"b", "a", "c"};
  24. }
  25. return super.findSerializationPropertyOrder(ac);
  26. }
  27. }
  28. @Data
  29. class DynamicOrderPojo {
  30. private String a;
  31. private Integer b;
  32. private Double c;
  33. }

Above code prints:

  1. {
  2. "b" : 1,
  3. "a" : "A",
  4. "c" : 3.33
  5. }

See also:

huangapple
  • 本文由 发表于 2023年5月30日 09:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361027.html
匿名

发表评论

匿名网友

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

确定