diff --git a/pkg/yqlib/encoder.go b/pkg/yqlib/encoder.go index 7b68ec45..d050c84a 100644 --- a/pkg/yqlib/encoder.go +++ b/pkg/yqlib/encoder.go @@ -46,6 +46,10 @@ func (o *orderedMap) UnmarshalJSON(data []byte) error { // cycle through k/v var tok json.Token for tok, err = dec.Token(); !errors.Is(err, io.EOF); tok, err = dec.Token() { + if err != nil && !errors.Is(err, io.EOF) { + return err + } + // we can expect two types: string or Delim. Delim automatically means // that it is the closing bracket of the object, whereas string means // that there is another key. diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index 6745bc53..66ec1146 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -84,6 +84,13 @@ var jsonScenarios = []formatScenario{ input: `{"cat": "meow"}`, expected: "D0, P[], (!!map)::cat: meow\n", }, + { + description: "bad json", + skipDoc: true, + input: `{"a": 1 "b": 2}`, + expectedError: `bad file 'sample.yml': invalid character '"' after object key:value pair`, + scenarioType: "decode-error", + }, { description: "Parse json: complex", subdescription: "JSON is a subset of yaml, so all you need to do is prettify the output", @@ -303,7 +310,13 @@ func testJSONScenario(t *testing.T, s formatScenario) { test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false, false)), s.description) case "roundtrip-multi": test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false, false)), s.description) - + case "decode-error": + result, err := processFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false, false)) + if err == nil { + t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result) + } else { + test.AssertResultComplexWithContext(t, s.expectedError, err.Error(), s.description) + } default: panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType)) }