Contribution of JRef-based json pointer support#3050
Open
scottslewis wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The purpose of this pull request is to add support for the enhancement #1443.
The pr implements the JRef 'local-only' specification. Local-pnly means that only json pointers starting with '#' (local to the document) are supported.
The main two classes for the implementation are JRefTypeAdapter and JRefTypeAdapterFactory. The JRefTypeAdapterFactory implements the Gson TypeAdapterFactory and creates instances of JRefTypeAdapter.
JRefTypeAdapter implements Gson TypeAdapter and so implements the write (serialization) and read (deserialization) with JRef support.
Two support classes from the Jackson 3.0 databind project have been included as part of this contribution, with permission from the Jackson maintainer: @cowtowncoder. Please see this dialog about these two classes. The two classes from Jackson are:
JsonPointer (implementation of the rfc-6901 json pointer specification for local-only json pointers). Also includes parent/child ptr creation/validation/compare code.
TokenStreamContext - Allows the creation of a context for parsers (like Jackson and Gson) that treat the wirting and reading of json as a sequentially processed stream of tokens. This class is used within JsonPointer to go back and forth between JsonPointers as strings (e.g. /foo/bar/0) and JsonPointers as parent and child contexts (e.g. /foo parent for bar parent for /0 container element 0).
The JRefTypeAdapter. uses the JsonReader.getPath() (json path syntax...e.g. $foo, $bar[0]) in order to get a contextual name from the token stream, and contruct JsonPointer instances during deserialization. See JRefTypeAdapter.read(JsonReader) and JRefTypeAdapter.getJsonPointerFromJsonPath methods.
The only modification to an existing Gson class, other than new classes described above is the addition of a single method to JsonWriter class
This method is used in the JRefTypeAdapter.write method to get the deferred name in serialization context where required (e.g. objects, and containers).
Also included in this pr are test classes, in the test src tree in new package named com.google.gson.jref package.
All tests are passing using junit 4.
I'm happy to break this up into multiple prs (e.g. gson changes, jref impl, tests) if desired. Or to refactor.