Improve settings propagation for temporary JsonTreeReader
and JsonTreeWriter
#2151
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.
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:
JsonElement
, perform modifications to it and then parse theJsonElement
as desired target typeJsonElement
, perform modifications to it and then write theJsonElement
to the finalJsonWriter
In these situations a temporary
JsonTreeReader
respectivelyJsonTreeWriter
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
andtoJsonTreeWithSettingsFrom
. Both apply the settings from an existing reader / writer to the temporaryJsonTreeReader
/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
andJsonTreeWriter
are internal classes and therefore cannot be used from outside of Gson (not even from theextras
module at the moment), which was the proposed solution in #677.