Skip to content
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

Improve settings propagation for temporary JsonTreeReader and JsonTreeWriter #2151

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Marcono1234
Copy link
Collaborator

Fixes #676
Supersedes #677 (this PR here does not contain the same test code, but have verified that the test code there passed with these changes too)

Description of the issue

In some situations adapters need to:

  • first parse the JSON as JsonElement, perform modifications to it and then parse the JsonElement as desired target type
  • first write the JSON to a JsonElement, perform modifications to it and then write the JsonElement to the final JsonWriter

In these situations a temporary JsonTreeReader respectively JsonTreeWriter is used. The issue was that these readers and writers were not created with the same settings (lenientness, serialize null) as the original reader or writer. This could cause behavior mismatch or unexpected exceptions during deserialization or serialization.

Implementation notes for this PR

This PR introduces two new TypeAdapter methods: fromJsonTreeWithSettingsFrom and toJsonTreeWithSettingsFrom. Both apply the settings from an existing reader / writer to the temporary JsonTreeReader / JsonTreeWriter.
This might not be the cleanest solution, so any feedback is appreciated. However, it appears a solution similar to this is necessary because JsonTreeReader and JsonTreeWriter are internal classes and therefore cannot be used from outside of Gson (not even from the extras module at the moment), which was the proposed solution in #677.

…tings

Fixes multiple issues where a JsonTreeReader or JsonTreeWriter is used during
deserializion / serialization, but the settings from the original JsonReader
or JsonWriter (such as lenientness) are not applied.
@Marcono1234
Copy link
Collaborator Author

Needs additional @throws tags once #2172 is merged

@Marcono1234 Marcono1234 marked this pull request as draft August 7, 2022 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RuntimeTypeAdapterFactory overrides explicit control over serializeNulls behavior of contained objects
1 participant