From 44f0222571389d8052346fc79635b3ba7d8e7191 Mon Sep 17 00:00:00 2001 From: Tony Seebregts Date: Tue, 5 Jan 2021 15:50:34 -0800 Subject: [PATCH] Updated types.Version.MarshalJSON for newly implemented Stringer interface --- types/version.go | 2 +- types/version_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/types/version.go b/types/version.go index 40c3d98..a70ec13 100644 --- a/types/version.go +++ b/types/version.go @@ -30,7 +30,7 @@ func (v *Version) UnmarshalUT0311L0x(bytes []byte) (interface{}, error) { } func (v Version) MarshalJSON() ([]byte, error) { - return json.Marshal(fmt.Sprintf("%04x", v)) + return json.Marshal(fmt.Sprintf("%04x", uint16(v))) } func (v *Version) UnmarshalJSON(bytes []byte) error { diff --git a/types/version_test.go b/types/version_test.go index 24af3c9..50fad38 100644 --- a/types/version_test.go +++ b/types/version_test.go @@ -13,3 +13,27 @@ func TestVersionStringer(t *testing.T) { t.Errorf("Invalid string value - expected:%s, got:%s", "6.62", s) } } + +func TestMarshalJSON(t *testing.T) { + v := Version(0x0662) + b, err := v.MarshalJSON() + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + if string(b) != `"0662"` { + t.Errorf("Invalid JSON value - expected:%s, got:%s", `"0662"`, string(b)) + } +} + +func TestUnmarshalJSON(t *testing.T) { + v := Version(0) + err := v.UnmarshalJSON([]byte(`"0662"`)) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + if uint16(v) != 0x0662 { + t.Errorf("Invalid version - expected:%v, got:%v", "0662", v) + } +}