从json-lib转成jackson的遇到的问题
从json-lib转成jackson的遇到的问题
问题一:json 字符串,再经过Jackson序列化之后就变成原生字符串了。而json-lib经过再序列化之后,还是json格式的串。
针对这种情况,可以写一个Serializer类,遇到json串的时候就当作原生字符串写入即可。
<<JsonStringSerializer>>
/**
* 序列化时,对Json格式的字符串做特殊处理:不用引号括起来
* @author
*
*/
public class JsonStringSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value == null) {
jgen.writeNull();
} else {
if (value instanceof String) {
String newValue = ((String) value).trim();
// 只对Json格式的字符串做处理
if (newValue.startsWith("{") || newValue.startsWith("[")) {
jgen.writeRawValue((String) value);
} else {
jgen.writeObject(value);
}
} else {
jgen.writeObject(value);
}
}
}
}
问题二、jackson和json-lib对null值的处理大不相同。对于值为null的字符串类型的字段,jackson输出null,而json-lib输出空字符串。对于List类型,json-lib输出空列表[],而jackson还是输出null。
如果是从json-lib移植到jackson,为了兼容老代码,可以写一个SerializerProvider,遇到null值输出空字符串等。
<<NullToEmptyStringProvider>>
/**
* Customize the DefaultSerializerProvider so that when it is looking for a
* NullSerializer it will use one that is class sensitive, writing strings as ""
* and everything else using the default value.
*
* @author
*/
public class NullToEmptyStringProvider extends DefaultSerializerProvider {
private static final long serialVersionUID = -1L;
// A couple of constructors and factory methods to keep the compiler happy
public NullToEmptyStringProvider() {
super();
}
public NullToEmptyStringProvider(NullToEmptyStringProvider provider, SerializationConfig config,
SerializerFactory jsf) {
super(provider, config, jsf);
}
@Override
public NullToEmptyStringProvider createInstance(SerializationConfig config, SerializerFactory jsf) {
return new NullToEmptyStringProvider(this, config, jsf);
}
@Override
public JsonSerializer<Object> findNullValueSerializer(BeanProperty property) throws JsonMappingException {
if (property.getType().getRawClass().equals(String.class)) {
return EmptyStringSerializer.INSTANCE;
} else if ((property.getType().isArrayType() || property.getType().isCollectionLikeType())
&& !property.getType().isMapLikeType()) {
return EmptyListSerializer.INSTANCE;
} else if (property.getType().getRawClass().equals(Long.class)
|| property.getType().getRawClass().equals(Short.class)
|| property.getType().getRawClass().equals(Integer.class)
|| property.getType().getRawClass().equals(Double.class)
|| property.getType().getRawClass().equals(Float.class)
|| property.getType().getRawClass().equals(BigDecimal.class)) {
return EmptyNumberSerializer.INSTANCE;
} else {
return super.findNullValueSerializer(property);
}
}
}
/**
* Output null of String to empty string.
*
* @author
*
*/
class EmptyStringSerializer extends JsonSerializer<Object> {
public static final JsonSerializer<Object> INSTANCE = new EmptyStringSerializer();
private EmptyStringSerializer() {
}
// Since we know we only get to this seralizer in the case where the value
// is null and the type is String, we can
// do our handling without any additional logic and write that empty string
// we are so desperately wanting.
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeString("");
}
}
/**
* For null list
* @author
*
*/
class EmptyListSerializer extends JsonSerializer<Object> {
public static final JsonSerializer<Object> INSTANCE = new EmptyListSerializer();
private EmptyListSerializer() {
}
// Since we know we only get to this seralizer in the case where the value
// is null and the type is String, we can
// do our handling without any additional logic and write that empty string
// we are so desperately wanting.
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartArray();
jsonGenerator.writeEndArray();
}
}
/**
* For null Number, such as Integer, Long, Short ....
* @author
*
*/
class EmptyNumberSerializer extends JsonSerializer<Object> {
public static final JsonSerializer<Object> INSTANCE = new EmptyNumberSerializer();
private EmptyNumberSerializer() {
}
// Since we know we only get to this seralizer in the case where the value
// is null and the type is String, we can
// do our handling without any additional logic and write that empty string
// we are so desperately wanting.
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeNumber(0);
}
}
最后,需要把这个provider配置到jackson的mapper实例中。
jsonMapper.setSerializerProvider(new NullToEmptyStringProvider());
从json-lib转成jackson的遇到的问题的更多相关文章
- 使用jackson来进行数组格式的json字符串转换成List。
有一个字符串如下.如下,也是通过jackson把list转换成的json字符串,我想把它转过来,看网上的内容都不尽人如意,都是片断的内容.估计只有写的知道怎么使用,所以就直接看了jackson的官网, ...
- Atitit.json类库的设计与实现 ati json lib
Atitit.json类库的设计与实现 ati json lib 1. 目前jsonlib库可能有问题,可能版本冲突,抛出ex1 2. 解决之道:1 2.1. 自定义json解析库,使用多个复合的js ...
- Java json工具类,jackson工具类,ObjectMapper工具类
Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...
- 使用JsonConfig控制JSON lib序列化
将对象转换成字符串,是非常常用的功能,尤其在WEB应用中,使用 JSON lib 能够便捷地完成这项工作.JSON lib能够将Java对象转成json格式的字符串,也可以将Java对象转换成xml格 ...
- (转)json格式转换成javaBean对象的方法
把json格式转换成javaBean才可以.于是查了一下资料,网上最多的资料就是下面的这种方式: Java code? 1 2 3 4 5 6 7 8 9 String str = "[{\ ...
- js 将json对象转成字符串
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 前台 JSON对象转换成字符串 相互转换 的几种方式
在最近的工作中,使用到JSON进行数据的传递,特别是从前端传递到后台,前台可以直接采用ajax的data函数,按json格式传递,后台Request即可,但有的时候,需要传递多个参数,后台使用requ ...
- C#中服务端接受前端JSON字符串转换成字典集合
我们是否可以把从前端接受的JSON字符串转换成字典集合呢? 比如从前端接收:{'size':'10', 'weight':'10kg'} 在服务端转换成:[{size:"10"}, ...
- json字符串转成 Map/List
package jsonToMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import n ...
随机推荐
- PostgreSQL自学笔记:9 索引
9 索引 9.1 索引简介 索引是对数据库表中一列或多列值进行排序的一种结构,使用 索引可提高数据库中特定数据的查询速度 9.1.1 索引的含义和特点 索引是一种单独的.存储在磁盘上的数据库结构,他们 ...
- CodeForces - 1013B And 与运算暴力
题目链接: https://vjudge.net/problem/1735275/origin 基本思路: 本题思路比较简单,首先,我们知道 a & x = b, b & x = b; ...
- selenium基础实例学习
在这里我们通过selenium官方文档做给的实例以及翻译,做出如果代码注释 from selenium import webdriverfrom selenium.webdriver.common ...
- ServletRegistrationBean的源码摘要
感觉ServletRegistrationBean在Springboot中是一个可以看懂的类,好像作用就相当于@Controoller注解, package org.springframework.b ...
- vue组件创建学习总结
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ng7 设置http proxy
看文档 proxy.conf.json { "/api": { "target": "http://localhost:5000", &qu ...
- Oracle 备份与恢复
在进行生产服务器升级.或更换数据库服务器.搭建测试环境时,需要对生产数据库进行备份以及将来可能的还原. 1.expdp导出 expdp DMS version directory=DATA_PUMP_ ...
- linux for循环 fork() 产生子进程
#include <sys/types.h> #include <unistd.h> #include<stdio.h> int main() { for(int ...
- python连接服务器上传文件,后台执行命令
上传文件 import os import paramikoimport logging from django.core.cache import cache from YunTai import ...
- 使用Eureka作为springcloud的注册机
使用springcloud做项目的负载均衡,需要导的jar这里不再显示,具体配置如下: 作为被注册服务配置: 启动多台服务端就可以实现集群,相应的localhost需要转成真实的ip 当然一个项目还要 ...