Merge pull request #14094 from thibaultfaure/article/BAEL-6508

BAEL-6508 Code for the Solving Gson Parsing Errors article
This commit is contained in:
davidmartinezbarua 2023-05-24 12:18:58 -03:00 committed by GitHub
commit 98e392f9de
5 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,7 @@
## GSON
This module contains articles about Gson
### Relevant Articles:

View File

@ -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>

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -18,6 +18,7 @@
<module>json-2</module> <module>json-2</module>
<module>json-path</module> <module>json-path</module>
<module>gson</module> <module>gson</module>
<module>gson-2</module>
</modules> </modules>
<build> <build>