我如何使用 Jackson 将 Color java class 解析为 JSON?
How I parse Color java class to JSON with Jackson?
我正在尝试使用 Jackson
从 JSON
反序列化 Color
class 但它抛出异常:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "colorSpace" (class java.awt.Color), not marked as
ignorable.
我做错了什么?
这是我的代码:
File act = new File(new File().getAbsolutePath());
ObjectMapper om = new ObjectMapper();
File f = new File(act, "123.JSON");
om.writeValue(f, new person());
person per = om.readValue(f, person.class);
System.out.println(per);
这是我的人class:
public class person implements Serializable {
//it include getters, setters and builder
String nombe = "Pepe";
String CI = "12345678978";
Color c = Color.red;
}
java.awt.Color
class 不是常规的 POJO
或 Enum
。如果要以 JSON
格式存储它,则需要实现自定义序列化器和反序列化器。 Color
class 可以用它的 RGB
表示来表示,你可以把它存储为一个数字:
class ColorJsonSerializer extends JsonSerializer<Color> {
@Override
public void serialize(Color value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
return;
}
gen.writeNumber(value.getRGB());
}
}
class ColorJsonDeserializer extends JsonDeserializer<Color> {
@Override
public Color deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return new Color(p.getValueAsInt());
}
}
简单用法:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.awt.*;
import java.io.IOException;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
SimpleModule awtModule = new SimpleModule("AWT Module");
awtModule.addSerializer(Color.class, new ColorJsonSerializer());
awtModule.addDeserializer(Color.class, new ColorJsonDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(awtModule);
String json = mapper.writeValueAsString(new Person());
System.out.println(json);
System.out.println(mapper.readValue(json, Person.class));
}
}
以上代码打印:
{"nombe":"Pepe","c":-65536,"ci":"12345678978"}
Person{nombe='Pepe', CI='12345678978', c=java.awt.Color[r=255,g=0,b=0]}
查看类似问题,其中 Color
存储为 JSON
对象:
我正在尝试使用 Jackson
从 JSON
反序列化 Color
class 但它抛出异常:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "colorSpace" (class java.awt.Color), not marked as ignorable.
我做错了什么? 这是我的代码:
File act = new File(new File().getAbsolutePath());
ObjectMapper om = new ObjectMapper();
File f = new File(act, "123.JSON");
om.writeValue(f, new person());
person per = om.readValue(f, person.class);
System.out.println(per);
这是我的人class:
public class person implements Serializable {
//it include getters, setters and builder
String nombe = "Pepe";
String CI = "12345678978";
Color c = Color.red;
}
java.awt.Color
class 不是常规的 POJO
或 Enum
。如果要以 JSON
格式存储它,则需要实现自定义序列化器和反序列化器。 Color
class 可以用它的 RGB
表示来表示,你可以把它存储为一个数字:
class ColorJsonSerializer extends JsonSerializer<Color> {
@Override
public void serialize(Color value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
return;
}
gen.writeNumber(value.getRGB());
}
}
class ColorJsonDeserializer extends JsonDeserializer<Color> {
@Override
public Color deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return new Color(p.getValueAsInt());
}
}
简单用法:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.awt.*;
import java.io.IOException;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
SimpleModule awtModule = new SimpleModule("AWT Module");
awtModule.addSerializer(Color.class, new ColorJsonSerializer());
awtModule.addDeserializer(Color.class, new ColorJsonDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(awtModule);
String json = mapper.writeValueAsString(new Person());
System.out.println(json);
System.out.println(mapper.readValue(json, Person.class));
}
}
以上代码打印:
{"nombe":"Pepe","c":-65536,"ci":"12345678978"}
Person{nombe='Pepe', CI='12345678978', c=java.awt.Color[r=255,g=0,b=0]}
查看类似问题,其中 Color
存储为 JSON
对象: