Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions json-modules/gson-4/gson-module-opens/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
Comment thread
MBuczkowski2025 marked this conversation as resolved.

<parent>
<groupId>com.baeldung</groupId>
<artifactId>gson-4</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>gson-module-opens</artifactId>
<packaging>jar</packaging>
</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gson.exception;

public class ConferencePojo {

private String name;
private int numberOfParticipants;

public String getName() {
return name;
}

public int getNumberOfParticipants() {
return numberOfParticipants;
}

public void setName(String name) {
this.name = name;
}

public void setNumberOfParticipants(int numberOfParticipants) {
this.numberOfParticipants = numberOfParticipants;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gson.exception;

import java.time.LocalDate;

public class ConferencePojoWithDate {

private String name;
private int numberOfParticipants;
private LocalDate conferenceStart;

public String getName() {
return name;
}

public int getNumberOfParticipants() {
return numberOfParticipants;
}

public void setName(String name) {
this.name = name;
}

public void setNumberOfParticipants(int numberOfParticipants) {
this.numberOfParticipants = numberOfParticipants;
}

public LocalDate getConferenceStart() {
return conferenceStart;
}

public void setConferenceStart(LocalDate conferenceStart) {
this.conferenceStart = conferenceStart;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gson.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

public class GsonModuleMain {

static Logger log = LoggerFactory.getLogger(GsonModuleMain.class);

public static void main(String[] args) {

String moduleName = GsonModuleMain.class.getModule()
.getName();

if (moduleName == null) {
log.info("Mode: [ Class Path ] (Class in the Unnamed Module)");
} else {
log.info("Mode: [ Module Path ] - Module name: " + moduleName);
}

Gson gson = new Gson();
String json = "{\"name\":\"Java Conference\"}";

try {
ConferencePojo pojo = gson.fromJson(json, ConferencePojo.class);
log.info("Deserialization successful! Object " + pojo);
} catch (Exception e) {
log.error("Expected exception caught!", e);
}
Comment thread
MBuczkowski2025 marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module gson.exception {

requires com.google.gson;
requires org.slf4j;

opens gson.exception to com.google.gson;

exports gson.exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gson.exception;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

import com.google.gson.Gson;

class ModularOpensGsonUnitTest {

@Test
void givenModularAndOpens_whenDeserializingPojo_thenSuccess() {
String json = """
{
"name": "Java Conference",
"numberOfParticipants": 150
}
""";

Gson gson = new Gson();

ConferencePojo result = assertDoesNotThrow(() -> {
return gson.fromJson(json, ConferencePojo.class);
});

assertNotNull(result);
assertEquals("Java Conference", result.getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gson.exception;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class ModularStructureConfirmationUnitTest {

@Test
void whenModular_thenModuleIsNamed() {
Module module = this.getClass()
.getModule();

assertTrue(module.isNamed(), "Test run on Classpath, JPMS strong encapsulation won't work!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gson.exception;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.LocalDate;

import org.junit.jupiter.api.Test;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

class PojoWithLocalDateUnitTest {

@Test
void whenObjectDateFormat_thenSuccessfulDeserialization() {
String correctJson = """
{
name:"Java Conference",
numberOfParticipants:500,
conferenceStart:{"year":2026,"month":8,"day":17}
Comment on lines +19 to +21

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are missing "" around the fields here - this is only working by accident because Gson has lenient parsing by default

And please normalise the formatting to the standard indentation style

}
""";

Gson gson = new Gson();
ConferencePojoWithDate result = gson.fromJson(correctJson, ConferencePojoWithDate.class);

LocalDate expectedDate = LocalDate.of(2026, 8, 17);
assertEquals(expectedDate, result.getConferenceStart(), "Date should be the same as in JSON");
}

@Test
void whenISOTextFormat_thenJsonSyntaxException() {
String wrongDateInJson = """
{
name:"Java Conference",
numberOfParticipants:500,
conferenceStart:"2026-08-17"
}
""";
Gson gson = new Gson();
assertThrows(JsonSyntaxException.class, () -> {
gson.fromJson(wrongDateInJson, ConferencePojoWithDate.class);
}, "JsonSyntaxException was expected due to an incompatible date format");
}
}
15 changes: 15 additions & 0 deletions json-modules/gson-4/gson-module/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>gson-4</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>gson-module</artifactId>
<packaging>jar</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gson.exception;

public class ConferencePojo {

private String name;
private int numberOfParticipants;

public String getName() {
return name;
}

public int getNumberOfParticipants() {
return numberOfParticipants;
}

public void setName(String name) {
this.name = name;
}

public void setNumberOfParticipants(int numberOfParticipants) {
this.numberOfParticipants = numberOfParticipants;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gson.exception;

import java.io.IOException;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

public class ConferencePojoAdapter extends TypeAdapter<ConferencePojo> {

@Override
public void write(JsonWriter out, ConferencePojo value) throws IOException {
throw new UnsupportedOperationException("This adapter is for deserialization only!");
}

@Override
public ConferencePojo read(JsonReader in) throws IOException {
String name = null;
int numberOfParticipants = 0;

in.beginObject();
while (in.hasNext()) {
String key = in.nextName();
if ("name".equals(key)) {
name = in.nextString();
} else if ("numberOfParticipants".equals(key)) {
numberOfParticipants = in.nextInt();
} else {
in.skipValue();
}
}
in.endObject();

ConferencePojo result = new ConferencePojo();
result.setName(name);
result.setNumberOfParticipants(numberOfParticipants);

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gson.exception;

public class ConferencePojoPublic {

public String name;
public int numberOfParticipants;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gson.exception;

public record ConferenceRecord(String name, int numberOfParticipants) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gson.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

public class GsonModuleMain {

static Logger log = LoggerFactory.getLogger(GsonModuleMain.class);

public static void main(String[] args) {

String moduleName = GsonModuleMain.class.getModule()
.getName();

if (moduleName == null) {
log.info("Mode: [ Class Path ] (Class in the Unnamed Module)");
} else {
log.info("Mode: [ Module Path ] - Module name: " + moduleName);
}

Gson gson = new Gson();
String json = "{\"name\":\"Java Conference\"}";

try {
ConferencePojo pojo = gson.fromJson(json, ConferencePojo.class);
log.info("Deserialization successful! Object " + pojo);
} catch (Exception e) {
log.error("Expected exception caught!", e);
}
Comment thread
MBuczkowski2025 marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gson.exception {

requires com.google.gson;
requires org.slf4j;

exports gson.exception;
}
Loading