Home > Java > javaTutorial > body text

How to implement a custom serializer using @JsonSerialize annotation in Java?

WBOY
Release: 2023-09-11 18:17:06
forward
736 people have browsed it

How to implement a custom serializer using @JsonSerialize annotation in Java?

@JsonSerialize annotation is used to declare a custom serializer during field serialization. We can implement a custom serializer by extending the StdSeralizer class. And you need to override the serialize() method of the StdSerializer class.

Syntax

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

In the following program, we can use the @JsonSerialize annotation to implement a custom serializer

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.ser.std.*;
public class JsonSerializeAnnotationTest {
   public static void main (String[] args) throws JsonProcessingException, IOException {
      Employee emp = new Employee(115, "Adithya", new String[] {"Java", "Python", "Scala"});
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
      System.out.println(jsonString);
   }
}
// CustomSerializer class
class CustomSerializer extends StdSerializer {
   public CustomSerializer(Class t) {
      super(t);
   }
   public CustomSerializer() {
      this(Employee.class);
   }
   @Override
   public void serialize(Employee emp, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonGenerationException {
      StringBuilder sb = new StringBuilder();
      jgen.writeStartObject();
      jgen.writeNumberField("id", emp.getId());
      jgen.writeStringField("name", emp.getName());
      for(String s: emp.getLanguages()) {
         sb.append(s).append(";");
      }
      jgen.writeStringField("languages", sb.toString());
      jgen.writeEndObject();
   }
}
// Employee class
@JsonSerialize(using=CustomSerializer.class)<strong>
</strong>class Employee {
   private int id;
   private String name;
   private String[] languages;
   public Employee(int id, String name, String[] languages) {
      this.id = id;
      this.name = name;
      this.languages = languages;
   }
   public int getId() {
      return this.id;
   }
   public String getName() {
      return this.name;
   }
   public String[] getLanguages() {
      return this.languages;
   }
<strong>   </strong>@Override<strong>
 </strong>  public String toString() {
      StringBuilder sb = new StringBuilder("ID: ").append(this.id).append("\nName: ").append(this.name).append("\nLanguages:");
      for(String s: this.languages) {
         sb.append(" ").append(s);
      }
      return sb.toString();
   }
}
Copy after login

Output

{
   "id" : 115,
   "name" : "Adithya",
   "languages" : "Java;Python;Scala;"
}
Copy after login

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!