Home > Java > javaTutorial > body text

How to implement a custom deserializer using @JsonDeserialize annotation in Java?

王林
Release: 2023-08-25 19:29:05
forward
1294 people have browsed it

How to implement a custom deserializer using @JsonDeserialize annotation in Java?

@JsonDeserialize annotation is used to declare a custom deserializer when deserializing JSON into a Java object. We can implement a custom deserializer by extending the StdDeserializer class using the generic type Employee, and need to override the deserialize() method StdDeserializer class.

Syntax

@Target(value={ANNOTATION_TYPE,METHOD,FIELD,TYPE,PARAMETER})
@Retention(value=RUNTIME)
public @interface JsonDeserialize
Copy after login

In the following program, we can use the

@JsonDeserialize annotation to implement a custom deserializer

Example

import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;
import com.fasterxml.jackson.databind.deser.std.*;
public class JsonDeSerializeAnnotationTest {
   public static void main (String[] args) throws JsonProcessingException, IOException {
      Employee emp = new Employee(115, "Adithya");
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writeValueAsString(emp);
      emp = mapper.readValue(jsonString, Employee.class);
      System.out.println(emp);
   }
}
// CustomDeserializer class
class CustomDeserializer extends StdDeserializer<Employee> {
   public CustomDeserializer(Class<Employee> t) {
      super(t);
   }
   public CustomDeserializer() {
      this(Employee.class);
   }
   @Override
   public Employee deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
      int id = 0;
      String name = null;
      JsonToken currentToken = null;
      while((currentToken = jp.nextValue()) != null) {
         switch(currentToken) {
            case VALUE_NUMBER_INT:
            if(jp.getCurrentName().equals("id")) {
               id = jp.getIntValue();
            }
            break;
            case VALUE_STRING:
            switch(jp.getCurrentName()) {
               case "name":
               name = jp.getText();
               break;
               default:
               break;
            }
            break;
            default:
            break;
         }
      }
      return new Employee(id, name);
   }
}
// Employee class
@JsonDeserialize(using=CustomDeserializer.class)<strong>
</strong>class Employee {
   private int id;
   private String name;
   public Employee(int id, String name) {
      this.id = id;
      this.name = name;
   }
   public int getId() {
      return id;
   }
   public String getName() {
      return name;
   }
   @Override
   public String toString() {
      StringBuilder sb = new StringBuilder("ID: ").append(this.id).append("\nName: ").append(this.name);
      return sb.toString();
   }
}
Copy after login

Output

ID: 115
Name: Adithya
Copy after login

The above is the detailed content of How to implement a custom deserializer using @JsonDeserialize annotation in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template