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{})
| 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. |
| 163 | func 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. |
no outgoing calls