pkg/guid: Switch from json.Marshaler to encoding.TextMarshaler

Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
This commit is contained in:
Kevin Parsons
2019-05-13 14:31:21 -07:00
parent b940a6f7ae
commit 3dac69f6ea
+10 -12
View File
@@ -7,11 +7,10 @@ package guid
import ( import (
"crypto/rand" "crypto/rand"
"encoding"
"encoding/binary" "encoding/binary"
"encoding/json"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
@@ -36,8 +35,8 @@ const (
// hash of an input string. // hash of an input string.
type Version uint8 type Version uint8
var _ = (json.Marshaler)(GUID{}) var _ = (encoding.TextMarshaler)(GUID{})
var _ = (json.Unmarshaler)(&GUID{}) var _ = (encoding.TextUnmarshaler)(&GUID{})
// GUID represents a GUID/UUID. It has the same structure as // 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 // 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) return Version((g.Data3 & 0xF000) >> 12)
} }
// MarshalJSON marshals the GUID to JSON representation and returns it as a // MarshalText returns the textual representation of the GUID.
// slice of bytes. func (g GUID) MarshalText() ([]byte, error) {
func (g GUID) MarshalJSON() ([]byte, error) { return []byte(g.String()), nil
return json.Marshal(g.String())
} }
// UnmarshalJSON unmarshals a GUID from JSON representation and sets itself to // UnmarshalText takes the textual representation of a GUID, and unmarhals it
// the unmarshaled GUID. // into this GUID.
func (g *GUID) UnmarshalJSON(data []byte) error { func (g *GUID) UnmarshalText(text []byte) error {
g2, err := FromString(strings.Trim(string(data), "\"")) g2, err := FromString(string(text))
if err != nil { if err != nil {
return err return err
} }