Skip to content

Commit 8ecfd8d

Browse files
pcoates33maibin
authored andcommitted
Bael 2721 - JSON deserialization mapping different JSON fields to same Java field (eugenp#6434)
* BAEL-2721 Examples of @JsonAlias and Gson's alternate parameter * BAEL-2721 Update class and method names for JsonAlias and GsonAlternate
1 parent 57a1096 commit 8ecfd8d

4 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.baeldung.gson.entities;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class Weather {
6+
7+
@SerializedName(value = "location", alternate = "place")
8+
private String location;
9+
10+
@SerializedName(value = "temp", alternate = "temperature")
11+
private int temp;
12+
13+
@SerializedName(value = "outlook", alternate = "weather")
14+
private String outlook;
15+
16+
public String getLocation() {
17+
return location;
18+
}
19+
20+
public void setLocation(String location) {
21+
this.location = location;
22+
}
23+
24+
public int getTemp() {
25+
return temp;
26+
}
27+
28+
public void setTemp(int temp) {
29+
this.temp = temp;
30+
}
31+
32+
public String getOutlook() {
33+
return outlook;
34+
}
35+
36+
public void setOutlook(String outlook) {
37+
this.outlook = outlook;
38+
}
39+
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung.gson.deserialization;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.baeldung.gson.entities.Weather;
6+
import org.junit.Test;
7+
8+
import com.google.gson.Gson;
9+
import com.google.gson.GsonBuilder;
10+
11+
public class GsonAlternateUnitTest {
12+
13+
@Test
14+
public void givenTwoJsonFormats_whenDeserialized_thenWeatherObjectsCreated() throws Exception {
15+
16+
Gson gson = new GsonBuilder().create();
17+
18+
Weather weather = gson.fromJson("{" +
19+
"\"location\": \"London\"," +
20+
"\"temp\": 15," +
21+
"\"weather\": \"Cloudy\"" +
22+
"}", Weather.class);
23+
24+
assertEquals("London", weather.getLocation());
25+
assertEquals("Cloudy", weather.getOutlook());
26+
assertEquals(15, weather.getTemp());
27+
28+
weather = gson.fromJson("{" +
29+
"\"place\": \"Lisbon\"," +
30+
"\"temperature\": 35," +
31+
"\"outlook\": \"Sunny\"" +
32+
"}", Weather.class);
33+
34+
assertEquals("Lisbon", weather.getLocation());
35+
assertEquals("Sunny", weather.getOutlook());
36+
assertEquals(35, weather.getTemp());
37+
38+
}
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.jackson.entities;
2+
3+
import com.fasterxml.jackson.annotation.JsonAlias;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
public class Weather {
7+
8+
@JsonProperty("location")
9+
@JsonAlias("place")
10+
private String location;
11+
12+
@JsonProperty("temp")
13+
@JsonAlias("temperature")
14+
private int temp;
15+
16+
@JsonProperty("outlook")
17+
@JsonAlias("weather")
18+
private String outlook;
19+
20+
public String getLocation() {
21+
return location;
22+
}
23+
24+
public void setLocation(String location) {
25+
this.location = location;
26+
}
27+
28+
public int getTemp() {
29+
return temp;
30+
}
31+
32+
public void setTemp(int temp) {
33+
this.temp = temp;
34+
}
35+
36+
public String getOutlook() {
37+
return outlook;
38+
}
39+
40+
public void setOutlook(String outlook) {
41+
this.outlook = outlook;
42+
}
43+
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.jackson.deserialization.jsonalias;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.jackson.entities.Weather;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
public class JsonAliasUnitTest {
11+
12+
@Test
13+
public void givenTwoJsonFormats_whenDeserialized_thenWeatherObjectsCreated() throws Exception {
14+
15+
ObjectMapper mapper = new ObjectMapper();
16+
17+
Weather weather = mapper.readValue("{" +
18+
"\"location\": \"London\"," +
19+
"\"temp\": 15," +
20+
"\"weather\": \"Cloudy\"" +
21+
"}", Weather.class);
22+
23+
assertEquals("London", weather.getLocation());
24+
assertEquals("Cloudy", weather.getOutlook());
25+
assertEquals(15, weather.getTemp());
26+
27+
weather = mapper.readValue("{" +
28+
"\"place\": \"Lisbon\"," +
29+
"\"temperature\": 35," +
30+
"\"outlook\": \"Sunny\"" +
31+
"}", Weather.class);
32+
33+
assertEquals("Lisbon", weather.getLocation());
34+
assertEquals("Sunny", weather.getOutlook());
35+
assertEquals(35, weather.getTemp());
36+
37+
}
38+
}

0 commit comments

Comments
 (0)