Custom format for Property names using PropertyNamingStrategy Part 1

In this post under JSONB, I will explain how to create custom format for property names using PropertyNamingStrategy interface.

Whenever we serialize a java object to JSON format, the property or field names in json are same as property names in its corresponding java class.

For example when we serialize the below java class instance

Person


public class Person {
    private String fName;
    private String lName;
    private int age;
    
    public String getfName() {
        return fName;
    }
    public void setfName(String fName) {
        this.fName = fName;
    }
    public String getlName() {
        return lName;
    }
    public void setlName(String lName) {
        this.lName = lName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(fName).append(":");
        sb.append(lName).append(":");
        sb.append(age);
        
        return sb.toString();
    }
}

The JSON generated is as shown below

Output 1


{"age":10,"fName":"fName","lName":"lName"}

If you check the json data, the field or property names age, fName, and lName is same as the Java class property names.

We can change this, provide different format for the field names with the help of PropertyNamingStrategy.

Out of the box, PropertyNamingStrategy provides 6 formats as mentioned below
1) CASE_INSENSITIVE
2) IDENTITY
3) LOWER_CASE_WITH_DASHES
4) LOWER_CASE_WITH_UNDERSCORES
5) UPPER_CAMEL_CASE
6) UPPER_CAMEL_CASE_WITH_SPACES

We change the formats of property names by using JsonbConfig as shown in the below code

Main Code


1  import javax.json.bind.Jsonb;
2  import javax.json.bind.JsonbBuilder;
3  import javax.json.bind.JsonbConfig;
4  import javax.json.bind.config.PropertyNamingStrategy;
5  
6  public class JsonbDemo8 {
7   public static void main(String[] args) {
8       JsonbConfig jsonbConfig = new JsonbConfig();
9       jsonbConfig.withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
10      
11      Jsonb jsonb = JsonbBuilder.create(jsonbConfig);
12      
13      Person person1 = new Person();
14      person1.setfName("fName");
15      person1.setlName("lName");
16      person1.setAge(10);
17      
18      String result = jsonb.toJson(person1);
19      System.out.println(result);
20      
21      Person person2 = jsonb.fromJson(result, Person.class);
22      System.out.println(person2);
23  }
24 }

At line 8, we create an instance of JsonbConfig, set the property name format using “withPropertyNamingStrategy” method as shown at line 9.

Next we create a Jsonb instance using jsonbConfig instance at line 11.

Next we use the Jsonb instance to serialize or deserialize Person java class.

Fow our example we are using LOWER_CASE_WITH_UNDERSCORES format. So all the property names will be in lower case with underscore.

Below is the output

Output 2


{"age":10,"f_name":"fName","l_name":"lName"}
fName:lName:10

When we compare output1 with output2, the property names format are changed in output2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s