From 3dac69f6eaf77472d472dd9347db16a624ce7c4b Mon Sep 17 00:00:00 2001 From: Kevin Parsons Date: Mon, 13 May 2019 14:31:21 -0700 Subject: [PATCH] pkg/guid: Switch from json.Marshaler to encoding.TextMarshaler Signed-off-by: Kevin Parsons --- pkg/guid/guid.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pkg/guid/guid.go b/pkg/guid/guid.go index 58895dd..d0595f6 100644 --- a/pkg/guid/guid.go +++ b/pkg/guid/guid.go @@ -7,11 +7,10 @@ package guid import ( "crypto/rand" + "encoding" "encoding/binary" - "encoding/json" "fmt" "strconv" - "strings" "golang.org/x/sys/windows" ) @@ -36,8 +35,8 @@ const ( // hash of an input string. type Version uint8 -var _ = (json.Marshaler)(GUID{}) -var _ = (json.Unmarshaler)(&GUID{}) +var _ = (encoding.TextMarshaler)(GUID{}) +var _ = (encoding.TextUnmarshaler)(&GUID{}) // GUID represents a GUID/UUID. It has the same structure as // golang.org/x/sys/windows.GUID so that it can be used with functions expecting @@ -171,16 +170,15 @@ func (g GUID) Version() Version { return Version((g.Data3 & 0xF000) >> 12) } -// MarshalJSON marshals the GUID to JSON representation and returns it as a -// slice of bytes. -func (g GUID) MarshalJSON() ([]byte, error) { - return json.Marshal(g.String()) +// MarshalText returns the textual representation of the GUID. +func (g GUID) MarshalText() ([]byte, error) { + return []byte(g.String()), nil } -// UnmarshalJSON unmarshals a GUID from JSON representation and sets itself to -// the unmarshaled GUID. -func (g *GUID) UnmarshalJSON(data []byte) error { - g2, err := FromString(strings.Trim(string(data), "\"")) +// UnmarshalText takes the textual representation of a GUID, and unmarhals it +// into this GUID. +func (g *GUID) UnmarshalText(text []byte) error { + g2, err := FromString(string(text)) if err != nil { return err }