Open
Conversation
Pretty much as implemented in bytes.Read() [1] [1]: https://golang.org/src/bytes/reader.go?s=3260:3292#L38
As the reader pointer contains the bytes for the JSON, whne it changes these
would not be valid anymore.
NOTE: If the JSON changes between 2 Read() calls the result would still be
incorrect as the 2nd read would returns bytes from the new JSON.
Not sure how this
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.
simplejson.Json implements the
io.Readerinterface:Why?
By implementing this standard interface simplejson would play nice with the standard library and all the 3rd party packages that work with an
io.Reader.For example, it would allow to POST the JSON directly rather than being forced to
Encode()and usebytes.NewReader():Implementation
When starting to read the Json it encodes the data and save the bytes in the
Readerstruct.This struct also keeps the index of the bytes read so far.
At every read the bytes are copied to the buffer argument and the index is increased.
(See
bytes.Read()implementation)The reader is reseted when
Json.datachangesI'm far from a Go expert, so happy to receive feedback.
Tests
The test builds a first
*simplejson, then a second one usingNewFromReader(), this test would fail if*simplejsondoesn't implementio.Reader.Also, to test that the second JSON is equivalent with the original one (so
Read()returns the correct bytes) I encode and compare them.