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.Reader
interface: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
Reader
struct.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.data
changesI'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*simplejson
doesn'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.