英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论