Skip to content

Contribution of JRef-based json pointer support#3050

Open
scottslewis wants to merge 1 commit into
google:mainfrom
scottslewis:issue_1443
Open

Contribution of JRef-based json pointer support#3050
scottslewis wants to merge 1 commit into
google:mainfrom
scottslewis:issue_1443

Conversation

@scottslewis

Copy link
Copy Markdown

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

  public String getDeferredName() {
	  return this.deferredName;
  }

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.

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.

1 participant