goccy / go-json

Fast JSON encoder/decoder compatible with encoding/json for Go
MIT License
3.11k stars 148 forks source link

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

Open AsafMah opened 2 months ago

AsafMah commented 2 months ago

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.