如何将List包装为Jackson生成的JSON中的顶级元素 [英] How to wrap a List as top level element in JSON generated by Jackson
问题描述
我遇到了一个问题,我试图将List包含为根节点,但我似乎无法得到它。让我解释。假设我们有一个类TestClass
I am running into a problem where I am trying to include a List as the root node, but I can't seem to be able to get this. Let me explain. Let's say we have a class "TestClass"
class TestClass{
String propertyA;
}
现在,在某种实用方法中,这就是我的工作
Now, in some utility method this is what I do
String utilityMethod(){
List<TestClass> list = someService.getList();
new ObjectMapper().writeValueAsString(list);
}
我想用JSON获得的输出是
The output I am trying to get in JSON is
{"ListOfTestClasses":[{"propertyA":"propertyAValue"},{"propertyA":"someOtherPropertyValue"}]}
我试图使用
objMapper.getSerializationConfig().set(Feature.WRAP_ROOT_VALUE, true);
但是,我似乎仍然没有做对。
But, I still don't seem to get it right.
现在,我正在创建一个Map< String,TestClass>我写的是为了实现我想要做的事情,但这很有效但很明显这是一个黑客。有人可以帮我一个更优雅的解决方案吗?谢谢
Right now, I am just creating a Map < String,TestClass > and I write that to achieve what I am trying to do, which works but clearly this is a hack. Could someone please help me with a more elegant solution? Thanks
推荐答案
不幸的是,即使启用了 WRAP_ROOT_VALUE
功能,您仍然需要用于控制序列化Java集合时生成的根名称的额外逻辑(请参阅这个答案详情为什么)。这使您可以选择:
Unfortunately, even with the WRAP_ROOT_VALUE
feature enabled you still need extra logic to control the root name generated when serializing a Java collection (see this answer for details why). Which leaves you with the options of:
- 使用持有人类来定义根名称
- 使用地图。
- 使用自定义
ObjectWriter
- using a holder class to define the root name
- using a map.
- using a custom
ObjectWriter
以下是一些说明三种不同选项的代码:
Here is some code illustrating the three different options:
public class TestClass {
private String propertyA;
// constructor/getters/setters
}
public class TestClassListHolder {
@JsonProperty("ListOfTestClasses")
private List<TestClass> data;
// constructor/getters/setters
}
public class TestHarness {
protected List<TestClass> getTestList() {
return Arrays.asList(new TestClass("propertyAValue"), new TestClass(
"someOtherPropertyValue"));
}
@Test
public void testSerializeTestClassListDirectly() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
System.out.println(mapper.writeValueAsString(getTestList()));
}
@Test
public void testSerializeTestClassListViaMap() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final Map<String, List<TestClass>> dataMap = new HashMap<String, List<TestClass>>(
4);
dataMap.put("ListOfTestClasses", getTestList());
System.out.println(mapper.writeValueAsString(dataMap));
}
@Test
public void testSerializeTestClassListViaHolder() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final TestClassListHolder holder = new TestClassListHolder();
holder.setData(getTestList());
System.out.println(mapper.writeValueAsString(holder));
}
@Test
public void testSerializeTestClassListViaWriter() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final ObjectWriter writer = mapper.writer().withRootName(
"ListOfTestClasses");
System.out.println(writer.writeValueAsString(getTestList()));
}
}
输出:
{ArrayList:[{propertyA:propertyAValue},{propertyA:someOtherPropertyValue}]}
{ListOfTestClasses :[{propertyA:propertyAValue},{propertyA:someOtherPropertyValue}]}
{ListOfTestClasses:[{propertyA:propertyAValue},{propertyA :someOtherPropertyValue}]}
{ListOfTestClasses:[{propertyA:propertyAValue},{propertyA:someOtherPropertyValue}]}
{"ArrayList":[{"propertyA":"propertyAValue"},{"propertyA":"someOtherPropertyValue"}]}
{"ListOfTestClasses":[{"propertyA":"propertyAValue"},{"propertyA":"someOtherPropertyValue"}]}
{"ListOfTestClasses":[{"propertyA":"propertyAValue"},{"propertyA":"someOtherPropertyValue"}]}
{"ListOfTestClasses":[{"propertyA":"propertyAValue"},{"propertyA":"someOtherPropertyValue"}]}
使用 ObjectWriter
非常方便 - 请记住,用它序列化的所有顶级对象都将具有相同的根名称。如果那不可取,那么请改用地图或持有人类。
Using an ObjectWriter
is very convenient - just bare in mind that all top level objects serialized with it will have the same root name. If thats not desirable then use a map or holder class instead.
这篇关于如何将List包装为Jackson生成的JSON中的顶级元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!