Jackson反序列化/序列化不对属性进行排序 [英] Jackson deserialization/serialization does not sort properties
问题描述
我在应用程序中使用 Jackson 2.8.9 生成一些JSON。我有一些单元测试,在其中将生成的JSON与某些文件内容进行比较。
I'm using Jackson 2.8.9 in my application to generate some JSON. I have some unit tests in which I compare generated JSON with some files content.
当我将生成的JSON与文件内容进行比较时,由于属性的原因,它不匹配
When I compare my generated JSON with my file content, it does not match due to the properties order.
为了使测试可重复,我需要按字母顺序对属性进行排序。但是对于杰克逊,它似乎不起作用。
For the tests to be repeatable I need to have the properties sorted alphabetically. But with Jackson, it does not seems to work.
我写了一些关于幻觉的测试。只能通过 should_indent_properties
。
I wrote some tests for illustation. Only should_indent_properties
pass.
public class FormatJsonWithJacksonTest {
private static final String INDENTED_UNSORTED = "{\r\n" +
" \"firstChild\" : {\r\n" +
" \"subChild\" : {\r\n" +
" \"alphaItem\" : \"1234567891234567\",\r\n" +
" \"otherProperty\" : \"2017-06-21\",\r\n" +
" \"someOtherProperty\" : \"NONE\",\r\n" +
" \"alphaType\" : \"KIND_OF_TYPE\"\r\n" +
" }\r\n" +
" }\r\n" +
"}";
private static final String INDENTED_SORTED = "{\r\n" +
" \"firstChild\" : {\r\n" +
" \"subChild\" : {\r\n" +
" \"alphaItem\" : \"1234567891234567\",\r\n" +
" \"alphaType\" : \"KIND_OF_TYPE\",\r\n" +
" \"otherProperty\" : \"2017-06-21\",\r\n" +
" \"someOtherProperty\" : \"NONE\"\r\n" +
" }\r\n" +
" }\r\n" +
"}";
private static final String UNINDENTED_UNSORTED = "{" +
"\"firstChild\":{" +
"\"subChild\":{" +
"\"alphaItem\":\"1234567891234567\"," +
"\"otherProperty\":\"2017-06-21\"," +
"\"someOtherProperty\":\"NONE\"," +
"\"alphaType\":\"KIND_OF_TYPE\"" +
"}" +
"}" +
"}";
private static final String UNINDENTED_SORTED = "{" +
"\"firstChild\":{" +
"\"subChild\":{" +
"\"alphaItem\":\"1234567891234567\"," +
"\"alphaType\":\"KIND_OF_TYPE\"," +
"\"otherProperty\":\"2017-06-21\"," +
"\"someOtherProperty\":\"NONE\"" +
"}" +
"}" +
"}";
@Test
public void should_sort_properties() throws Exception {
// Given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper = objectMapper
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
// When
String formattedJson = tryingToFormatJson(objectMapper, INDENTED_UNSORTED);
// Then
assertEquals(UNINDENTED_SORTED, formattedJson);
}
@Test
public void should_indent_properties() throws Exception {
// Given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper = objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// When
String formattedJson = tryingToFormatJson(objectMapper, UNINDENTED_UNSORTED);
// Then
assertEquals(INDENTED_UNSORTED, formattedJson);
}
@Test
public void should_sort_and_indent_properties() throws Exception {
// Given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper = objectMapper
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.enable(SerializationFeature.INDENT_OUTPUT);
// When
String formattedJson = tryingToFormatJson(objectMapper, INDENTED_UNSORTED);
// Then
assertEquals(INDENTED_SORTED, formattedJson);
}
//
// Utils
//
private String tryingToFormatJson(ObjectMapper objectMapper, String unformattedJson)
throws IOException, JsonProcessingException {
JsonNode unsortedTree = objectMapper.readTree(unformattedJson);
Object treeToValue = objectMapper.treeToValue(unsortedTree, Object.class);
return objectMapper.writeValueAsString(treeToValue);
}
}
- 我如何用Jackson排序JSON?
- 您有实现我的方法
tryingToFormatJson
的任何解决方案吗? - 杰克逊(Jackson)是我执行此操作的正确工具吗?
- How could I sort my JSON with Jackson?
- Do you have any solution for implementing my method
tryingToFormatJson
? - Is Jackson the right tool I want for doing this?
推荐答案
何时您调用 objectMapper.treeToValue(unsortedTree,Object.class)
, Object
的类型是<$的子类c $ c>地图-只需放入 System.out.println(treeToValue.getClass())
进行检查。
When you call objectMapper.treeToValue(unsortedTree, Object.class)
, the Object
's type is a subclass of Map
- just put a System.out.println(treeToValue.getClass())
to check.
并根据 javadoc , SORT_PROPERTIES_ALPHABETICALLY
不对映射键进行排序:
And according to javadoc, SORT_PROPERTIES_ALPHABETICALLY
doesn't sort map keys:
功能,用于定义用于POJO字段的默认属性序列化顺序(注意:不适用于Map序列化离子!)
如果要对字段进行排序,您可以必须为您的结构创建自定义类,例如:
If you want to sort the fields, you have to create a custom class for you structure, like this:
public class Tree {
private Map<String, Child> firstChild;
// getters and setters
}
public class Child {
private String alphaItem;
private String otherProperty;
private String someOtherProperty;
private String alphaType;
// getters and setters
}
然后更改 treeToValue
调用:
Tree treeToValue = objectMapper.treeToValue(unsortedTree, Tree.class);
通过此操作,字段将被排序并且测试将起作用。
With this, the fields will be sorted and the test will work.
这篇关于Jackson反序列化/序列化不对属性进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!