获取同一对象的不同JSON表示形式 [英] Get different JSON representations of the same object
问题描述
给出一个用Jackson序列化为JSON字符串的java对象. 是否可以控制序列化过程以从同一对象生成不同的JSON输出?
Given a java object which is serialised into a JSON string with Jackson. Is it possible to control the serialisation process to generate different JSON outputs from the same object?
压缩:
{
"a":"123",
"s":"100"
}
或正常:
{
"altitude":"123",
"speed":"100"
}
我要达到的目标是拥有一个长的JSON格式(可用于调试)(易于阅读),并具有一个压缩的格式(可提供最小的占用空间).
The goal I want to achieve with this to have a long JSON format which is good for debugging (human-readable) and have a compressed format which is providing the smallest footprint.
推荐答案
您可以通过多种方式进行操作.这取决于您的要求.我建议您实施自己的财产命名策略.参见以下示例:
You can do it in many ways. It depends from your requirements. I suggest to implement your own property naming strategy. See below example:
class CompressedPropertyNamingStrategy extends PropertyNamingStrategyBase {
private static final long serialVersionUID = 1L;
@Override
public String translate(String name) {
return String.valueOf(name.charAt(0));
}
}
您可以通过以下方式使用它:
You can use it in this way:
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new CompressedPropertyNamingStrategy());
String json = mapper.writeValueAsString(new Pojo());
如果您不想压缩属性名称,只需删除第2行.
If you do not want to compress property names, just remove line number 2.
编辑1
在@aumand注释之后,我想告诉您,该解决方案不适用于包含以同一字母开头的许多属性的实体.我们必须编写很多复杂的解决方案.例如:
EDIT 1
After @aumand comment I want to inform, that this solution will not work for entities which contain many properties which starts from the same letter. We have to write much sophisticated solution. For example:
class CompressedPropertyNamingStrategy extends PropertyNamingStrategyBase {
private static final long serialVersionUID = 1L;
private final int length;
public CompressedPropertyNamingStrategy(int length) {
this.length = length;
}
@Override
public String translate(String name) {
if (name.length() < length) {
return name;
}
return name.substring(0, length);
}
}
编辑2
如果您确实希望在序列化过程中控制属性名称,则应实现自己的注释.例如:
Edit 2
If you really want to have control on property names during serialization process, you should implement your own annotation. For example:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = { ElementType.METHOD, ElementType.FIELD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface CompressedName {
String value();
}
在这种情况下,您的命名策略可能如下所示:
Your naming strategy in this case could look like this:
class CompressedPropertyNamingStrategy extends PropertyNamingStrategy {
private static final long serialVersionUID = 1L;
@Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method,
String defaultName) {
CompressedName compressedProperty = method.getAnnotation(CompressedName.class);
if (compressedProperty != null) {
return compressedProperty.value();
}
// Implement default value: first letter, or something else
return defaultName;
}
}
现在,您必须在实体方法中添加注释:
Now, you have to add annotation to your entity methods:
class Entity {
private long altitude = 123;
private int speed = 100;
@CompressedName("a")
public long getAltitude() {
return altitude;
}
public void setAltitude(long altitude) {
this.altitude = altitude;
}
@CompressedName("sp")
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
在这种情况下,JSON可能如下所示:
In this scenario example JSON could look like this:
{"a":123,"sp":100}
这篇关于获取同一对象的不同JSON表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!