parseUsingFastParser parses string as JSON using fast json parser.
(s string, cfg parseConfig)
| 18 | |
| 19 | // parseUsingFastParser parses string as JSON using fast json parser. |
| 20 | func parseUsingFastParser(s string, cfg parseConfig) (JSON, error) { |
| 21 | input, err := unsafeGetBytes(s) |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | |
| 26 | p := fastJSONParser{ |
| 27 | parseConfig: cfg, |
| 28 | decoder: tokenizer.MakeDecoder(input), |
| 29 | state: (*fastJSONParser).parseTopValue, |
| 30 | } |
| 31 | defer p.decoder.Release() |
| 32 | |
| 33 | j, err := p.parse() |
| 34 | if err != nil { |
| 35 | if errors.Is(err, io.ErrUnexpectedEOF) && p.decoder.More() { |
| 36 | // JSON scanner returns nil token if it encounters an invalid input |
| 37 | // character. In such cases, decoder returns io.ErrUnexpectedEOF error. |
| 38 | // However, we know it's not an EOF because decoder has more data. So, |
| 39 | // produce a bit nicer error message. |
| 40 | return nil, jsonDecodeError(decodeErrorContext(errInvalidInputToken, s, p.decoder.Pos())) |
| 41 | } |
| 42 | return nil, jsonDecodeError(decodeErrorContext(err, s, p.decoder.Pos())) |
| 43 | } |
| 44 | |
| 45 | if j == nil { |
| 46 | return nil, errors.AssertionFailedf("expected parsed JSON value, got nil") |
| 47 | } |
| 48 | |
| 49 | if p.decoder.More() { |
| 50 | return nil, jsonDecodeError(decodeErrorContext(errTrailingCharacters, s, p.decoder.Pos()+1)) |
| 51 | } |
| 52 | |
| 53 | return j, nil |
| 54 | } |
| 55 | |
| 56 | // fastJSONParser builds JSON given input string. This implementation uses low level |
| 57 | // API provided by fork of github.com/pkg/json package to implement direct |
no test coverage detected
searching dependent graphs…