JackJson

6 篇文章 0 订阅
5 篇文章 0 订阅

jackjson

https://github.com/FasterXML/jackson-docs
https://github.com/FasterXML/jackson-core
https://github.com/FasterXML/jackson-databind
https://github.com/FasterXML/jackson-annotations

ObjectMapper

http://www.studytrails.com/java/json/jackson-create-json/
http://tutorials.jenkov.com/java-json/jackson-objectmapper.html
JackJson - JSON to Java

Annotation

https://github.com/FasterXML/jackson-docs/wiki/JacksonAnnotations
http://tutorials.jenkov.com/java-json/jackson-annotations.html

general annotationread or writedescription
@JsonIgnoreread + write忽略某个字段
@JsonIgnorePropertiesread + write忽略多个字段,ignoreUnknown=true可未知属性
@JsonPropertyread + write重命名字段
@JsonNamingread + write自定义属性序列化或反序列化规则
@JsonFormatread + write时间格式化,有时区问题
@JsonIncludewrite忽略满足条件的属性,如null
@JsonValuewrite序列化对象为返回值,如把Enum序列化为code
@JsonCreatorread反序列化指定值为对象,如把code序列化为Enum
@JsonDeserializeread反序列为指定的接口或是泛型时,用来指定实际的类型

Features

JsonParser Features

general featruedefaultdescription
ALLOW_UNQUOTED_FIELD_NAMESfalse允许字段名称没有双引号
ALLOW_SINGLE_QUOTESfalse允许字段名称和值使用单引号
ALLOW_NUMERIC_LEADING_ZEROSfalse允许以0开头的数字

Serialization Features

general featruedefaultdescription
INDENT_OUTPUTfalse格式化输出json,测试时有用

Deserialization Features

general featruedefaultdescription
FAIL_ON_UNKNOWN_PROPERTIEStrue有未知的属性时报错

Serialization Inclusion

序列化时,是否显示某些属性,如Include.NOT_NULL表示如果字段值为null,则序列化输出时不显示该字段
全局设置:setSerializationInclusion(Include.NON_NULL)
单个设置:@JsonInclude(JsonInclude.Include.NON_NULL)

Date Format

默认在java.uti.Date和时间戳之间序列化和反序列化
全局设置:setDateFormat(new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”))
单个设置: @JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”,timezone = “Asia/Shanghai”)

@JsonFormat有时区问题,参考:
https://stackoverflow.com/questions/46011703/set-current-timezone-to-jsonformat-timezone-value
https://github.com/FasterXML/jackson-databind/issues/1266

Property Naming Strategy

自定义属性序列化策略,比如下划线转骆峰
全局设置: setPropertyNamingStrategy(PropertyNamingStrategy.SnakeCaseStrategy.SNAKE_CASE)
单个设置:@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)

General Configure

ObjectMapper objectMapper = new ObjectMapper()
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
            .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
            .configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true)
            .configure(SerializationFeature.INDENT_OUTPUT,true)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值