d
import java.io.IOException; import org.apache.commons.lang.time.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonTokenId; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer; /** * 支持多种日期格式的解析 */ @SuppressWarnings("deprecation") class CustomDateDeseralizer extends UntypedObjectDeserializer { Logger logger = LoggerFactory.getLogger(getClass()); private static final long serialVersionUID = -2275951539867772400L; @Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { if (jp.getCurrentTokenId() == JsonTokenId.ID_STRING) { try { return DateUtils.parseDate(jp.getText(), new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy", "yyyy.MM.dd G 'at' HH:mm:ss z", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz"}); } catch (Exception e) { logger.error(e.getMessage(), e); return super.deserialize(jp, ctxt); } } else { return super.deserialize(jp, ctxt); } } }
import java.text.SimpleDateFormat; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.module.SimpleModule; /** * json与java对象之间转化器 * * @version 1.0.0 * @since 2014年10月21日 下午3:11:39 */ public final class JsonObjectMapper extends ObjectMapper { /** */ private static final long serialVersionUID = 4563671462132723274L; public JsonObjectMapper() { super(); // 从JSON到java object // 没有匹配的属性名称时不作失败处理 this.configure(MapperFeature.AUTO_DETECT_FIELDS, true); // 反序列化 // 禁止遇到空原始类型时抛出异常,用默认值代替。 this.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false); this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); // 禁止遇到未知(新)属性时报错,支持兼容扩展 this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); this.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); this.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); // 按时间戳格式读取日期 // this.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); this.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); this.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); // 序列化 // 禁止序列化空值 this.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); this.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); this.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true); // 按时间戳格式生成日期 // this.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); this.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, true); this.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true); // 不包含空值属性 this.setSerializationInclusion(Include.NON_EMPTY); this.setSerializationInclusion(Include.NON_NULL); // this.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true); // 是否缩放排列输出,默认false,有些场合为了便于排版阅读则需要对输出做缩放排列 this.configure(SerializationFeature.INDENT_OUTPUT, false); // 设置全局的时间转化 SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); this.setDateFormat(smt); // 设置日期反序列化组件 SimpleModule dateDeserializerModule = new SimpleModule(); dateDeserializerModule.addDeserializer(Object.class, new CustomDateDeseralizer()); this.registerModule(dateDeserializerModule); // 注意:不要开启序列化和反序列化时为对象附加@class属性(坑) // this.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, As.PROPERTY); } }
/** * 测试objectMapper特性 * * @version 1.0.0 * @since 2015年3月2日 下午2:02:05 * @param <T> 泛型参数 */ @JsonAutoDetect(fieldVisibility = Visibility.ANY) //只包含非空属性 @JsonInclude(Include.NON_NULL) @JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY) public class Msg<T> { @JsonProperty(required = false, value = "code") private String code; /** 消息内容 */ @JsonProperty(required = false, value = "msg") private String msg; @JsonProperty(required = false, value = "name") String name; @JsonProperty(required = false, value = "payload") @JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY) T payload; public T getPayload() { return payload; } public void setPayload(T payload) { this.payload = payload; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
/** * 测试objectMapper特性 * * @version 1.0.0 * @since 2015年3月2日 下午2:02:05 * @param <T> d */ @JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY) public class TypeMap<T> { @JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY) //指示Map的value是使用@JsonTypeInfo进行解析 Map<String, T> map = new HashMap<String, T>(); public Map<String, T> getMap() { return map; } public void setMap(Map<String, T> map) { this.map = map; } }
import java.io.Serializable; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo.As; import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; /** * 测试类 * * @version 1.0.0 * @since 2015年2月5日 上午10:32:38 */ @JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY) public class User implements Serializable { private String id; private String name; private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import java.io.Serializable; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo.As; import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; /** * 测试类 * * @version 1.0.0 * @since 2015年2月5日 上午10:32:38 */ @JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY) public class User1 implements Serializable { /** */ private static final long serialVersionUID = 5723985672168696454L; private String id; private String name; private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
d
相关推荐
主要介绍了实例解析Json反序列化之ObjectMapper,json自定义序列化的方法,需要的朋友可以了解下。
使用ObjectMapper,您可以根据您的复杂对象自动构建XML和SOAP数据包,从而可能节省数百行的序列化代码。 ## Download在这里您可以下载最新的jar。 添加到您的项目中。 ## OMSerailizer ## OMSerailizer是一个...
ObjectMapper 是一个用 Swift 编写的框架,它使您可以轻松地将... ObjectMapper 还支持在映射过程中转换值的自定义转换。 要使用转换,只需使用 map["field_name"] 和 <- 运算符右侧的您选择的转换创建一个元组。
映射期间的自定义转换 结构支持 基础 为了支持映射,一个类或结构只是需要实现Mappable协议,其中包括以下功能: init? ( map : Map) mutating func mapping ( map : Map) ObjectMapper使用<-运算符定义每个...
使用 Alamofire Moya ObjectMapper 请求网络和解析数据,封装了一层Provider请求,也有使用自己封装的 Alamofire,使用 Decodable 解析数据 采用 SnapKit 对控件的约束,扩展了一层快速创建的控件的UIView子控件,...
对我来说有用的一些事情是: 属性注入有多种方式:文件,数据库,环境或注释注射式测井Json到Object和Object到Json转换器(使用Jackson) 自定义Jackson ObjectMapper注入查找由CDI管理的@Named bean的方法测试覆盖...
1. ObjectMapper:这是一个功能强大且易于使用的Swift框架,它提供了简单的API来实现字典到模型对象的转换。它支持将JSON数据、字典或字符串转换为自定义的Swift对象,并且可以自动映射属性和嵌套对象。 2. ...
自定义的表情键盘,自定义的工具键盘。录音的 wav 文件会被自动转换成 amr文件,这样可以方便和 Android 设备进行文件传输。这两种文件都已做了缓存。上传使用 amr 文件,播放使用 wav 文件。当你点击声音 ...
1. ObjectMapper:jackson-databind的核心类,用于转换Java对象和JSON。使用简单,一般就一两行代码就可以完成序列化和反序列化。 2. 支持复杂对象与JSON的映射:支持Java类型如Map、List、数组以及自定义bean与JSON的...
自定义Jackson ObjectMapper iv. 65.4. 自定义@ResponseBody渲染 v. 65.5. 处理Multipart文件上传 vi. 65.6. 关闭Spring MVC DispatcherServlet vii. 65.7. 关闭默认的MVC配置 Spring Boot参考指南 7 viii. 65.8. ...
您首先需要首先注册该模块(与所有Jackson数据类型模块相同): ObjectMapper mapper = new ObjectMapper ();mapper . registerModule( new JtsModule ());读写几何类型注册JTS模块后, 将能够将Geometry实例编写为...
包含配置用法返回值从源头构建 视图这个库的所有功能都归结为一个自定义的 Jackson 序列化器。 用法只需像这样初始化一个标准的 Jackson ObjectMapper类: import com.fasterxml.jackson.databind.ObjectMapper ;...
ObjectMapper mapper = new ObjectMapper(); return new ByteArrayInputStream(mapper.writeValueAsString(new JsonApi(ApiCodeEnum.TIMEOUT)).getBytes("UTF-8")); } @Override public String getStatusText...
开源 bbs 源码 java 网站 : 后台管理系统: update date : 2018-05-13 ...(7)引入spring-data-redis,修改接口的返回值是JSONObject(原来的ObjectMapper不支持Redis的序列化)(2018-04-12) (8)自定义登录异常