gpt4 book ai didi

java - 如何在 Jackson 中使用自定义序列化程序?

转载 作者:IT老高 更新时间:2023-10-28 11:26:42 25 4
gpt4 key购买 nike

我有两个想要使用 Jackson 序列化为 JSON 的 Java 类:

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}

将用户序列化为仅包含 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 不支持接口(interface)。

最佳答案

您可以将 @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);
}

}

关于java - 如何在 Jackson 中使用自定义序列化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7161638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com