添加小编微信:372787553,备注进群!
Fastjson 是阿里开源的Json类库,功能很强大,使用也很简单,不得不说温少还是很厉害的,但是fastjson却经常会暴露出高危的漏洞,每次都是紧急发版,导致项目上经常会去更新,这一点让使用者有点痛苦!
json的类库还是很多的,比如:Gosn,JSON-B,Jackson,这三种json类库也是Springboot官方推荐的,如图:
但是这三者的使用,却没有Fastjson那么方便,接下就进入我们今天的主角:Jackson
Jackson是一个简单基于Java应用库,Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。Jackson所依赖的jar包较少,简单易用并且性能也要相对高些,并且Jackson社区相对比较活跃,更新速度也比较快。
提供了三种不同的方法来处理JSON
流式API - 读取并将JSON内容写入作为离散事件。 JsonParser读取数据,而JsonGenerator写入数据。它是三者中最有效的方法,是最低的开销和最快的读/写操作。它类似于Stax解析器XML。
树模型 - 准备JSON文件在内存里以树形式表示。 ObjectMapper构建JsonNode节点树。这是最灵活的方法。它类似于XML的DOM解析器。
数据绑定 - 转换JSON并从POJO(普通Java对象)使用属性访问或使用注释。它有两个类型。
简单的数据绑定 - 转换JSON和Java Maps, Lists, Strings, Numbers, Booleans 和null 对象。
全部数据绑定 - 转换为JSON从任何JAVA类型。
ObjectMapper读/写JSON两种类型的数据绑定。数据绑定是最方便的方式是类似XML的JAXB解析器。
ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象匹配JSON结构,反之亦然。它使用JsonParser和JsonGenerator的实例实现JSON实际的读/写
本案例以Springboot项目为例,Spring boot已经自带了Jackson依赖,无需额外引用,接下类我们实现两个常用的场景:Bean To Json , Json To Bean
@Test
public void beanToJson() throws JsonProcessingException {
//创建mapper
ObjectMapper mapper = new ObjectMapper();
User user = new User("123", "123",13);
//bean to json
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));
}
@Test
public void jsonToBean() throws JsonProcessingException {
//创建mapper
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"name\":\"haiji\", \"age\":21}";
// json to bean
System.out.println(mapper.readValue(jsonString, User.class));
}
在上面的案例中我们引进发现,每次使用时都需要创建一个ObjectMapper,而且使用起来也很多的冗余代码,这样我们仅需要进一步优化,如下步骤:
@Configuration
public class JacksonConfig {
private final static Logger logger = LoggerFactory.getLogger(JacksonConfig.class);
private static ObjectMapper objectMapper;
private static Lock LOCK = new ReentrantLock(true);
private static AtomicInteger TIME= new AtomicInteger(60);
private final static Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public static ObjectMapper jacksonObjectMapper() {
if(ObjectUtils.isEmpty(objectMapper)){
try {
if(LOCK.tryLock(TIME.get(), TimeUnit.SECONDS) && ObjectUtils.isEmpty(objectMapper)){
objectMapper= builder.createXmlMapper(false).build();
// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
// Include.Include.ALWAYS 默认
// Include.NON_DEFAULT 属性为默认值不序列化
// Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
// Include.NON_NULL 属性为NULL 不序列化
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 允许出现特殊字符和转义符
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
// 允许出现单引号
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// 字段保留,将null值转为""
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
{
@Override
public void serialize(Object o, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException
{
jsonGenerator.writeString("");
}
});
}
} catch (InterruptedException e) {
logger.error("object mapper try lock ",e);
}finally {
LOCK.unlock();
}
}
return objectMapper;
}
}
public class JsonUtils<T>{
private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);
public static <T> T jsonToBean(@Nullable String string, Class<T> bean){
assertNotNull("json to bean ", string);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(string, bean);
} catch (JsonProcessingException e) {
logger.info("json to object is error -> JsonProcessingException ",e);
}
return t;
}
public static <T> String beanToJson(@Nullable T bean){
assertNotNull("bean to json ",bean);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
String t = null;
try {
t = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
} catch (JsonProcessingException e) {
logger.info("json to object is error -> JsonProcessingException ",e);
}
return t;
}
public static <T> T jsonToBean(@Nullable String json, TypeReference<T> typeReference){
assertNotNull("to json generics",json);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(json, typeReference);
} catch (IOException e) {
logger.error("json to object is exception -> ",e);
}
return t;
}
public static <T> void beanToJson(@Nullable File file, T bean){
assertNotNull("to file",file);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
try {
objectMapper.writeValue(file,bean);
} catch (IOException e) {
logger.info("json to object is error -> IOException ",e);
}
}
public static <T> T jsonToBean(@Nullable File file, TypeReference<T> typeReference){
assertNotNull("to file",file);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(file,typeReference);
} catch (IOException e) {
logger.error("json to object exception -> ",e);
}
return t;
}
public static <T> JsonNode jsonToTree(@Nullable String json){
assertNotNull("to tree",json);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
JsonNode jsonNode =null;
try {
jsonNode = objectMapper.readTree(json);
} catch (JsonProcessingException e) {
logger.error("json to tree exception ->",e);
}
return jsonNode;
}
public static <T> T jsonToBean(@Nullable URL url, Class<T> bean){
assertNotNull("url",url);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(url, bean);
} catch (IOException e) {
logger.info("url to object is error -> IOException ",e);
}
return t;
}
public static <T> T jsonToBean(@Nullable URL url, TypeReference<T> bean){
assertNotNull("url generic paradigm",url);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(url, bean);
} catch (IOException e) {
logger.info("url to object is error -> IOException ",e);
}
return t;
}
public static <T> T inputStreamToBean(@Nullable InputStream inputStream, Class<T> bean){
assertNotNull("inputStream class",inputStream);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(inputStream, bean);
} catch (IOException e) {
logger.info("url to object is error -> IOException ",e);
}
return t;
}
public static <T> T inputStreamToBean(@Nullable InputStream inputStream, TypeReference<T> bean){
assertNotNull("inputStream",inputStream);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(inputStream, bean);
} catch (IOException e) {
logger.info("url to object is error -> IOException ",e);
}
return t;
}
public static <T> T byteToBean(@Nullable byte[] src, Class<T> bean){
assertNotNull("byte class",src);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(src, bean);
} catch (IOException e) {
logger.info("url to object is error -> IOException ",e);
}
return t;
}
public static <T> T byteToBean(@Nullable byte[] src, TypeReference<T> bean){
assertNotNull("byte TypeReference",src);
ObjectMapper objectMapper = JacksonConfig.jacksonObjectMapper();
T t = null;
try {
t = objectMapper.readValue(src, bean);
} catch (IOException e) {
logger.info("url to object is error -> IOException ",e);
}
return t;
}
protected static void assertNotNull(String paramName, Object src) {
if (ObjectUtils.isEmpty(src)) {
throw new IllegalArgumentException(String.format("argument \"%s\" is null", paramName));
}
}
}
@Test
public void testJson(){
User user = new User("123", "123",13);
System.out.println(JsonUtils.beanToJson(user));
String jsonString = "[{\"name\":\"haiji\", \"age\":21},{\"name\":\"java\", \"age\":21}]";
System.out.println(JsonUtils.jsonToBean(jsonString, new TypeReference<List<User>>() {}));
}
这里只是对常用的一些json工具进行了封装,大家也可以自己进行
案例源代码地址:https://github.com/yanghaiji/javayh-platform/tree/master/javayh-dependencies/javayh-common-starter/src/main/java/com/javayh/common
如果对你有帮助,请给个赞吧!
本文的分享暂时就到这里,希望对您有所帮助
关注 Java有货领取更多资料
联系小编。微信:372787553,带您进群互相学习
左侧小编微信,右侧获取免费资料