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

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

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 方法为任何你想要的类提供顺序。

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.Data;

public class JacksonAnnotationIntrospectorApp {
	private final static JsonMapper JSON_MAPPER = JsonMapper.builder()
			.enable(SerializationFeature.INDENT_OUTPUT)
			.annotationIntrospector(new DynamicOrderJacksonAnnotationIntrospector())
			.build();

	public static void main(String[] args) throws Exception {
		var pojo = new DynamicOrderPojo();
		pojo.setA("A");
		pojo.setB(1);
		pojo.setC(3.33);

		JSON_MAPPER.writeValue(System.out, pojo);
	}
}

class DynamicOrderJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {

	@Override
	public String[] findSerializationPropertyOrder(AnnotatedClass ac) {
		if (ac.getRawType() == DynamicOrderPojo.class) {
			return new String[]{"b", "a", "c"};
		}
		return super.findSerializationPropertyOrder(ac);
	}
}

@Data
class DynamicOrderPojo {

	private String a;
	private Integer b;
	private Double c;
}

以上代码会输出:

{
  "b" : 1,
  "a" : "A",
  "c" : 3.33
}

也可以参考:

英文:

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

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.Data;

public class JacksonAnnotationIntrospectorApp {
	private final static JsonMapper JSON_MAPPER = JsonMapper.builder()
			.enable(SerializationFeature.INDENT_OUTPUT)
			.annotationIntrospector(new DynamicOrderJacksonAnnotationIntrospector())
			.build();

	public static void main(String[] args) throws Exception {
		var pojo = new DynamicOrderPojo();
		pojo.setA("A");
		pojo.setB(1);
		pojo.setC(3.33);

		JSON_MAPPER.writeValue(System.out, pojo);
	}
}

class DynamicOrderJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {

	@Override
	public String[] findSerializationPropertyOrder(AnnotatedClass ac) {
		if (ac.getRawType() == DynamicOrderPojo.class) {
			return new String[]{"b", "a", "c"};
		}
		return super.findSerializationPropertyOrder(ac);
	}
}

@Data
class DynamicOrderPojo {

	private String a;
	private Integer b;
	private Double c;
}

Above code prints:

{
  "b" : 1,
  "a" : "A",
  "c" : 3.33
}

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:

确定