Introduction to the
In this article, you’ll learn how to use Jackson and Gson to map different JSON fields to a single Java field.
Maven rely on
To use the Jackson and Gson libraries, we need to add the following dependencies to the POM:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> The < artifactId > Jackson - databind < / artifactId > < version > 2.9.8 < / version > < scope >test</scope>
</dependency>
Copy the code
The example JSON
Let’s say we want to input weather details from different locations into our Java class. We found several sites that publish weather data as JSON documents. However, their formats are not consistent
{
"location": "Guangzhou"."temp": 15."weather": "Cloudy"
}
Copy the code
{
"place": "Shenzhen"."temperature": 35."outlook": "Sunny day"
}
Copy the code
We want to deserialize both formats into the same Java class called Weather:
The use of Jackson
To achieve this goal, we will use Jackson’s @jsonProperty and @JsonAlias annotations. These two annotations will help us map JSON properties to the same Java field.
First, we’ll use the @jsonProperty annotation to let Jackson know the name of the JSON field to map. The @jsonProperty annotation in value is used for both deserialization and serialization.
We can then use the @jsonAlias annotation. Therefore, Jackson will know the names of other fields in the JSON document that are mapped to Java fields. Properties annotated with @jsonAlias are used for deserialization.
@JsonProperty("location")
@JsonAlias("place")
private String location;
@JsonProperty("temp")
@JsonAlias("temperature")
private int temp;
@JsonProperty("outlook")
@JsonAlias("weather") private String outlook; Getters and setters ignoredCopy the code
Now that we’ve added annotations, let’s use Jackson’s ObjectMapper method to create a Weather object.
@Test
public void test() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Weather weather = mapper.readValue("{\n"
+ \"location\": \" Guangzhou \",\n"
+ " \"temp\": 15,\n"
+ "\ "Weather \": \" Cloudy \"\n"
+ "}", Weather.class);
TestCase.assertEquals("Guangzhou", weather.getLocation());
TestCase.assertEquals("Cloudy", weather.getOutlook());
TestCase.assertEquals(15, weather.getTemp());
weather = mapper.readValue("{\n"
+ "\" Place \ : \" Shenzhen \,\n"
+ " \"temperature\": 35,\n"
+ "\ "Outlook \ : \" Sunny Day \"\n"
+ "}", Weather.class);
TestCase.assertEquals("Shenzhen", weather.getLocation());
TestCase.assertEquals("Sunny day", weather.getOutlook());
TestCase.assertEquals(35, weather.getTemp());
}
Copy the code
Using Gson
Now, let’s see how Gson is implemented. We need to use values and alternate parameters in the @serializedName annotation.
The first will be used as the default, and the second will be used to indicate the alternate name of the JSON field we want to map:
@SerializedName(value="location", alternate="place")
private String location;
@SerializedName(value="temp", alternate="temperature")
private int temp;
@SerializedName(value="outlook", alternate="weather")
private String outlook;
Copy the code
Now that we have added comments, let’s test our example:
@Test
public void test() throws Exception {
Gson gson = new GsonBuilder().create();
Weather weather = gson.fromJson("{\n"
+ \"location\": \" Guangzhou \",\n"
+ " \"temp\": 15,\n"
+ "\ "Weather \": \" Cloudy \"\n"
+ "}", Weather.class);
TestCase.assertEquals("Guangzhou", weather.getLocation());
TestCase.assertEquals("Cloudy", weather.getOutlook());
TestCase.assertEquals(15, weather.getTemp());
weather = gson.fromJson("{\n"
+ "\" Place \ : \" Shenzhen \,\n"
+ " \"temperature\": 35,\n"
+ "\ "Outlook \ : \" Sunny Day \"\n"
+ "}", Weather.class);
TestCase.assertEquals("Shenzhen", weather.getLocation());
TestCase.assertEquals("Sunny day", weather.getOutlook());
TestCase.assertEquals(35, weather.getTemp());
}
Copy the code
conclusion
We saw this by using Jackson’s @JsonAlias or Gson’s alternative argument, which allows us to easily convert different JSON formats into the same Java object.