How to customize the JSON Serializer used by Hibernate-Types

How to customize the JSON Serializer used by Hibernate-Types
Techiio-author
Written by Sagar RabidasMarch 1, 2022
7 min read
Hibernate
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

How to customize the JSON Serializer used by Hibernate-Types - Today we will start the discussion.

Introduction of JSON:-

JavaScript object notation (complete shape of JSON) is a popular file layout used to interchange statistics. The data gadgets are saved and transmitted using key-price pairs and array statistics sorts. In simpler phrases, JSON information is (in some ways) the language of databases.

JSON has syntactic similarity to JavaScript because JSON is based on JavaScript object notation syntaxes. But JSON format is text only, which makes it easy to read and use with any programming language.

Where is JSON used:-

JSON facts layout is ubiquitous in net APIs, cloud computing, NoSQL databases (like MongoDB), and increasingly also in Machine Learning.

This is how a typical JSON data looks like (more on this in upcoming sections) -

{
"_id": "5c8a1d5b0190b214360dc097",
"category": "Crio Bytes",
"clusterName": "OOP Foundations",
"bytes": ["Abstraction", "Inheritance", "Encapsulation", "Polymorphism”]
"isFree": true
}

More generic scenarios where JSON data format is used are:

  • Data storage, configuration, and validation
  • Data transfer between APIs, client, and server
  • Restructuring input data from the user (JSON schema)

Declarative configuration:-

The easiest way to achieve this goal is to set the hibernate.types.json.serializer configuration property in the hibernate.properties file:

hibernate.types.JSON.serializer=com.vladmihalcea.hibernate.type.JSON.configuration.CustomJsonSerializerSupplier

This property takes the fully-qualified class name of an implementation of the CustomJsonSerializerSupplier interface:

What’s exceptional about customizing the hibernate-type project is that you may use both the hibernate-particular hibernate.Properties or you may offer a hibernate-kinds.Properties document in case you can't alter hibernate.Properties.

You can even supply a different Java properties file via the hibernate-types.properties.path System property,

The CustomJsonSerializerSupplier can look as follows:

public class CustomJsonSerializerSupplier
    implements JsonSerializerSupplier {
 
    @Override
    public JsonSerializer get() {
        return new CustomJsonSerializer();
    }
}

Right here, for checking out sake, we're using a custom JSON serializer that implements the JSON serializer interface:

    implements JsonSerializer {
 
    private static boolean called;
 
    public static boolean isCalled() {
        return called;
    }
 
    public static void reset() {
        called = false;
    }
 
    @Override
    public <T> T clone(T value) {
        called = true;
        return JacksonUtil.clone(value);
    }
}

We are going to use the called static variable to test in our unit test if this technique is called using hibernate-sorts.

assertFalse(CustomJsonSerializer.isCalled());
 
doInJPA(entityManager -> {
    Location location = new Location();
    location.setCountry("Romania");
    location.setCity("Cluj-Napoca");
    location.setReference(
        BigDecimal.valueOf(2.25262562526626D)
    );
 
    Event event = new Event();
    event.setId(1L);
    event.setLocation(location);
    entityManager.persist(event);
});
 
assertTrue(CustomJsonSerializer.isCalled());
CustomJsonSerializer.reset();
assertFalse(CustomJsonSerializer.isCalled());
 
doInJPA(entityManager -> {
    Event event = entityManager.find(Event.class, 1L);
    assertEquals(
        "2.25262562526626",
        event.getLocation().getReference().toString()
    );
});
 
assertTrue(CustomJsonSerializer.isCalled());

As you can see, the CustomJsonSerializer is being used, so you can easily customize the way a given JSON object is serialized by hibernate-types.

Conclusion:-

Because of the configuration mechanism, I introduced for customizing the object mapper, it's miles very clean to add new configuration options, like this one which lets you exchange the manner a given JSON item is serialized via hibernate-sorts.

Hibernate
JSON
JavaScript
Apis
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance