Custom format for Property names using PropertyNamingStrategy Part 2

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

In the post, “Custom format for Property names using PropertyNamingStrategy Part 1”, I listed the different formats provided by the interface out of the box, which are

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

In the Part 1 I showed how to use the above formats. In this post, I will show how to create our own format.

We can create our own format by implementing PropertyNamingStrategy interface as shown below

CustomPropertyNamingStrategy


import javax.json.bind.config.PropertyNamingStrategy;

public class CustomPropertyNamingStrategy implements PropertyNamingStrategy {
    @Override
    public String translateName(String propertyName) {
        return "pre_" + propertyName;
    }
}

We implement the method “translateName”. The method receives the property name as a parameter and returns a String which will be used as field name in json output. In the above code I am adding prefix “pre_” to every property name and return it.

In the below main class I will show how to use this custom implementation.

Main Code


1  import javax.json.bind.Jsonb;
2  import javax.json.bind.JsonbBuilder;
3  import javax.json.bind.JsonbConfig;
4  
5  public class JsonbDemo9 {
6   public static void main(String[] args) {
7       CustomPropertyNamingStrategy customPropertyNamingStrategy = new CustomPropertyNamingStrategy();
8       JsonbConfig jsonbConfig = new JsonbConfig();
9       jsonbConfig.setProperty(JsonbConfig.FORMATTING, true);
10      jsonbConfig.withPropertyNamingStrategy(customPropertyNamingStrategy);
11      
12      Jsonb jsonb = JsonbBuilder.create(jsonbConfig);
13      
14      Person person = new Person();
15      person.setfName("fName");
16      person.setlName("lName");
17      person.setAge(10);
18      
19      String result = jsonb.toJson(person);
20      System.out.println(result);
21  }
22 }

In the above code, at line 7, we create an instance of CustomPropertyNamingStrategy class.

At line 8, we create an instance of JsonbConfig.

At line 10, we set the PropertyNamingStrategy (i.e., CustomPropertyNamingStrategy) to use with the jsonbConfig using the method “withPropertyNamingStrategy”

Next at line 12, we create the Jsonb instance (which will serialize or deserialize the java class) with the jsonbConfig using JsonbBuilder.

The output of the above code will be

Output

{
“pre_age”: 10,
“pre_fName”: “fName”,
“pre_lName”: “lName”
}

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