BAEL-6508 Code for the Solving Gson Parsing Errors article
This commit is contained in:
parent
a0931c50ce
commit
e42d7a6b8a
|
@ -0,0 +1,7 @@
|
|||
## GSON
|
||||
|
||||
This module contains articles about Gson
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>gson-2</artifactId>
|
||||
<name>gson-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>json-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.gson.parsingerrors;
|
||||
|
||||
public class Person {
|
||||
|
||||
public String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.gson.parsingerror;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.gson.parsingerrors.Person;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
class GsonErrorDemoUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAJsonArray_WhenTellingGsonToExpectAnObject_ThenThrows() {
|
||||
assertThrows(JsonSyntaxException.class, () -> {
|
||||
Person person = new Gson().fromJson("[{\"name\":\"John\"},{\"name\":\"James\"}]", Person.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAJsonArray_WhenParsingIntoAnArray_ThenOK() {
|
||||
Person[] personArray = new Gson().fromJson("[{\"name\":\"John\"},{\"name\":\"James\"}]", Person[].class);
|
||||
assertThat(personArray).extracting(Person::getName)
|
||||
.containsExactly("John", "James");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAJsonArray_WhenParsingIntoACollection_ThenOK() {
|
||||
Type collectionType = new TypeToken<Collection<Person>>() {
|
||||
}.getType();
|
||||
Collection<Person> personCollection = new Gson().fromJson("[{\"name\":\"John\"},{\"name\":\"James\"}]", collectionType);
|
||||
assertThat(personCollection).extracting(Person::getName)
|
||||
.containsExactly("John", "James");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAJsonObject_WhenTellingGsonToExpectAnArray_ThenThrows() {
|
||||
assertThrows(JsonSyntaxException.class, () -> {
|
||||
Person[] personArray = new Gson().fromJson("{\"name\":\"John\"}", Person[].class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAJsonObject_WhenParsingIntoAnObject_ThenOK() {
|
||||
Person person = new Gson().fromJson("{\"name\":\"John\"}", Person.class);
|
||||
assertEquals("John", person.getName());
|
||||
}
|
||||
|
||||
}
|
|
@ -18,6 +18,7 @@
|
|||
<module>json-2</module>
|
||||
<module>json-path</module>
|
||||
<module>gson</module>
|
||||
<module>gson-2</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
|
Loading…
Reference in New Issue