Assert JSON

JSONassert is a project to “Write JSON unit tests in less code. Great for testing REST interfaces.” The following is maven dependency of JSONassert:

<dependency>
 <groupId>org.skyscreamer</groupId>
 <artifactId>jsonassert</artifactId>
 <version>1.3.0</version>
</dependency>

To test whether 2 JSON objects are same or not, do the following:

JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.NON_EXTENSIBLE);

NON_EXTENSIBLE is the most commonly used compare mode of 4. You can check javadoc to find out how they function.

Advertisements

Expose to GSON

Gson is a tool to convert between Java objects and JSON objects. Its usage with JSON is somewhat similar to the usage of JAX-B with XML (although JAX-B can also process JSON). This article will explain the basis of GSON in order to get a quick understanding of how it works for anyone who is new to GSON.

Maven Dependency

At the time this article is composed, the following GSON dependency is the latest release version.

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.7</version>
</dependency>

For more information about Maven configurations, see POM Configurations.

Serialize & Deserialize

There are 2 steps for both Serialization and Deserialization with GSON: annotate the fieds, and process.

Annotation

To serialize or deserialize objects, you need first to annotate the fields of the java classes (most likely to be Entity Beans). The following code is an example of annotating field. Basically, this is all you need to do in most cases.

@Expose
private String name;

There are 2 boolean arguments you can pass in for this annotation: serialize, deserialize. By default, they are both true, means you will include this annotated field in both processes. If serialize is false, when turning java object to json, this field will be ignored. On the other hand, if deserialize is false, when turning json to java, this field will be ignored. In addition, if you set both to be false, it’s same to not annotating @Expose on this field.

If you would like to change the name of the fields in the JSON file, you can use @SerializedName. For example:

@SerializedName("n")
private String name;

The following code shows an Entity class with GSON annotations.

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "student")
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Expose
    private Long id;

    @NotNull
    @Size(min = 2, max = 25)
    @Expose
    @SerializedName("n")
    private String name;

    // getters and setters...
}

From the above code stub, you can see that only field level annotation is needed.

Processing

To process conversion between Java object and JSON object, you can do the following.

Converting from JSON to Java object:

Target targetObject = new GsonBuilder().create().fromJson(jsonBody, Target.class);

Converting from Java object to JSON:

String jsonBody = new GsonBuilder().create().toJson(targetObject);

In above code, Target is a class annotated and associated with the body of the JSON String: jsonBody. However, I omitted some middle processes of how to create a Gson instance.

GsonBuilder gb = new GsonBuilder();
Gson g = gb.create();
// then you can do g.toJson() or g.fromJson()

The GsonBuilder instance can accept some configs in order to customize the conversion.

Versioning

In order to do versioning, you can put @Since and @Until to the fields of the Java objects:

@Since(1.1)
private String newStandard;

@Until(0.9)
private String oldStandard;

To make them work, you can configure the GsonBuilder:

GsonBuilder gb = new GsonBuilder();
gb.setVersion(1.0);

Then, the Gson instance will process conversions based on the version.