klausboeing / jackson-extension

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jackson Extension

Build Status

Module for Jackson adds utilities for serialization and deserialization.

Getting Started

@JsonPropertyGroup and @JsonUsePropertyGroup

Sample 1

public class Customer {

    private String name;
    private String email;
    private String phone;
    private Address address;
    
    // .. more
}
public class Address {

    private String street;
    private String city;
    private String state;
    private String zip;
    private List<Contact> contacts;

    // .. more
}
public class Contact {

    private String name;
    private String email;
    private String phone;
    
    // .. more
}

The JSON result of serialization:

{
  "name" : "Adam Levine",
  "email" : "adam.levine@maroon5.com",
  "phone" : "(877) 609-2233",
  "address" : {
    "street" : "1444 S. Alameda Street",
    "city" : "Los Angeles",
    "state" : "Califórnia",
    "zip" : "90021",
    "contacts" : [ {
      "name" : "James Valentine",
      "email" : "james.valentine@maroon5.com",
      "phone" : "(877) 609-2244"
    }, {
      "name" : "Jesse Carmichael",
      "email" : "jesse.carmichael@maroon5.com",
      "phone" : "(877) 609-2255"
    } ]
  }
}

Use @JsonUsePropertyGroup and @JsonPropertyGroup to reduce serialization of relationships. Note the relationships with @JsonUsePropertyGroup.

public class Customer {

    private String name;
    private String email;
    private String phone;
    
    @JsonUsePropertyGroup
    private Address address;
    
    // .. more
}

Note the attributes of the relationship with @JsonPropertyGroup.

public class Address {

    private String street;
    @JsonPropertyGroup
    private String city;
    @JsonPropertyGroup
    private String state;
    private String zip;
    private List<Contact> contacts;

   // .. more
}

The JSON result of serialization:

{
  "name" : "Adam Levine",
  "email" : "adam.levine@maroon5.com",
  "phone" : "(877) 609-2233",
  "address" : {
    "city" : "Los Angeles",
    "state" : "Califórnia"
  }
}

Prerequisities

  • Maven
  • Java 8

Installing

Configure the project repository:

        <repositories>
            <repository>
                <id>klausboeing-mvn-repo</id>
                <url>https://raw.github.com/klausboeing/jackson-extension/mvn-repo/</url>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>

Add the following dependence on project:

        <dependency>
          <groupId>com.klausboeing</groupId>
          <artifactId>jackson-extension</artifactId>
          <version>0.0.3-SNAPSHOT</version>
        </dependency>

Configure the module in ObjectMapper:

        ObjectMapper om = new ObjectMapper();
        om.registerModule(new JacksonExtensionModule());

Versioning

We use SemVer for versioning.

Authors

  • Klaus Boeing - Initial work

About


Languages

Language:Java 100.0%