使用jackson进行序列化和反序列化



我试图将我的对象转换为json,但默认的序列化器,jackson的反序列化器不起作用。

我怎样才能使它工作?我知道我可能需要编写自定义序列化器,反序列化器。我该怎么做呢?是否有一些注释添加代码将工作?

对象:

@JsonDeserialize(keyUsing = mypairDeserializer.class)
@JsonSerialize(keyUsing = mypairSerializer.class)
HashMap<Set < Mypair > , List < Mypair > > obj;

public class ConditionSerializer extends JsonSerializer<Collection<mypair>> {
@Override
public void serialize(final Collection<mypair> conditionSet, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("Pair");
jsonGenerator.writeStartArray();
for(final Condition condition: conditionSet) {
jsonGenerator.writeString(mypair.toString());
}
jsonGenerator.writeEndArray();
jsonGenerator.writeEndObject();
}
}

public class mypairDeserializer extends KeyDeserializer {
ObjectMapper mapper = new ObjectMapper();
@Override
public Collection<mypair> deserializeKey(final String key, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
// return new mypair(key);
return mapper.readValue(key, Collection.class);
}
}

大家好,又来了

所以,这是你可以做的一个例子:

注意,因为我不知道你的对象Mypair是什么,我用User类做了这个例子:

public class User {
private int id;
private String name;
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
// getters & setters
}

包含复杂对象的类:

public class YourClass {
@JsonSerialize(using = ComplexObjectSerializer.class)
private Map<Set<User>, List<User>> object;
public YourClass(Map<Set<User>, List<User>> object) {
this.object = object;
}
public Map<Set<User>, List<User>> getObject() {
return object;
}
public void setObject(Map<Set<User>, List<User>> object) {
this.object = object;
}
}

自定义序列化器:

public class ComplexObjectSerializer extends StdSerializer<Map<Set<User>, List<User>>> {
public ComplexObjectSerializer() {
this(null);
}
public ComplexObjectSerializer(Class<Map<Set<User>, List<User>>> t) {
super(t);
}
private static final long serialVersionUID = 1L;
@Override
public void serialize(Map<Set<User>, List<User>> complexObject,
JsonGenerator jsonGen, SerializerProvider arg2) throws IOException {
// Suppose you want the following json:
/**
* [ { "set":[], "list":[] } ]
*/
jsonGen.writeStartArray(); // [
for (Entry<Set<User>, List<User>> entry : complexObject.entrySet()) {
jsonGen.writeStartObject(); // {
jsonGen.writeObjectField("set", entry.getKey()); // It will call the default serializer for a Set<User>, ie : [ {"id": 0, "name":"string"} ]
jsonGen.writeObjectField("list", entry.getValue()); // It will call the default serializer for a List<User>, ie the same thing as the Set above
jsonGen.writeEndObject(); // }
}
jsonGen.writeEndArray(); // ]
}
}

主:

Map<Set<User>, List<User>> complexObject = new HashMap<Set<User>, List<User>>();
// Add some data in the map ...
YourClass yourClass = new YourClass(complexObject);
// Serialize your object
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(yourClass); // It will call your custom serializer
System.out.println(json);

输出:

{
"object": [
{
"set": [
{
"id": 5,
"name": "userName5"
},
{
"id": 6,
"name": "userName6"
}
],
"list": [
{
"id": 2,
"name": "userName2"
}
]
},
{
"set": [
{
"id": 4,
"name": "userName4"
},
{
"id": 3,
"name": "userName3"
}
],
"list": [
{
"id": 0,
"name": "userName0"
},
{
"id": 1,
"name": "userName1"
}
]
}
]
}

相关内容

  • 没有找到相关文章

最新更新