All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xkcoding.json.support.fastjson.FastJsonJsonAdapter Maven / Gradle / Ivy

There is a newer version: 0.0.3
Show newest version
package com.xkcoding.json.support.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xkcoding.json.config.JsonConfig;
import com.xkcoding.json.exception.SimpleJsonException;
import com.xkcoding.json.support.AbstractJsonAdapter;
import com.xkcoding.json.util.StringUtil;

/**
 * 

* FastJson 序列化实现,时间格式化可以通过 {@link JsonConfig} 设置,也可以通过 {@link com.alibaba.fastjson.annotation.JSONField} 设置 *

* * @author yangkai.shen * @date Created in 2021-01-20 15:12 */ public class FastJsonJsonAdapter extends AbstractJsonAdapter { public FastJsonJsonAdapter() { this(new JsonConfig()); } public FastJsonJsonAdapter(JsonConfig jsonConfig) { super(jsonConfig); } /** * 序列化 * * @param obj 对象 * @return json 字符串 * @throws SimpleJsonException 自定义异常 */ @Override public String serialize(Object obj) throws SimpleJsonException { if (StringUtil.isNotEmpty(jsonConfig.getDateFormat())) { JSON.DEFFAULT_DATE_FORMAT = jsonConfig.getDateFormat(); return JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat); } return JSON.toJSONString(obj); } /** * 反序列化 * * @param jsonStr json 字符串 * @param clazz 对象类型 * @return 对象 * @throws SimpleJsonException 自定义异常 */ @Override public T deserialize(String jsonStr, Class clazz) throws SimpleJsonException { return JSON.parseObject(jsonStr, clazz); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy