使用Jackson实现对象,json,xml,map的转换

使用Jackson实现对象,json,xml,map的转换

  • 前言
  • 一、pom文件
  • 二、创建实体类
  • 三、对象、json、xml、map的转换
  • 四、运行结果
  • 五。忽略字段
  • 六。解决long返回时精度丢失问题

前言

使用Jackson实现对象,json,xml,map的互相转换

使用的包

包名 版本
jackson-databind 2.12.3
jackson-dataformat-xml 2.12.3

一、pom文件

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.12.3</version>
        </dependency>

二、创建实体类

package au.com.koalaclass.pay.paylinx.message.wechatpay;

import au.com.koalaclass.pay.paylinx.message.XmlRequest;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author : luzhiyong
 * @CreateTime : 2021/6/1
 * @Description :
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@JacksonXmlRootElement(localName = "xml")
public class WechatRateXmlRequest {
    @JsonProperty(value = "mch_id")
    @JacksonXmlProperty(localName = "mch_id")
    private String mchId;//商户号

    @JsonProperty(value = "fee_type")
    @JacksonXmlProperty(localName = "fee_type")
    private String feeType;//货币,如AUD
}

三、对象、json、xml、map的转换

对象转换json使用的是ObjectMapper
对象转换map使用的是XmlMapper
转泛型mapper.readValue(json, new TypeReference() {
});

 @Test
    public void test2() throws JsonProcessingException {
        //创建一个对象
        WechatRateXmlRequest wechatRateXmlRequest = new WechatRateXmlRequest();
        //给对象属性赋值
        wechatRateXmlRequest.setMchId("123456");
        wechatRateXmlRequest.setFeeType("AUD");
        //创建ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        //对象转成json字符
        String json = mapper.writeValueAsString(wechatRateXmlRequest);
        System.out.println("对象转json"+json);
        //json字符串转对象
        WechatRateXmlRequest xmlRequest = mapper.readValue(json, WechatRateXmlRequest.class);
        System.out.println("json字符串转对象"+xmlRequest.toString());
        //json转map
        Map mp = mapper.readValue(json, Map.class);
        System.out.println("json转map"+mp);
        XmlMapper xmlMapper = new XmlMapper();
        //对象转xml
        String xml = xmlMapper.writeValueAsString(wechatRateXmlRequest);
        System.out.println("对象转xml"+xml);
        //xml转对象
        WechatRateXmlRequest rate = xmlMapper.readValue(xml, WechatRateXmlRequest.class);
        System.out.println("xml转对象"+rate);
        //xml转map
        Map map = xmlMapper.readValue(xml, Map.class);
        System.out.println("xml转map"+map);

    }

四、运行结果

对象转json{"sign":null,"mch_id":"13231952","fee_type":"AUD"}
json字符串转对象WechatRateXmlRequest(mchId=13231952, feeType=AUD)
json转map{sign=null, mch_id=13231952, fee_type=AUD}
对象转xml<xml><sign/><mch_id>13231952</mch_id><fee_type>AUD</fee_type></xml>
xml转对象WechatRateXmlRequest(mchId=13231952, feeType=AUD)
xml转map{sign=, mch_id=13231952, fee_type=AUD}

五。忽略字段

@JsonIgnoreProperties(ignoreUnknown = true)
将这个注解写在类上之后,就会忽略类中不存在的字段

这个注解还可以指定要忽略的字段。使用方法如下:
@JsonIgnoreProperties({ “internalId”, “secretKey” })

六。解决long返回时精度丢失问题

@Configuration
public class JsonConfig {
    /**
     * Jackson全局转化long类型为String,解决jackson序列化时long类型缺失精度问题
     *
     * @return Jackson2ObjectMapperBuilderCustomizer 注入的对象
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder.serializerByType(Long.TYPE, ToStringSerializer.instance);
                jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance);

            }
        };
    }
}

你可能感兴趣的:(java,Jackson,json,xml,java)