From 371fbf24037f96288a091e6415a48678fb06eb56 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 11 Aug 2020 18:11:13 +0900 Subject: [PATCH] Support RawMessage --- json.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/json.go b/json.go index 8675aa5..adc88b5 100644 --- a/json.go +++ b/json.go @@ -2,6 +2,7 @@ package json import ( "bytes" + "errors" "strconv" ) @@ -294,3 +295,25 @@ func (n Number) Float64() (float64, error) { func (n Number) Int64() (int64, error) { return strconv.ParseInt(string(n), 10, 64) } + +// 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 +}