ReadNextDocJSON reads the next NDJSON line and returns it as a NetworkTrafficDocJSON with the spec kept as a json.RawMessage. Only valid when the reader's format is JSON; panics (via error) otherwise. This is the read-side companion to EncodeMockJSON: the entire round-trip for a JSON mocks file can
()
| 212 | // for a JSON mocks file can now stay on encoding/json with no yaml.Node |
| 213 | // allocation or gopkg.in/yaml.v3 emit/parse. |
| 214 | func (r *MockReader) ReadNextDocJSON() (*NetworkTrafficDocJSON, error) { |
| 215 | if r.format != FormatJSON { |
| 216 | return nil, fmt.Errorf("mockreader: ReadNextDocJSON called on %s reader", r.format) |
| 217 | } |
| 218 | data, err := r.ReadNextDocument() |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | if len(bytes.TrimSpace(data)) == 0 { |
| 223 | return r.ReadNextDocJSON() |
| 224 | } |
| 225 | var doc NetworkTrafficDocJSON |
| 226 | if err := json.Unmarshal(data, &doc); err != nil { |
| 227 | return nil, fmt.Errorf("failed to decode JSON at line %d: %w", r.lineNum, err) |
| 228 | } |
| 229 | return &doc, nil |
| 230 | } |
| 231 | |
| 232 | // Close closes the file. |
| 233 | func (r *MockReader) Close() error { |