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/ #
MessagesTotal messages: 16
|