parseJSONGoStd parses json using encoding/json library. TODO(yevgeniy): Remove this code once we get more confidence in lexer implementation.
(s string, _ parseConfig)
| 997 | // parseJSONGoStd parses json using encoding/json library. |
| 998 | // TODO(yevgeniy): Remove this code once we get more confidence in lexer implementation. |
| 999 | func parseJSONGoStd(s string, _ parseConfig) (JSON, error) { |
| 1000 | // This goes in two phases - first it parses the string into raw interface{}s |
| 1001 | // using the Go encoding/json package, then it transforms that into a JSON. |
| 1002 | // This could be faster if we wrote a parser to go directly into the JSON. |
| 1003 | // Note: a better way to unmarshal a single JSON value would be to use |
| 1004 | // json.Unmarshal; however, we cannot do that because we want to get numbers |
| 1005 | // as strings; Alas, json.Unmarshal does not support that, and so, we have |
| 1006 | // to use json.Decoder. |
| 1007 | var result interface{} |
| 1008 | decoder := json.NewDecoder(strings.NewReader(s)) |
| 1009 | // We want arbitrary size/precision decimals, so we call UseNumber() to tell |
| 1010 | // the decoder to decode numbers into strings instead of float64s (which we |
| 1011 | // later parse using apd). |
| 1012 | decoder.UseNumber() |
| 1013 | err := decoder.Decode(&result) |
| 1014 | if err != nil { |
| 1015 | return nil, jsonDecodeError(err) |
| 1016 | } |
| 1017 | |
| 1018 | // Check to see if input has more data. |
| 1019 | // Note: using decoder.More() is wrong since it allows `]` and '}' |
| 1020 | // characters (i.e. '{}]this is BAD' will be parsed fine, and More will return |
| 1021 | // false -- thus allowing invalid JSON data to be parsed correctly). The |
| 1022 | // decoder is meant to be used in the streaming applications; not in a one-off |
| 1023 | // Unmarshal mode we are using here. Therefore, to check if input has |
| 1024 | // trailing characters, we have to attempt to decode the next value. |
| 1025 | var more interface{} |
| 1026 | if err := decoder.Decode(&more); err != io.EOF { |
| 1027 | return nil, errTrailingCharacters |
| 1028 | } |
| 1029 | return MakeJSON(result) |
| 1030 | } |
| 1031 | |
| 1032 | func jsonDecodeError(err error) error { |
| 1033 | return pgerror.Wrapf( |
no test coverage detected
searching dependent graphs…