[英]Jackson Serialize Field to Different Name
I have this JSON to deserialize:我有这个 JSON 来反序列化:
{
"first-name": "Alpha",
"last-name": "Beta",
"gender": "m"
}
I want to serialize it to 2 different formats:我想将其序列化为 2 种不同的格式:
[A] [一]
{
"first-name": "Alpha",
"last-name": "Beta",
"gender": "m"
}
[B] [乙]
{
"firstName": "Alpha",
"lastName": "Beta",
"gender": "m"
}
I'm able to serialize it to 1 format: [A] only or [B] only.我可以将其序列化为 1 种格式:仅 [A] 或仅 [B]。 Here's my code to serialize it to [B]:
这是我将其序列化为 [B] 的代码:
public String firstName;
public String lastName;
public String gender;
@JsonProperty("firstName")
public String getFirstNameCC() {
return firstName;
}
@JsonProperty("first-name")
public void setFirstNameD(String firstName) {
this.firstName = firstName;
}
@JsonProperty("lastName")
public String getLastNameCC() {
return lastName;
}
@JsonProperty("last-name")
public void setLastNameD(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
I read about JsonView
here http://www.baeldung.com/jackson-json-view-annotation (section '5. Customize JSON Views') but it only changes its value.我在这里阅读了
JsonView
http://www.baeldung.com/jackson-json-view-annotation(“5 . 自定义 JSON 视图”部分),但它只会改变它的值。 I want to change field name as example above.我想像上面的例子一样更改字段名称。 Can anyone give insight on this?
任何人都可以对此提供见解吗?
I am not sure I completly understand your question, but for what I could understand you can do something like this to achieve different serializtions.我不确定我是否完全理解你的问题,但对于我所理解的,你可以做这样的事情来实现不同的序列化。
Create a custom annotation to hold all possible different serialization options:创建一个自定义注释来保存所有可能的不同序列化选项:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomJsonProperty {
String propertyName();
String format();
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface List {
CustomJsonProperty[] value();
}
}
Annotate your class accordingly:相应地注释你的类:
@JsonSerialize(using = CustomJsonPropertySerializer.class)
public class Bar {
@CustomJsonProperty.List({
@CustomJsonProperty(propertyName = "first-name", format = "A"),
@CustomJsonProperty(propertyName = "firstName", format = "B")
})
private String firstName;
@CustomJsonProperty.List({
@CustomJsonProperty(propertyName = "last-name", format = "A"),
@CustomJsonProperty(propertyName = "lastName", format = "B")
})
private String lastName;
@CustomJsonProperty.List({
@CustomJsonProperty(propertyName = "gender-x", format = "A"),
@CustomJsonProperty(propertyName = "gender", format = "B")
})
private String gender;
@JsonIgnore
private String format;
//getters & setters
}
Create a custom serializer to interpret your new annotation:创建一个自定义序列化程序来解释您的新注释:
public class CustomJsonPropertySerializer extends JsonSerializer<Bar> {
@Override
public void serialize(Bar bar, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
Field[] fields = bar.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = null;
try {
value = field.get(bar);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (field.isAnnotationPresent(CustomJsonProperty.List.class)) {
CustomJsonProperty[] properties = field.getAnnotation(CustomJsonProperty.List.class).value();
CustomJsonProperty chosenProperty = null;
for (CustomJsonProperty c : properties) {
if (c.format().equalsIgnoreCase(bar.getFormat())) {
chosenProperty = c;
break;
}
}
if (chosenProperty == null) {
//invalid format given, use first format then
chosenProperty = properties[0];
}
jsonGenerator.writeStringField(chosenProperty.propertyName(), value.toString());
}
}
jsonGenerator.writeEndObject();
}
}
Now you can serialize your objects taking into consideration different formats for the property names:现在,您可以考虑属性名称的不同格式来序列化您的对象:
public static void main(String[] args) throws IOException {
Bar bar1 = new Bar("first", "last", "m", "A");
Bar bar2 = new Bar("first", "last", "m", "B");
ObjectMapper mapper = new ObjectMapper();
String json1 = mapper.writeValueAsString(bar1);
String json2 = mapper.writeValueAsString(bar2);
System.out.println(json1);
System.out.println(json2);
}
Output:输出:
{"first-name":"first","last-name":"last","gender-x":"m"}
{"firstName":"first","lastName":"last","gender":"m"}
Of course the above serializer only works for Bar objects, but that can easily be solved using inheritance with abstract String getFormat();
当然,上面的序列化程序只适用于 Bar 对象,但是使用
abstract String getFormat();
继承可以很容易地解决这个问题abstract String getFormat();
on the super class and changing the custom serializer to accept the super class type, instead of Bar.在超类上并更改自定义序列化程序以接受超类类型,而不是 Bar。
Maybe there is a simpler way than creating your own stuff, but I don't know about it.也许有比创建自己的东西更简单的方法,但我不知道。 Let me know if something wasn't clear and I can elaborate it again.
如果有什么不清楚的,请告诉我,我可以再次详细说明。
There is a far easier way to do this - create an objectmapper that uses the "addMixin" function.有一个更简单的方法来做到这一点 - 创建一个使用“addMixin”函数的对象映射器。
Class to be serialized:要序列化的类:
Class YouWantToSerializeMe {
public String firstName;
public String lastName;
public String gender;
@JsonProperty("firstName")
public String getFirstNameCC() {
return firstName;
}
@JsonProperty("lastName")
public String getLastNameCC() {
return lastName;
}
}
Now, to serialize using both the built-in field names and custom field names, you can do this:现在,要同时使用内置字段名称和自定义字段名称进行序列化,您可以执行以下操作:
Class DoTheSerializing {
String serializeNormally(YouWantToSerializeMe me) {
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter objectWriter = objectMapper.writer();
return objectWriter(me)
}
String serializeWithMixin(YouWantToSerializeMe me) {
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter objectWriter = objectMapper
.addMixIn(YouWantToSerializeMe.class, MyMixin.class)
.writer();
return objectWriter(me)
}
interface MyMixin {
@JsonProperty("first-name")
public String getFirstNameCC();
@JsonProperty("last-name")
public String getLastNameCC();
}
}
This uses an embedded interface in the class to keep things very local.这在类中使用嵌入式接口来保持本地化。 You can make lots of optimizations around this, such as creating a static ObjectMapper and loading/unloading the mixin.
您可以围绕此进行大量优化,例如创建静态 ObjectMapper 和加载/卸载 mixin。
Using the interface as a "template" to control the mapping function is really powerful.使用界面作为“模板”来控制映射功能真的很强大。 You can add things at both the field and class level.
您可以在字段和类级别添加内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.