From b82b685c982b26399c940fc89be030f343570192 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Tue, 16 Jan 2024 15:35:55 -0800 Subject: [PATCH] Use a type alias for RawMessage to avoid breaking the public API of Operation --- v5/internal/json/stream.go | 24 ++---------------------- v5/patch_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/v5/internal/json/stream.go b/v5/internal/json/stream.go index 1442ef2..5598ce1 100644 --- a/v5/internal/json/stream.go +++ b/v5/internal/json/stream.go @@ -6,7 +6,7 @@ package json import ( "bytes" - "errors" + "encoding/json" "io" ) @@ -259,27 +259,7 @@ func (enc *Encoder) SetEscapeHTML(on bool) { // RawMessage is a raw encoded JSON value. // It implements Marshaler and Unmarshaler and can // be used to delay JSON decoding or precompute a JSON encoding. -type RawMessage []byte - -// MarshalJSON returns m as the JSON encoding of m. -func (m RawMessage) MarshalJSON() ([]byte, error) { - if m == nil { - return []byte("null"), nil - } - return m, nil -} - -// UnmarshalJSON sets *m to a copy of data. -func (m *RawMessage) UnmarshalJSON(data []byte) error { - if m == nil { - return errors.New("json.RawMessage: UnmarshalJSON on nil pointer") - } - *m = append((*m)[0:0], data...) - return nil -} - -var _ Marshaler = (*RawMessage)(nil) -var _ Unmarshaler = (*RawMessage)(nil) +type RawMessage = json.RawMessage // A Token holds a value of one of these types: // diff --git a/v5/patch_test.go b/v5/patch_test.go index b9d5e46..1f807f5 100644 --- a/v5/patch_test.go +++ b/v5/patch_test.go @@ -1236,3 +1236,12 @@ func TestMaintainOrderingIndented(t *testing.T) { }) } } + +// This is a compile time check that encoding/json's RawMessage can be used in Operation +func init() { + msg := json.RawMessage([]byte(`1`)) + + _ = Operation{ + "foo": &msg, + } +}