Skip to content

Example of a simple library for JSON serialization with Java Reflection API

Notifications You must be signed in to change notification settings

MihajloNesic/json-serialization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JSON Serialization with Java Reflection API

Features

  • @JsonSerializable - Class annotation to mark class as json serializable
  • @JsonElement - Field annotation to mark field as json serializable. Can have an alias and can be marked as required (only for Objects)
  • @JsonInit - Method annotation. The method will execute before the json is constructed

Can serialize

  • primitives and wrapper classes
  • arrays
  • collections (lists, sets)
  • objects
  • maps

Example

@JsonSerializable
public class Student {

    @JsonElement
    private String firstName;

    @JsonElement
    private String lastName;

    @JsonElement(required = false)
    private String middleName;

    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
Student student = new Student("John", "Doe");
String jsonString = JsonConverter.convertToJson(student);
System.out.println(jsonString);

Output

{"firstName": "John", "lastName": "Doe"}

See tests for full examples

About

Example of a simple library for JSON serialization with Java Reflection API

Topics

Resources

Stars

Watchers

Forks

Languages