我有两个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;
}
}
我想将一个项目序列化为这个JSON:
{"id":7, "itemNr":"TEST", "createdBy":3}
并将用户序列化为仅包含id
。我还可以将所有用户对象序列化为JSON,如下所示:
{"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();
}
但是我得到了这个错误:
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中使用自定义序列化程序?
这就是我使用Gson的方式:
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不支持接口。
发布于 2011-08-24 06:39:47
如前所述,@JsonValue是一种很好的方法。但是,如果您不介意自定义序列化程序,则不需要为Item编写序列化程序,而是为用户编写序列化程序--如果是这样的话,它将非常简单:
public void serialize(Item value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeNumber(id);
}
另一种可能性是实现JsonSerializable
,在这种情况下不需要注册。
至于错误;这很奇怪--您可能想升级到更高的版本。但扩展org.codehaus.jackson.map.ser.SerializerBase
也更安全,因为它将拥有非必要方法的标准实现(即,除了实际的序列化调用之外的所有方法)。
发布于 2012-05-31 22:21:41
您可以将@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);
}
}
发布于 2013-12-12 05:30:40
我也尝试过这样做,在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
https://stackoverflow.com/questions/7161638
复制相似问题