MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / FixJSONNumbers

Function FixJSONNumbers

base/util.go:163–181  ·  view source on GitHub ↗

This is a workaround for an incompatibility between Go's JSON marshaler and CouchDB. Go parses JSON numbers into float64 type, and then when it marshals float64 to JSON it uses scientific notation if the number is more than six digits long, even if it's an integer. However, CouchDB doesn't seem to l

(value interface{})

Source from the content-addressed store, hash-verified

161// TODO: In Go 1.1 we will be able to use a new option in the JSON parser that converts numbers
162// to a special number type that preserves the exact formatting.
163func FixJSONNumbers(value interface{}) interface{} {
164 switch value := value.(type) {
165 case float64:
166 var asInt int64 = int64(value)
167 if float64(asInt) == value {
168 return asInt // Representable as int, so return it as such
169 }
170 case map[string]interface{}:
171 for k, v := range value {
172 value[k] = FixJSONNumbers(v)
173 }
174 case []interface{}:
175 for i, v := range value {
176 value[i] = FixJSONNumbers(v)
177 }
178 default:
179 }
180 return value
181}
182
183// Convert a JSON string, which has extra double quotes (eg, `"thing"`) into a normal string
184// with the extra double quotes removed (eg "thing"). Normal strings will be returned as-is.

Callers 3

handleBulkDocsMethod · 0.92
FixJSONNumbersMethod · 0.92
TestFixJSONNumbersFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestFixJSONNumbersFunction · 0.68