-
Notifications
You must be signed in to change notification settings - Fork 53.3k
Bael 7805 how to fix json io exception failed making field property accessible in gson #19293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MBuczkowski2025
wants to merge
15
commits into
eugenp:master
Choose a base branch
from
MBuczkowski2025:BAEL-7805_How_to_fix_JsonIOException_Failed_making_field_property_accessible_in_Gson
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
037df57
BAEL-7805 new module and code added
MBuczkowski2025 8680734
BAEL-7805 removing unnecessary files, format checking
MBuczkowski2025 ef3482d
BAEL-7805 minor fixes
MBuczkowski2025 94a64da
Merge branch 'master' into BAEL-7805_How_to_fix_JsonIOException_Faile…
MBuczkowski2025 3753c06
BAEL-7805 changes after Code Review
MBuczkowski2025 ad79e4b
BAEL-7805 validaton code and tests removed
MBuczkowski2025 e3eaa0d
BAEL-7805 changes after Code Review, cont.
MBuczkowski2025 a461a1d
BAEL-7805 changes after Code Review, cont.
MBuczkowski2025 be445b0
BAEL-7805 changes after Code Review, cont.
MBuczkowski2025 88a9b48
BAEL-7805 changes after Code Review, cont.
MBuczkowski2025 af29485
BAEL-7805 pom.xml fix.
MBuczkowski2025 2c42e30
BAEL-7805 adding class and tests for LocalDate
MBuczkowski2025 9137581
BAEL-7805 fix Gson version
MBuczkowski2025 31bc8dd
BAEL-7805 minor fixes
MBuczkowski2025 7a12e46
BAEL-7805 changes after CR, mainly formatting and logging
MBuczkowski2025 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
|
|
||
| <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> | ||
|
|
||
23 changes: 23 additions & 0 deletions
23
json-modules/gson-4/gson-module-opens/src/main/java/gson/exception/ConferencePojo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
...modules/gson-4/gson-module-opens/src/main/java/gson/exception/ConferencePojoWithDate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
json-modules/gson-4/gson-module-opens/src/main/java/gson/exception/GsonModuleMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
MBuczkowski2025 marked this conversation as resolved.
|
||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
json-modules/gson-4/gson-module-opens/src/main/java/module-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
32 changes: 32 additions & 0 deletions
32
...dules/gson-4/gson-module-opens/src/test/java/gson/exception/ModularOpensGsonUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
|
|
||
| } |
17 changes: 17 additions & 0 deletions
17
.../gson-module-opens/src/test/java/gson/exception/ModularStructureConfirmationUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | ||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
46
...ules/gson-4/gson-module-opens/src/test/java/gson/exception/PojoWithLocalDateUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
23 changes: 23 additions & 0 deletions
23
json-modules/gson-4/gson-module/src/main/java/gson/exception/ConferencePojo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
json-modules/gson-4/gson-module/src/main/java/gson/exception/ConferencePojoAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
json-modules/gson-4/gson-module/src/main/java/gson/exception/ConferencePojoPublic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
5 changes: 5 additions & 0 deletions
5
json-modules/gson-4/gson-module/src/main/java/gson/exception/ConferenceRecord.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package gson.exception; | ||
|
|
||
| public record ConferenceRecord(String name, int numberOfParticipants) { | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
json-modules/gson-4/gson-module/src/main/java/gson/exception/GsonModuleMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
MBuczkowski2025 marked this conversation as resolved.
|
||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
json-modules/gson-4/gson-module/src/main/java/module-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.