FastJson对JodaTime的处理
来源:互联网 发布:漫画控不能连接网络 编辑:程序博客网 时间:2024/06/05 20:50
当POJO中时间用的是JodaTime时,用FastJson转换时会完全解散,所以需要一个转换器。
仿照com.alibaba.fastjson.serializer.SimpleDateFormatSerializer
import com.alibaba.fastjson.serializer.JSONSerializer;import com.alibaba.fastjson.serializer.ObjectSerializer;import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import java.io.IOException;import java.lang.reflect.Type;public class JodaTimeSerializer implements ObjectSerializer { private final String pattern; public JodaTimeSerializer(String pattern) { this.pattern = pattern; } @Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { if (object == null) { serializer.out.writeNull(); return; } final DateTime date = (DateTime) object; serializer.write(date.toString(DateTimeFormat.forPattern(pattern))); }}
FastJson工具类
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.alibaba.fastjson.serializer.SerializeConfig;import com.alibaba.fastjson.serializer.SerializerFeature;import org.joda.time.DateTime;import java.util.List;import java.util.Map;import javafx.animation.KeyValue;public class FastJsonUtils { private static final SerializeConfig config; static { config = new SerializeConfig(); config.put(DateTime.class, new JodaTimeSerializer("yyyy-MM-dd'T'HH:mm:ss.SSSZ")); } private static final SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, // 输出空置字段 SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null }; public static String toJsonString(Object object) { return JSON.toJSONString(object, config, features); } public static String toJsonNoFeatures(Object object) { return JSON.toJSONString(object, config); } public static Object toBean(String text) { return JSON.parse(text); } public static <T> T toBean(String text, Class<T> clazz) { return JSON.parseObject(text, clazz); } // 转换为数组 public static <T> Object[] toArray(String text) { return toArray(text, null); } // 转换为数组 public static <T> Object[] toArray(String text, Class<T> clazz) { return JSON.parseArray(text, clazz).toArray(); } // 转换为List public static <T> List<T> toList(String text, Class<T> clazz) { return JSON.parseArray(text, clazz); } /** * 将javabean转化为序列化的json字符串 */ public static Object beanToJson(KeyValue keyvalue) { String textJson = JSON.toJSONString(keyvalue); Object objectJson = JSON.parse(textJson); return objectJson; } /** * 将string转化为序列化的json字符串 */ public static Object textToJson(String text) { Object objectJson = JSON.parse(text); return objectJson; } /** * json字符串转化为map */ public static Map stringToCollect(String s) { Map m = JSONObject.parseObject(s); return m; } /** * 将map转化为string */ public static String collectToString(Map m) { String s = JSONObject.toJSONString(m); return s; }}
阅读全文
0 0
- FastJson对JodaTime的处理
- fastjson对Date的处理
- fastjson对Date的处理
- 浅谈jodaTime 的使用
- Jodatime
- jodaTime 优秀的日期框架
- FastJSON处理Map的问题
- JodaTime时间处理工具使用详解
- 使用FastJson对数据的常用操作
- 阿里巴巴FastJson对数据的常用操作
- Json的处理(阿里巴巴的fastjson)
- fastjson 对于json数组字符串的处理
- scala 调用 Java 的 fastjson 处理 json
- Gson&FastJson解析异常Json的处理
- fastjson开发学习(二)---对返回的json进行解析
- 【Java】消除fastjson对同一对象循环引用的问题
- maven中pom.xml对fastjson的依赖
- Java对【JSON数据的解析】--fastjson解析法
- tomcat服务已经开启,自己可以访问但是同网段其他机器无法访问解决办法
- eclipse环境import工程出现中文乱码问题的解决方案
- springboot利用springsession实现redis共享session,并且自定义sessionid
- xml4种解析方法性能比较
- 测试计划2
- FastJson对JodaTime的处理
- 整合Spring时Service层为什么不做全局包扫描详解
- 服务器端设计的思想、方法及应用
- Java微信支付全教程demo【公众号支付】
- live555 调优总结
- turtlebot3_Python脚本控制运行
- C++ 获取exe路径
- 7. 并发模式(Go Tutorial)
- 架构architecture篇-----1.docker命令