是否可以使Jackson和XML映射器使用类名称作为元素名称而不是属性名称来序列化属性?
即给出:
_buildGameCategory() {
List<Map<String, dynamic>> games = [
{
"title": AppMenu.sport,
"page": "Sport",
"icon": AppImages.icon_sport,
},
{
"title": AppMenu.live_casino,
"page": "LiveCasino",
"icon": AppImages.icon_casino,
},
{
"title": AppMenu.virtual_sport,
"page": "VirtualSport",
"icon": AppImages.icon_sport,
},
{
"title": AppMenu.game,
"page": "Games",
"icon": AppImages.icon_casino,
},
{
"title": AppMenu.game,
"page": "Games",
"icon": AppImages.icon_casino,
},
{
"title": AppMenu.game,
"page": "Games",
"icon": AppImages.icon_casino,
}
];
return GridView.count(
shrinkWrap: true,
crossAxisCount: 3,
padding: EdgeInsets.all(Constant.sizeMedium),
crossAxisSpacing: Constant.sizeSmall,
mainAxisSpacing: Constant.sizeSmall,
physics: BouncingScrollPhysics(),
children: games.map((category) {
return GestureDetector(
child: Card(
elevation: 0.0,
color: AppColors.color2,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Container(
width: 80.0,
height: 80.0,
child: Stack(
children: <Widget>[AppIcons.iconGame],
),
),
),
Expanded(
child: Text(
category["title"],
style: TextStyle(
fontSize: FontSize.s18,
fontWeight: FontWeight.w400,
color: AppColors.color1),
),
),
Divider(color: Colors.white, thickness: 1.0),
],
)),
onTap: () {
print(category);
},
);
}).toList(),
);
结果是:
package com.example.sometest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
public class Test2 {
public static void main(String[] args) throws JsonProcessingException {
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.registerModule(new JaxbAnnotationModule());
System.out.println(mapper.writeValueAsString(new A()));
}
}
class A {
public B getFoobar() {return new B();}
}
class B {
public String getSomething() {return "something";}
}
相反,我希望输出为:
<A>
<foobar>
<something>something</something>
</foobar>
</A>
显然,这仅适用于某些类型,否则也会影响字符串,这不是我们想要的。
这是一个更大的应用程序的一部分,使用xjc生成XML类,在这种特殊情况下,<A>
<B>
<something>something</something>
</B>
</A>
还具有超类,还需要使用它们各自的类名作为元素名来进行序列化。
修改
这是一个示例,其中我希望周围的元素成为实际的类名,即使具有继承性:
B
这将产生:
public class Test2 {
public static void main(String[] args) throws JsonProcessingException {
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.registerModule(new JaxbAnnotationModule());
System.out.println(mapper.writeValueAsString(new A(new C())));
}
}
class A {
private final B object;
A(B object) {this.object = object;}
public B getFoobar() {return object;}
}
class B {
public String getSomething() {return "something";}
}
class C extends B {
public String getSomething() {return "other thing";}
}
但应产生:
<A>
<foobar>
<something>other thing</something>
</foobar>
</A>
Edit2:
这不是Jackson的工作方式,因此使用自定义序列化程序解决了该问题。
答案 0 :(得分:0)
在类上提供XML属性注释将使您拥有方法getFoobar
,该方法可以序列化为其他方法。 https://mincong-h.github.io/2019/03/19/jackson-xml-mapper/