给定一个与Jackson一起序列化为JSON字符串的java对象.是否可以控制序列化过程从同一对象生成不同的JSON输出?
压缩:
{
"a":"123",
"s":"100"
}
Run Code Online (Sandbox Code Playgroud)
或正常:
{
"altitude":"123",
"speed":"100"
}
Run Code Online (Sandbox Code Playgroud)
编辑:我想要实现的目标是拥有一个长JSON格式,这对于调试(人类可读)很好,并且具有提供最小占用空间的压缩格式.
Mic*_*ber 10
你可以通过很多方式做到这一点.这取决于您的要求.我建议你实现自己的属性命名策略.见下面的例子:
class CompressedPropertyNamingStrategy extends PropertyNamingStrategyBase {
private static final long serialVersionUID = 1L;
@Override
public String translate(String name) {
return String.valueOf(name.charAt(0));
}
}
Run Code Online (Sandbox Code Playgroud)
您可以这样使用它:
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new CompressedPropertyNamingStrategy());
String json = mapper.writeValueAsString(new Pojo());
Run Code Online (Sandbox Code Playgroud)
如果您不想压缩属性名称,只需删除第2行.
编辑1
@aumand评论之后我想通知,该解决方案不适用于包含许多以相同字母开头的属性的实体.我们必须编写更复杂的解决方案.例如:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2
如果您确实希望在序列化过程中控制属性名称,则应实现自己的注释.例如:
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();
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您的命名策略可能如下所示:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您必须为实体方法添加注释:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在此方案示例中,JSON可能如下所示:
{"a":123,"sp":100}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1348 次 |
| 最近记录: |