Java fasterxml使用toString进行序列化,并使用字符串构造函数进行反序列化

Java fasterxml使用toString进行序列化,并使用字符串构造函数进行反序列化,java,json,dictionary,fasterxml,Java,Json,Dictionary,Fasterxml,我有一个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 pu

我有一个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
)的键,该映射在序列化为json时,使用对象的toString()表示作为键,在反序列化时,使用字符串构造函数。这是否可能使用类似jackson数据绑定的简单注释?解决这个问题的最佳方法是什么?

您可以创建一个新类,扩展JsonSerializer并重写其序列化方法。将本应在toString()方法中编写的实现写入JsonGenerator的writeString()方法,如图所示。您必须在项目中使用jackson-core-asl.jar和jackson-mapper-asl.jar。下面是JsontingSerializer.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());
}

}
您可以使用下面的代码测试您的代码

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();
        }

    }

}
输出为:

“我想要这种方式..我想通过实验在toString()方法10内部实现它”(我认为文档中可以更清楚地说明这一点)我发现我可以使用
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() ...

}

我发现有一个更简单的方法来实现这一点。请参阅我的答案。如果您想序列化/反序列化一个成员,但又不控制该成员类的源代码,这将非常有帮助(但您需要使用
JsonSerialize
JsonSerialize
来注释该成员).高效的方法你可能会认为这样一个可爱的节省时间的方法应该是粗体的,但遗憾的是,编写好的文档的能力似乎是一种正在消亡的技能。
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() ...

}