json序列化定义类型jsckson

Posted codelei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json序列化定义类型jsckson相关的知识,希望对你有一定的参考价值。

可以解决一批问题。

1.导包

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.7</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.7</version>
    </dependency>

2. json反序列化定义

package javastu;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateJsonDeserializer extends JsonDeserializer<Date> 

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException 
        try 
            return simpleDateFormat.parse(jsonParser.getText());
         catch (ParseException e) 
            e.printStackTrace();
        
        return null;
    

3. 自定义json序列化类

package javastu;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateJsonSerializer extends JsonSerializer<Date> 

    public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException 
        jsonGenerator.writeString(simpleDateFormat.format(date));
    

4.对象

package javastu;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.Date;


public class Student 

    private String name;

    @JsonDeserialize(using = DateJsonDeserializer.class)
    @JsonSerialize(using = DateJsonSerializer.class)
    private Date time;

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public Date getTime() 
        return time;
    

    public void setTime(Date time) 
        this.time = time;
    

    @Override
    public String toString() 
        return "Student" +
                "name=‘" + name + ‘\‘‘ +
                ", time=" + time +
                ‘‘;
    

6. 测试类

package javastu;


import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Date;

public class App 

    public static void main(String[] args) throws IOException 
        Student student = new Student();
        student.setName("张三");
        student.setTime(new Date());

        ObjectMapper objectMapper = new ObjectMapper();

        String s = objectMapper.writeValueAsString(student);

        Student student2 = objectMapper.readValue(s, Student.class);

        System.out.println(s);

        System.out.println(student2);



    

 

以上是关于json序列化定义类型jsckson的主要内容,如果未能解决你的问题,请参考以下文章

关闭Spring Boot的Jsckson的FAIL_ON_EMPTY_BEANS

vs code 自定义代码片段

Qt中 使用Json 对自定义对象进行序列与反序列化 之二

使用 System.Text.Json 时,如何处理 Dictionary 中 Key 为自定义类型的问题

Python json.dumps 特殊数据类型的自定义序列化操作

Java 数据类型的自定义 JSON 序列化