我有一个POJO,看起来像这样:
public class Thing
{
private final int x;
private final int y;
private final int z;
public Thing(String strThing)
{
// parse strThing which is in some arbitrary format to set x, y and z
}
@Override
public String toString()
{
// return a string representation of thing
// (same format as that parsed by the constructor)
}
@Override
public boolean equals(Object obj) ...
@Override
public int hashCode() ...
}
,我想使用它作为一个映射(例如HashMap<Thing, SomeOtherPOJO>
)的键,当序列化到json时,使用toString()表示的Thing作为键,当反序列化时,使用String构造函数。是否可以使用jackson数据绑定注释之类的简单方法?解决这个问题的最好方法是什么?
通过实验(我认为文档可以更清楚一点),我发现我可以在String
构造器上使用JsonCreator
注释,在toString()
方法上使用JsonValue
来实现我想要的:
public class Thing
{
private final int x;
private final int y;
private final int z;
@JsonCreator
public Thing(String strThing)
{
// parse strThing which is in some arbitrary format to set x, y and z
}
@Override
@JsonValue
public String toString()
{
// return a string representation of thing
// (same format as that parsed by the constructor)
}
@Override
public boolean equals(Object obj) ...
@Override
public int hashCode() ...
}
你可以创建一个新的类来扩展JsonSerializer并覆盖它的serialize方法。将您应该在toString()方法中编写的实现写入JsonGenerator的writeString()方法中,如图所示。你必须在你的项目中使用jackson-core-asl.jar和jackson-mapper-asl.jar。下面是JsonThingSerializer.java类
import java.io.IOException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
public class JsonThingSerializer extends JsonSerializer<Thing>{ //note use of generics
@Override
public void serialize(Thing myThing, JsonGenerator gen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
gen.writeString("I want this way.. which I was thinking to implement inside toString() method "+" "+myThing.getX()+" "+myThing.getY()+" "+myThing.getZ());
}
}
在你的Thing.java中使用下面的注释
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonAutoDetect
@JsonSerialize(using=JsonThingSerializer.class)
public class Thing
{
private final int x;
private final int y;
private final int z;
public Thing(String strThing)
{
// parse strThing which is in some arbitrary format to set x, y and z
//insert your own implementation to get x, y and z
x=y=z=10;
}
@Override
public String toString()
{
//no need to override this for json serialization.. mapper will not use it
}
@Override
public boolean equals(Object obj){
//you can have your own implementation
}
@Override
public int hashCode() {
//you can have your own implementation
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
}
你可以使用下面的代码来测试你的代码。
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class TestJsonSerialization {
public static void main(String[] args) {
Thing thing =new Thing("your strThing which is in some arbitrary format to set x, y and z");
ObjectMapper mapper =new ObjectMapper();
try {
String thingString = mapper.writeValueAsString(thing);
System.out.println(thingString);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Blow为输出:
"我想这样……"我想在toString()方法中实现10 10 10"