json: add struct tag option to wrap literals in strings

Since JavaScript doesn't have [u]int64 types, some JSON APIs
encode such types as strings to avoid losing precision.

This adds a new struct tag option ",string" to cause
fields to be wrapped in JSON strings on encoding
and unwrapped from strings when decoding.

R=rsc, gustavo
CC=golang-dev
https://golang.org/cl/4918051
This commit is contained in:
Brad Fitzpatrick 2011-08-29 12:46:32 -07:00
parent 2afc46f2b2
commit 596bf61d6c
7 changed files with 175 additions and 32 deletions

View file

@ -265,6 +265,8 @@ type All struct {
Foo string `json:"bar"`
Foo2 string `json:"bar2,dummyopt"`
IntStr int64 `json:",string"`
PBool *bool
PInt *int
PInt8 *int8
@ -333,6 +335,7 @@ var allValue = All{
Float64: 15.1,
Foo: "foo",
Foo2: "foo2",
IntStr: 42,
String: "16",
Map: map[string]Small{
"17": {Tag: "tag17"},
@ -394,6 +397,7 @@ var allValueIndent = `{
"Float64": 15.1,
"bar": "foo",
"bar2": "foo2",
"IntStr": "42",
"PBool": null,
"PInt": null,
"PInt8": null,
@ -485,6 +489,7 @@ var pallValueIndent = `{
"Float64": 0,
"bar": "",
"bar2": "",
"IntStr": "0",
"PBool": true,
"PInt": 2,
"PInt8": 3,