如何使用Jackson将Color java类解析为JSON? [英] How I parse Color java class to JSON with Jackson?
问题描述
我试图用Jackson
从JSON
反序列化Color
类,但是它抛出异常:
I am trying to deserialise the Color
class from JSON
with Jackson
but it throws exception:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 无法识别的字段"colorSpace"(类java.awt.Color),未标记为 可忽略的.
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "colorSpace" (class java.awt.Color), not marked as ignorable.
我做错了什么? 这是我的代码:
What i'm doing wrong? This is my code:
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);
这是我的人物课程:
public class person implements Serializable {
//it include getters, setters and builder
String nombe = "Pepe";
String CI = "12345678978";
Color c = Color.red;
}
推荐答案
java.awt.Color
类不是常规的POJO
或Enum
.如果要以JSON
格式存储,则需要实现自定义序列化程序和反序列化程序. Color
类可以用其RGB
表示形式表示,并且可以将其存储为数字:
java.awt.Color
class is not a regular POJO
or Enum
. You need to implement custom serialiser and deserialiser if you want to store it in JSON
format. Color
class can be represented by its RGB
representation and you can store it as a number:
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
对象:
Take a look on similar question where Color
is stored as JSON
object:
这篇关于如何使用Jackson将Color java类解析为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!