Encode json.Number as a number, not a quoted string - #445
Open
vsaraikin wants to merge 1 commit into
Open
Conversation
The decoder has a custom json.Number path (in.JsonNumber()), but the encoder treated json.Number as its underlying string kind and emitted it quoted, so round-tripping changed the JSON type from number to string. Add the encoder counterpart: customEncoders routes json.Number to a new jwriter.JsonNumber, which writes the literal raw, encodes the empty value as 0 and errors on invalid literals, matching encoding/json. Fixes mailru#220.
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.
Fixes #220
json.Numberfields are generated asout.String(string(v)), so they come out quoted:encoding/json writes them as raw numbers. Decoding was already brought in line in #145, #163 and #172, encoding was left behind.
This adds a
customEncodersmap ingen, mirroring the existingcustomDecoders, sojson.Numberroutes to a newWriter.JsonNumberinstead of the string encoder. The writer follows encoding/json: an empty Number is written as0and an invalid literal setsw.Errorrather than emitting broken JSON.isValidNumberis the grammar check copied from encoding/json so the accepted set is identical.tests/json_number.gocovers the round trip through the generated code, plus jwriter tests for the empty and invalid cases. The Makefile picks up the new fixture and adds./jwriterto the test target, which was not being run.Reverting only the
genchange makes the new case fail with the quoted output above