Skip to content

decoder.InputOffset() reports wrong result when having nested quotes #520

Open
@AsafMah

Description

When decoding, it seems that the internal buffer stores the data without nested quotes, and its offsets seem to be reported for this buffer.

So the InputOffset() method will report the wrong offset compared to the original input and to encoding/json.

Example:

func TestDecode(t *testing.T) {
	dec := json.NewDecoder(bytes.NewReader([]byte(`{"test":"\""}`)))
	offset := dec.InputOffset()
	if offset != 0 {
		t.Errorf("expected 0, got %d", offset)
	}

	var v map[string]interface{}
	err := dec.Decode(&v)

	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if v["test"] != "\"" {
		t.Errorf("expected [\"a\",\"b\"], got %v", v["test"])
	}

	offset = dec.InputOffset()

	if offset != 13 {
		t.Errorf("expected offset to be 13, got %d", offset)
	}
}

When using encoding/json: passes

when using goccy/go-json: expected offset to be 13, got 12.

The discrepancy will get bigger the more nested quotes there are.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions