From 1a9a0e4efa8f54a601354a97d3fffbc65b2355f5 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 18 May 2022 17:57:07 +0100 Subject: [PATCH] Use encoding TextMarshaler & TextUnmarshaler instead json equivalents (#95) --- version.go | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/version.go b/version.go index fd1a873..e87df69 100644 --- a/version.go +++ b/version.go @@ -2,7 +2,6 @@ package version import ( "bytes" - "encoding/json" "fmt" "reflect" "regexp" @@ -390,25 +389,19 @@ func (v *Version) Original() string { return v.original } -// UnmarshalJSON implements JSON.Unmarshaler interface. -func (v *Version) UnmarshalJSON(b []byte) error { - var s string - if err := json.Unmarshal(b, &s); err != nil { - return err - } - temp, err := NewVersion(s) +// UnmarshalText implements encoding.TextUnmarshaler interface. +func (v *Version) UnmarshalText(b []byte) error { + temp, err := NewVersion(string(b)) if err != nil { return err } - v.metadata = temp.metadata - v.pre = temp.pre - v.segments = temp.segments - v.si = temp.si - v.original = temp.original + + *v = *temp + return nil } -// MarshalJSON implements JSON.Marshaler interface. -func (v Version) MarshalJSON() ([]byte, error) { - return json.Marshal(v.String()) +// MarshalText implements encoding.TextMarshaler interface. +func (v *Version) MarshalText() ([]byte, error) { + return []byte(v.String()), nil }