Skip to content

Commit

Permalink
Use a type alias for RawMessage to avoid breaking the public API of O…
Browse files Browse the repository at this point in the history
…peration
  • Loading branch information
evanphx committed Jan 16, 2024
1 parent 05c9526 commit b82b685
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
24 changes: 2 additions & 22 deletions v5/internal/json/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package json

import (
"bytes"
"errors"
"encoding/json"
"io"
)

Expand Down Expand Up @@ -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:
//
Expand Down
9 changes: 9 additions & 0 deletions v5/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

0 comments on commit b82b685

Please sign in to comment.