业务场景
目的
想把所有的 value 都序列化为字符串,而不是 map 或者 object。
实现方式
xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import java.util.HashMap;
import java.util.Map;
public class JsonTest {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("name", "123");
Map<String, Object> userInfo = new HashMap<>();
userInfo.put("age", 10);
Map<String, Object> hobbyInfo = new HashMap<>();
hobbyInfo.put("love", "it");
userInfo.put("hobby", hobbyInfo);
map.put("user", userInfo);
// 自定义 ValueFilter 将所有非字符串类型的值转换为 JSON 字符串
ValueFilter valueFilter = (obj, s, v) -> {
if (v instanceof Map || v instanceof Object) {
return JSON.toJSONString(v); // 将 Map 或对象序列化为 JSON 字符串
}
return v; // 保留其他类型的原始值
};
System.out.println(JSON.toJSONString(map));
System.out.println(JSON.toJSONString(map, SerializerFeature.WriteNonStringValueAsString));
System.out.println(JSON.toJSONString(map, valueFilter));
}
}
效果
{"name":"123","user":{"age":10,"hobby":{"love":"it"}}}
{"name":"123","user":{"age":"10","hobby":{"love":"it"}}}
{"name":"\"123\"","user":"{\"age\":10,\"hobby\":{\"love\":\"it\"}}"}