Merge pull request #15127 from panos-kakos/JAVA-26735
[JAVA-26735] Upgraded gson library to 2.10.1 version
This commit is contained in:
commit
9e107be963
|
@ -79,7 +79,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<dynamodblocal.version>1.21.1</dynamodblocal.version>
|
||||
<maven-plugins-version>3.1.1</maven-plugins-version>
|
||||
</properties>
|
||||
|
|
|
@ -73,7 +73,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<dynamodblocal.version>1.21.1</dynamodblocal.version>
|
||||
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||
<jets3t-version>0.9.4.0006L</jets3t-version>
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
|
||||
<properties>
|
||||
<javax.websocket-api.version>1.1</javax.websocket-api.version>
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -64,7 +64,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<joda-time.version>2.9.6</joda-time.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package com.baeldung.gson.conversion;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -10,24 +14,29 @@ public class JsonObjectConversionsUnitTest {
|
|||
void whenUsingJsonParser_thenConvertToJsonObject() throws Exception {
|
||||
// Example 1: Using JsonParser
|
||||
String json = "{ \"name\": \"Baeldung\", \"java\": true }";
|
||||
|
||||
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
|
||||
|
||||
Assert.assertTrue(jsonObject.isJsonObject());
|
||||
Assert.assertTrue(jsonObject.get("name").getAsString().equals("Baeldung"));
|
||||
Assert.assertTrue(jsonObject.get("java").getAsBoolean() == true);
|
||||
|
||||
JsonObject jsonObject = JsonParser.parseString(json)
|
||||
.getAsJsonObject();
|
||||
|
||||
assertTrue(jsonObject.isJsonObject());
|
||||
assertEquals("Baeldung", jsonObject.get("name")
|
||||
.getAsString());
|
||||
assertTrue(jsonObject.get("java")
|
||||
.getAsBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingGsonInstanceFromJson_thenConvertToJsonObject() throws Exception {
|
||||
// Example 2: Using fromJson
|
||||
String json = "{ \"name\": \"Baeldung\", \"java\": true }";
|
||||
|
||||
|
||||
JsonObject convertedObject = new Gson().fromJson(json, JsonObject.class);
|
||||
|
||||
Assert.assertTrue(convertedObject.isJsonObject());
|
||||
Assert.assertTrue(convertedObject.get("name").getAsString().equals("Baeldung"));
|
||||
Assert.assertTrue(convertedObject.get("java").getAsBoolean() == true);
|
||||
|
||||
assertTrue(convertedObject.isJsonObject());
|
||||
assertEquals("Baeldung", convertedObject.get("name")
|
||||
.getAsString());
|
||||
assertTrue(convertedObject.get("java")
|
||||
.getAsBoolean());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,8 +3,9 @@ package com.baeldung.gson.deserialization.test;
|
|||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
|
@ -68,7 +69,7 @@ public class GsonDeserializationUnitTest {
|
|||
|
||||
final GenericFoo<Integer> targetObject = new Gson().fromJson(json, typeToken);
|
||||
|
||||
assertEquals(targetObject.theValue, new Integer(1));
|
||||
assertEquals(targetObject.theValue, Integer.valueOf(1));
|
||||
}
|
||||
|
||||
// tests - multiple elements
|
||||
|
@ -98,8 +99,7 @@ public class GsonDeserializationUnitTest {
|
|||
@Test
|
||||
public void whenDeserializingJsonIntoElements_thenCorrect() {
|
||||
final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}";
|
||||
final JsonParser jParser = new JsonParser();
|
||||
final JsonElement jElement = jParser.parse(jsonSourceObject);
|
||||
final JsonElement jElement = JsonParser.parseString(jsonSourceObject);
|
||||
final JsonObject jObject = jElement.getAsJsonObject();
|
||||
final int intValue = jObject.get("valueInt").getAsInt();
|
||||
final String stringValue = jObject.get("valueString").getAsString();
|
||||
|
|
|
@ -16,79 +16,71 @@ public class JsonCompareUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenIdenticalSimpleObjects_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27 }}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\", \"fullName\": \"Emily Jenkins\",\"age\": 27}}";
|
||||
|
||||
assertTrue(parser.parse(string1)
|
||||
.isJsonObject());
|
||||
assertEquals(parser.parse(string1), parser.parse(string2));
|
||||
assertTrue(JsonParser.parseString(string1).isJsonObject());
|
||||
assertEquals(JsonParser.parseString(string1), JsonParser.parseString(string2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameObjectsInDifferentOrder_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27 }}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"age\": 27, \"fullName\": \"Emily Jenkins\" }}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
JsonElement json1 = JsonParser.parseString(string1);
|
||||
JsonElement json2 = JsonParser.parseString(string2);
|
||||
|
||||
assertEquals(json1, json2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalArrays_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "[10, 20, 30]";
|
||||
String string2 = "[10, 20, 30]";
|
||||
|
||||
assertTrue(parser.parse(string1)
|
||||
assertTrue(JsonParser.parseString(string1)
|
||||
.isJsonArray());
|
||||
assertEquals(parser.parse(string1), parser.parse(string2));
|
||||
assertEquals(JsonParser.parseString(string1), JsonParser.parseString(string2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysInDifferentOrder_whenCompared_thenNotEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "[20, 10, 30]";
|
||||
String string2 = "[10, 20, 30]";
|
||||
|
||||
assertNotEquals(parser.parse(string1), parser.parse(string2));
|
||||
assertNotEquals(JsonParser.parseString(string1), JsonParser.parseString(string2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalNestedObjects_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"fav_product\": \"Coke\", \"last_buy\": \"2012-04-23\"}}}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"fav_product\": \"Coke\"}}}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
JsonElement json1 = JsonParser.parseString(string1);
|
||||
JsonElement json2 = JsonParser.parseString(string2);
|
||||
|
||||
assertEquals(json1, json2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalNestedObjectsWithArray_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"banana\", \"eggs\"]}}}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"banana\", \"eggs\"]}}}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
JsonElement json1 = JsonParser.parseString(string1);
|
||||
JsonElement json2 = JsonParser.parseString(string2);
|
||||
|
||||
assertEquals(json1, json2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNestedObjectsDifferentArrayOrder_whenCompared_thenNotEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"banana\", \"eggs\"]}}}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"eggs\", \"banana\"]}}}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
JsonElement json1 = JsonParser.parseString(string1);
|
||||
JsonElement json2 = JsonParser.parseString(string2);
|
||||
|
||||
assertNotEquals(json1, json2);
|
||||
}
|
||||
|
|
|
@ -43,8 +43,8 @@ public class PrimitiveValuesUnitTest {
|
|||
gson.toJson(model);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) public void
|
||||
whenSerializingNaN_thenShouldRaiseAnException() {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenSerializingNaN_thenShouldRaiseAnException() {
|
||||
FloatExample model = new FloatExample();
|
||||
model.value = Float.NaN;
|
||||
|
||||
|
@ -52,7 +52,8 @@ public class PrimitiveValuesUnitTest {
|
|||
gson.toJson(model);
|
||||
}
|
||||
|
||||
@Test public void whenDeserializingFromJSON_thenShouldParseTheValueInTheString() {
|
||||
@Test
|
||||
public void whenDeserializingFromJSON_thenShouldParseTheValueInTheString() {
|
||||
String json = "{\"byteValue\": 17, \"shortValue\": 3, \"intValue\": 3, "
|
||||
+ "\"longValue\": 3, \"floatValue\": 3.5" + ", \"doubleValue\": 3.5"
|
||||
+ ", \"booleanValue\": true, \"charValue\": \"a\"}";
|
||||
|
@ -72,14 +73,16 @@ public class PrimitiveValuesUnitTest {
|
|||
// @formatter:on
|
||||
}
|
||||
|
||||
@Test public void whenDeserializingHighPrecissionNumberIntoFloat_thenShouldPerformRounding() {
|
||||
@Test
|
||||
public void whenDeserializingHighPrecissionNumberIntoFloat_thenShouldPerformRounding() {
|
||||
String json = "{\"value\": 12.123425589123456}";
|
||||
Gson gson = new Gson();
|
||||
FloatExample model = gson.fromJson(json, FloatExample.class);
|
||||
assertEquals(12.123426f, model.value, 0.000001);
|
||||
}
|
||||
|
||||
@Test public void whenDeserializingHighPrecissiongNumberIntoDouble_thenShouldPerformRounding() {
|
||||
@Test
|
||||
public void whenDeserializingHighPrecissiongNumberIntoDouble_thenShouldPerformRounding() {
|
||||
String json = "{\"value\": 12.123425589123556}";
|
||||
Gson gson = new Gson();
|
||||
DoubleExample model = gson.fromJson(json, DoubleExample.class);
|
||||
|
@ -87,12 +90,12 @@ public class PrimitiveValuesUnitTest {
|
|||
}
|
||||
|
||||
|
||||
@Test public void whenDeserializingValueThatOverflows_thenShouldOverflowSilently() {
|
||||
@Test(expected = JsonSyntaxException.class)
|
||||
public void whenDeserializingValueThatOverflows_thenShouldRaiseAnException() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": \"300\"}";
|
||||
ByteExample model = gson.fromJson(json, ByteExample.class);
|
||||
|
||||
assertEquals(44, model.value);
|
||||
}
|
||||
|
||||
@Test public void whenDeserializingRealIntoByte_thenShouldRaiseAnException() {
|
||||
|
|
|
@ -41,8 +41,7 @@ public class GsonSerializeUnitTest {
|
|||
.serializeNulls()
|
||||
.disableHtmlEscaping()
|
||||
.create()
|
||||
.toJson(new JsonParser()
|
||||
.parse("{\"imdbId\":null,\"actors\":[{\"<strong>IMDB Code</strong>\":\"nm2199632\",\"<strong>Date Of Birth</strong>\":\"21-09-1982\",\"<strong>N° Film:</strong> \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}]}"));
|
||||
.toJson(JsonParser.parseString("{\"imdbId\":null,\"actors\":[{\"<strong>IMDB Code</strong>\":\"nm2199632\",\"<strong>Date Of Birth</strong>\":\"21-09-1982\",\"<strong>N° Film:</strong> \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}]}"));
|
||||
Assert.assertEquals(gson.toJson(movieWithNullValue), expectedOutput);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
|
||||
<properties>
|
||||
<jsonb-api.version>1.0</jsonb-api.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<javax.version>1.1.2</javax.version>
|
||||
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
|
||||
</properties>
|
||||
|
|
|
@ -68,7 +68,7 @@
|
|||
<networknt.json.schema.version>1.0.72</networknt.json.schema.version>
|
||||
<jsonb-api.version>1.0</jsonb-api.version>
|
||||
<yasson.version>1.0.1</yasson.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<javax.version>1.1.2</javax.version>
|
||||
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
|
||||
</properties>
|
||||
|
|
|
@ -106,7 +106,7 @@
|
|||
<google-sheets.version>v4-rev493-1.21.0</google-sheets.version>
|
||||
<docx4j.version>6.1.2</docx4j.version>
|
||||
<jaxb-runtime.version>2.3.1</jaxb-runtime.version>
|
||||
<gson.version>2.8.7</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<yamlbeans.version>1.15</yamlbeans.version>
|
||||
<apache-thrift.version>0.14.2</apache-thrift.version>
|
||||
<google-protobuf.version>3.17.3</google-protobuf.version>
|
||||
|
|
|
@ -210,7 +210,7 @@
|
|||
<kafka.version>3.3.1</kafka.version>
|
||||
<ignite.version>2.14.0</ignite.version>
|
||||
<ignite-spring-data.version>2.9.1</ignite-spring-data.version>
|
||||
<gson.version>2.9.1</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<cache.version>1.1.1</cache.version>
|
||||
<flink.version>1.5.0</flink.version>
|
||||
<hazelcast.version>5.2.0</hazelcast.version>
|
||||
|
|
|
@ -110,7 +110,7 @@
|
|||
|
||||
<properties>
|
||||
<okhttp.version>4.9.1</okhttp.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<mockwebserver.version>4.9.1</mockwebserver.version>
|
||||
<jetty.httpclient.version>1.0.3</jetty.httpclient.version>
|
||||
<jetty.server.version>9.4.19.v20190610</jetty.server.version>
|
||||
|
|
|
@ -103,7 +103,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<httpclient.version>4.5.3</httpclient.version>
|
||||
<com.squareup.okhttp3.version>4.9.1</com.squareup.okhttp3.version>
|
||||
<googleclient.version>1.23.0</googleclient.version>
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
|
||||
<properties>
|
||||
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
<properties>
|
||||
<sparkjava.spark-core.version>2.5.4</sparkjava.spark-core.version>
|
||||
<google.code.gson.version>2.8.0</google.code.gson.version>
|
||||
<google.code.gson.version>2.10.1</google.code.gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue