Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3882)

Issue 6202068: code review 6202068: encoding/json: Adding json.NumberConverter for cust... (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
11 years, 11 months ago by jgold.bg
Modified:
11 years, 10 months ago
Reviewers:
CC:
rsc, iant2, golang-dev
Visibility:
Public.

Description

encoding/json: Adding json.NumberConverter for custom number conversion. The default behavior of the JSON library is to convert numbers to float64 when the decode target is of type interface{}. This makes it difficult to have any control over the handling of JSON numbers in some cases. One such example would be a JSON-based interchange format in which the caller has no knowledge of the structure of documents yet still wants to store/forward documents in a lossless but efficient way, perhaps passing the number literals through unchanged, or decoding them differently based on whether they are floating point or integral. Some discussion around this issue exists. In this thread: https://groups.google.com/d/msg/golang-nuts/oXbGCPh1e0Y/3j9tB6Eka8oJ Russ points out that the JSON spec nor the JavaScript language don't distinguish between "3" and "3.0", which is of course valid. That being said, not all applications working with JSON are destined to have anything to do with JavaScript, and having a way to opt-in to a different interpretation than that dictated by JavaScript implementations seems reasonable. His response indicated a suggested fix but no implementation plan. Hopefully the implementation of NumberConverter in this change allows for a similar, though slightly lower-level, approach to the same end result (nor does it preclude his suggested fix at a later time). In this discussion: https://groups.google.com/d/msg/golang-nuts/HGuVS-DJVh4/L_Wnat9R57AJ Russ also suggests another way to get access to handling of JSON number literals, which is to tap in to (and presumably expose somehow) scanner.step. This isn't something I'd feel comfortable tinkering around with myself, though I'm aware that it might also suffice (though would also require me to take over control of the rest of the document parse, I believe). I hope that this can find its way in to the codebase, and appreciate your time.

Patch Set 1 #

Patch Set 2 : diff -r 2d8e9028098e https://code.google.com/p/go/ #

Patch Set 3 : diff -r 2d8e9028098e https://code.google.com/p/go/ #

Patch Set 4 : diff -r 4c333000f50b https://code.google.com/p/go/ #

Total comments: 12

Patch Set 5 : diff -r 2f24d335beeb https://code.google.com/p/go/ #

Patch Set 6 : diff -r 2f24d335beeb https://code.google.com/p/go/ #

Total comments: 4

Patch Set 7 : diff -r d5d55e8e2f51 https://code.google.com/p/go/ #

Total comments: 2

Patch Set 8 : diff -r f6da295143d6 https://code.google.com/p/go/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+143 lines, -11 lines) Patch
M src/pkg/encoding/json/decode.go View 1 2 3 4 5 6 7 5 chunks +40 lines, -4 lines 0 comments Download
M src/pkg/encoding/json/decode_test.go View 1 2 3 4 5 6 7 9 chunks +90 lines, -6 lines 0 comments Download
M src/pkg/encoding/json/encode.go View 1 2 3 4 5 6 2 chunks +9 lines, -1 line 0 comments Download
M src/pkg/encoding/json/stream.go View 1 2 3 4 1 chunk +4 lines, -0 lines 0 comments Download

Messages

Total messages: 16
jgold.bg
Hello golang-dev@googlegroups.com, I'd like you to review this change to https://code.google.com/p/go/
11 years, 11 months ago (2012-05-11 18:08:06 UTC) #1
rsc
Introducing number, as I suggested in 2010, was probably the best answer, but it never ...
11 years, 11 months ago (2012-05-15 15:07:18 UTC) #2
jgold.bg
Thanks for checking back in. I'm happy with either, and am happy to modify my ...
11 years, 11 months ago (2012-05-15 16:30:42 UTC) #3
rsc
Okay, let's add a type Number like in my 2010 mail and enable its use ...
11 years, 11 months ago (2012-05-22 02:48:06 UTC) #4
jgold.bg
Hello golang-dev@googlegroups.com, rsc@golang.org (cc: golang-dev@googlegroups.com), Please take another look.
11 years, 11 months ago (2012-05-23 19:03:54 UTC) #5
jgold.bg
ping On Wed, May 23, 2012 at 07:03:54PM +0000, jgold.bg@gmail.com wrote: > Hello golang-dev@googlegroups.com, rsc@golang.org ...
11 years, 10 months ago (2012-05-29 20:05:05 UTC) #6
rsc
Thanks for working on this. The one-sentence summary of my comments would be "write less". ...
11 years, 10 months ago (2012-05-29 21:19:23 UTC) #7
jgold.bg
On Tue, May 29, 2012 at 09:19:24PM +0000, rsc@golang.org wrote: > Thanks for working on ...
11 years, 10 months ago (2012-05-30 03:57:22 UTC) #8
jgold.bg
Ping? Sorry if I'm being pesky or too pushy -- I'm new to the team's ...
11 years, 10 months ago (2012-06-01 15:47:04 UTC) #9
iant2
Jonathan Gold <jgold.bg@gmail.com> writes: > Ping? > > Sorry if I'm being pesky or too ...
11 years, 10 months ago (2012-06-02 00:19:45 UTC) #10
rsc
Sorry for the delay. I have been busy with some other things. http://codereview.appspot.com/6202068/diff/21001/src/pkg/encoding/json/decode.go File src/pkg/encoding/json/decode.go ...
11 years, 10 months ago (2012-06-06 21:12:03 UTC) #11
jgold.bg
PTAL. Thanks for the suggestion about letting Int64()/Float64() return the strconv errors. I originally assumed ...
11 years, 10 months ago (2012-06-07 01:53:24 UTC) #12
jgold.bg
Ping. On 2012/06/07 01:53:24, jgold.bg wrote: > PTAL. > > Thanks for the suggestion about ...
11 years, 10 months ago (2012-06-14 02:15:56 UTC) #13
rsc
Looks good, sorry for the delay. Please fix these two doc nits and I'll submit. ...
11 years, 10 months ago (2012-06-25 15:31:10 UTC) #14
jgold.bg
Done (assuming your comments below were sed-style edits). jonathan On Mon, Jun 25, 2012 at ...
11 years, 10 months ago (2012-06-25 16:33:13 UTC) #15
rsc
11 years, 10 months ago (2012-06-25 21:36:14 UTC) #16
*** Submitted as http://code.google.com/p/go/source/detail?r=73dfe1ecc9ac ***

encoding/json: add Number type

Number represents the actual JSON text,
preserving the precision and
formatting of the original input.

R=rsc, iant
CC=golang-dev
http://codereview.appspot.com/6202068

Committer: Russ Cox <rsc@golang.org>
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b