How can I serialize a property which is a Map as a List of the Map's values? I've been able to do other simple conversions using the @JsonSerialize(using=...)
annotation on the getter. However, I am not sure if one exists for what I want to do.
如何将映射的属性序列化为映射值的列表?我可以使用getter上的@JsonSerialize(使用=…)注释进行其他简单的转换。然而,我不确定是否存在一个我想做的事情。
2 个解决方案
#1
7
We needed something similar, in our case we used a customized @JsonSerialize
as you commented, and it was stupid simple:
我们需要类似的东西,在我们的例子中,我们使用了定制的@JsonSerialize,就像你评论的那样,它是愚蠢的简单:
public class MyCustomSerializer extends JsonSerializer<Map<?, ?>> {
@Override
public void serialize(final Map<?, ?> value, final JsonGenerator jgen, final SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeObject(value.values());
}
}
Code using it:
代码使用它:
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;
public class JacksonTest {
public static class ModelClass {
private final Map<String, String> map;
public ModelClass(final Map<String, String> map) {
super();
this.map = map;
}
@JsonSerialize(using = MyCustomSerializer.class)
public Map<String, String> getMap() {
return map;
}
}
public static void main(final String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(System.out, new ModelClass(Collections.singletonMap("test", "test")));
}
}
#2
3
I implemented using default Serializer to handle values that are not just String :
我实现了使用默认序列化器来处理不只是字符串的值:
@Override
public void serialize(final Map<Long, ?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException,
JsonProcessingException {
provider.defaultSerializeValue(value.values(), jgen);
}
EDIT : As mentioned by Radu Simionescu this solution only works for Maps of Pojos.
编辑:正如Radu Simionescu提到的,这个解决方案只适用于pojo地图。
#1
7
We needed something similar, in our case we used a customized @JsonSerialize
as you commented, and it was stupid simple:
我们需要类似的东西,在我们的例子中,我们使用了定制的@JsonSerialize,就像你评论的那样,它是愚蠢的简单:
public class MyCustomSerializer extends JsonSerializer<Map<?, ?>> {
@Override
public void serialize(final Map<?, ?> value, final JsonGenerator jgen, final SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeObject(value.values());
}
}
Code using it:
代码使用它:
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;
public class JacksonTest {
public static class ModelClass {
private final Map<String, String> map;
public ModelClass(final Map<String, String> map) {
super();
this.map = map;
}
@JsonSerialize(using = MyCustomSerializer.class)
public Map<String, String> getMap() {
return map;
}
}
public static void main(final String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(System.out, new ModelClass(Collections.singletonMap("test", "test")));
}
}
#2
3
I implemented using default Serializer to handle values that are not just String :
我实现了使用默认序列化器来处理不只是字符串的值:
@Override
public void serialize(final Map<Long, ?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException,
JsonProcessingException {
provider.defaultSerializeValue(value.values(), jgen);
}
EDIT : As mentioned by Radu Simionescu this solution only works for Maps of Pojos.
编辑:正如Radu Simionescu提到的,这个解决方案只适用于pojo地图。