如何对 Jackson 使用自定义 Serializer?

我有两个 Java 类,我想用 Jackson 把它们序列化为 JSON:

public class User {
public final int id;
public final String name;


public User(int id, String name) {
this.id = id;
this.name = name;
}
}


public class Item {
public final int id;
public final String itemNr;
public final User createdBy;


public Item(int id, String itemNr, User createdBy) {
this.id = id;
this.itemNr = itemNr;
this.createdBy = createdBy;
}
}

我想序列化一个 Item 到这个 JSON:

{"id":7, "itemNr":"TEST", "createdBy":3}

with User serialized to only include the id. I will also be able to serilize all user objects to JSON like:

{"id":3, "name": "Jonas", "email": "jonas@example.com"}

因此,我想我需要为 Item编写一个自定义序列化程序,并尝试这样做:

public class ItemSerializer extends JsonSerializer<Item> {


@Override
public void serialize(Item value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("id", value.id);
jgen.writeNumberField("itemNr", value.itemNr);
jgen.writeNumberField("createdBy", value.user.id);
jgen.writeEndObject();
}


}

我用 Jackson How-to: Custom Serializers中的代码序列化 JSON:

ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule("SimpleModule",
new Version(1,0,0,null));
simpleModule.addSerializer(new ItemSerializer());
mapper.registerModule(simpleModule);
StringWriter writer = new StringWriter();
try {
mapper.writeValue(writer, myItem);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

But I get this error:

Exception in thread "main" java.lang.IllegalArgumentException: JsonSerializer of type com.example.ItemSerializer does not define valid handledType() (use alternative registration method?)
at org.codehaus.jackson.map.module.SimpleSerializers.addSerializer(SimpleSerializers.java:62)
at org.codehaus.jackson.map.module.SimpleModule.addSerializer(SimpleModule.java:54)
at com.example.JsonTest.main(JsonTest.java:54)

如何对 Jackson 使用自定义 Serializer?


如果是我,我会这么对付格森:

public class UserAdapter implements JsonSerializer<User> {


@Override
public JsonElement serialize(User src, java.lang.reflect.Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.id);
}
}


GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(User.class, new UserAdapter());
Gson gson = builder.create();
String json = gson.toJson(myItem);
System.out.println("JSON: "+json);

但是我现在需要用 Jackson 来做这件事,因为 Gson 不支持接口。

295651 次浏览

如果您在自定义序列化程序中的唯一要求是跳过对 Username字段的序列化,请将其标记为 暂时的。Jackson 不会序列化或反序列化 暂时的字段。

[另见: 为什么 Java 有瞬态字段?]

Jackson 的 JSON 视图 可能是实现您的需求的一种更简单的方法,特别是如果您的 JSON 格式具有一定的灵活性。

如果 {"id":7, "itemNr":"TEST", "createdBy":{id:3}}是一个可以接受的表示,那么这将是非常容易实现的非常少的代码。

您只需将 User 的 name 字段注释为视图的一部分,并在序列化请求中指定一个不同的视图(默认情况下将包含未注释的字段)

例如: 定义观点:

public class Views {
public static class BasicView{}
public static class CompleteUserView{}
}

注释用户:

public class User {
public final int id;


@JsonView(Views.CompleteUserView.class)
public final String name;


public User(int id, String name) {
this.id = id;
this.name = name;
}
}

并序列化请求的视图不包含您想隐藏的字段(默认情况下,未注释的字段是序列化的) :

objectMapper.getSerializationConfig().withView(Views.BasicView.class);

使用@JsonValue:

public class User {
int id;
String name;


@JsonValue
public int getId() {
return id;
}
}

@JsonValue only works on methods so you must add the getId method. 您应该能够完全跳过自定义序列化程序。

如前所述,@JsonValue 是一种很好的方式。但是如果你不介意自定义序列化程序,就不需要为 Item 编写序列化程序,而是为 User 编写一个序列化程序——如果是这样的话,它会像下面这样简单:

public void serialize(Item value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeNumber(id);
}

Yet another possibility is to implement JsonSerializable, in which case no registration is needed.

As to error; that is weird -- you probably want to upgrade to a later version. But it is also safer to extend org.codehaus.jackson.map.ser.SerializerBase as it will have standard implementations of non-essential methods (i.e. everything but actual serialization call).

可以将 @JsonSerialize(using = CustomDateSerializer.class)放在要序列化的对象的任何日期字段上。

public class CustomDateSerializer extends SerializerBase<Date> {


public CustomDateSerializer() {
super(Date.class, true);
}


@Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)");
String format = formatter.format(value);
jgen.writeString(format);
}


}

你必须覆盖方法 HandledType和一切都将工作

@Override
public Class<Item> handledType()
{
return Item.class;
}

我也尝试过这样做,Jackson 网页上的示例代码中有一个错误,没有在对 addSerializer()方法的调用中包含类型(.class) ,应该是这样的:

simpleModule.addSerializer(Item.class, new ItemSerializer());

换句话说,这些代码行实例化了 simpleModule并添加了序列化程序(前面的错误代码行被注释掉了) :

ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule("SimpleModule",
new Version(1,0,0,null));
// simpleModule.addSerializer(new ItemSerializer());
simpleModule.addSerializer(Item.class, new ItemSerializer());
mapper.registerModule(simpleModule);

这里是正确示例代码的参考资料: http://wiki.fasterxml.com/JacksonFeatureModules

In my case (Spring 3.2.4 and Jackson 2.3.1), XML configuration for custom serializer:

<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="serializers">
<array>
<bean class="com.example.business.serializer.json.CustomObjectSerializer"/>
</array>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

以无法解释的方式被某种东西覆盖回默认状态。

这对我很有效:

CustomObject.java

@JsonSerialize(using = CustomObjectSerializer.class)
public class CustomObject {


private Long value;


public Long getValue() {
return value;
}


public void setValue(Long value) {
this.value = value;
}
}

CustomObjectSerializer.java

public class CustomObjectSerializer extends JsonSerializer<CustomObject> {


@Override
public void serialize(CustomObject value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("y", value.getValue());
jgen.writeEndObject();
}


@Override
public Class<CustomObject> handledType() {
return CustomObject.class;
}
}

在我的解决方案中不需要 XML 配置(<mvc:message-converters>(...)</mvc:message-converters>)。

这些是我在试图理解 Jackson 序列化时注意到的行为模式。

1)假设有一个对象,一个班级,一个学生。为了方便起见,我已经把所有的东西都公开了。

public class Classroom {
public final double double1 = 1234.5678;
public final Double Double1 = 91011.1213;
public final Student student1 = new Student();
}


public class Student {
public final double double2 = 1920.2122;
public final Double Double2 = 2324.2526;
}

2) Assume that these are the serializers we use for serializing the objects into JSON. The writeObjectField uses the object's own serializer if it is registered with the object mapper; if not, then it serializes it as a POJO. The writeNumberField exclusively only accepts primitives as arguments.

public class ClassroomSerializer extends StdSerializer<Classroom> {
public ClassroomSerializer(Class<Classroom> t) {
super(t);
}


@Override
public void serialize(Classroom value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeStartObject();
jgen.writeObjectField("double1-Object", value.double1);
jgen.writeNumberField("double1-Number", value.double1);
jgen.writeObjectField("Double1-Object", value.Double1);
jgen.writeNumberField("Double1-Number", value.Double1);
jgen.writeObjectField("student1", value.student1);
jgen.writeEndObject();
}
}


public class StudentSerializer extends StdSerializer<Student> {
public StudentSerializer(Class<Student> t) {
super(t);
}


@Override
public void serialize(Student value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeStartObject();
jgen.writeObjectField("double2-Object", value.double2);
jgen.writeNumberField("double2-Number", value.double2);
jgen.writeObjectField("Double2-Object", value.Double2);
jgen.writeNumberField("Double2-Number", value.Double2);
jgen.writeEndObject();
}
}

3) Register only a DoubleSerializer with DecimalFormat output pattern ###,##0.000, in SimpleModule and the output is:

{
"double1" : 1234.5678,
"Double1" : {
"value" : "91,011.121"
},
"student1" : {
"double2" : 1920.2122,
"Double2" : {
"value" : "2,324.253"
}
}
}

您可以看到,POJO 序列化区分了 Double 和 Double,使用 DoubleSerialzer for Doubles 和使用常规 String 格式的 Double。

4)注册 DoubleSerializer 和 Classroom Serializer,但不使用 StudentSerializer。我们希望输出是这样的: 如果我们把 Double 写成对象,它的行为就像 Double; 如果我们把 Double 写成数字,它的行为就像 Double。学生实例变量应该以 POJO 的形式写入,并遵循上述模式,因为它没有注册。

{
"double1-Object" : {
"value" : "1,234.568"
},
"double1-Number" : 1234.5678,
"Double1-Object" : {
"value" : "91,011.121"
},
"Double1-Number" : 91011.1213,
"student1" : {
"double2" : 1920.2122,
"Double2" : {
"value" : "2,324.253"
}
}
}

5)注册所有序列化器,输出结果如下:

{
"double1-Object" : {
"value" : "1,234.568"
},
"double1-Number" : 1234.5678,
"Double1-Object" : {
"value" : "91,011.121"
},
"Double1-Number" : 91011.1213,
"student1" : {
"double2-Object" : {
"value" : "1,920.212"
},
"double2-Number" : 1920.2122,
"Double2-Object" : {
"value" : "2,324.253"
},
"Double2-Number" : 2324.2526
}
}

正如所料。

另一个重要的注意事项: 如果同一个类有多个序列化器注册在同一个模块中,那么模块将为最近添加到列表中的该类选择序列化器。这不应该被使用-这是令人困惑的,我不知道如何一致性这是

教训: 如果您想要自定义对象中基元的序列化,您必须为对象编写自己的序列化程序。您不能依赖 POJO Jackson 序列化。

我为自定义 Timestamp.class序列化/反序列化编写了一个示例,但是您可以根据需要使用它。

在创建对象映射器时,可以这样做:

public class JsonUtils {


public static ObjectMapper objectMapper = null;


static {
objectMapper = new ObjectMapper();
SimpleModule s = new SimpleModule();
s.addSerializer(Timestamp.class, new TimestampSerializerTypeHandler());
s.addDeserializer(Timestamp.class, new TimestampDeserializerTypeHandler());
objectMapper.registerModule(s);
};
}

例如,在 java ee中,可以使用以下命令初始化它:

import java.time.LocalDateTime;


import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;


@Provider
public class JacksonConfig implements ContextResolver<ObjectMapper> {


private final ObjectMapper objectMapper;


public JacksonConfig() {
objectMapper = new ObjectMapper();
SimpleModule s = new SimpleModule();
s.addSerializer(Timestamp.class, new TimestampSerializerTypeHandler());
s.addDeserializer(Timestamp.class, new TimestampDeserializerTypeHandler());
objectMapper.registerModule(s);
};


@Override
public ObjectMapper getContext(Class<?> type) {
return objectMapper;
}
}

序列化程序应该是这样的:

import java.io.IOException;
import java.sql.Timestamp;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;


public class TimestampSerializerTypeHandler extends JsonSerializer<Timestamp> {


@Override
public void serialize(Timestamp value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
String stringValue = value.toString();
if(stringValue != null && !stringValue.isEmpty() && !stringValue.equals("null")) {
jgen.writeString(stringValue);
} else {
jgen.writeNull();
}
}


@Override
public Class<Timestamp> handledType() {
return Timestamp.class;
}
}

反序列化这样的东西:

import java.io.IOException;
import java.sql.Timestamp;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.SerializerProvider;


public class TimestampDeserializerTypeHandler extends JsonDeserializer<Timestamp> {


@Override
public Timestamp deserialize(JsonParser jp, DeserializationContext ds) throws IOException, JsonProcessingException {
SqlTimestampConverter s = new SqlTimestampConverter();
String value = jp.getValueAsString();
if(value != null && !value.isEmpty() && !value.equals("null"))
return (Timestamp) s.convert(Timestamp.class, value);
return null;
}


@Override
public Class<Timestamp> handledType() {
return Timestamp.class;
}
}

在您的示例中,问题是 ItemSerializer 缺少需要从 JsonSerializer 重写的 handledType ()方法

public class ItemSerializer extends JsonSerializer<Item> {
    

@Override
public void serialize(Item value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("id", value.id);
jgen.writeNumberField("itemNr", value.itemNr);
jgen.writeNumberField("createdBy", value.user.id);
jgen.writeEndObject();
}


@Override
public Class<Item> handledType()
{
return Item.class;
}
}

因此,您会得到一个明确的错误,即没有定义 HandledType ()

Exception in thread "main" java.lang.IllegalArgumentException: JsonSerializer of type com.example.ItemSerializer does not define valid handledType()

希望对某些人有所帮助。谢谢你读了我的回答。