From 096410449cad14b32b41a6de1ef56f16ef3d25fb Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 1 Feb 2020 10:22:14 -0500 Subject: fiddle with unmarshaling of numbers --- rrdformat/marshal_xml.go | 2 +- rrdformat/rrdbinary/types.go | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) (limited to 'rrdformat') diff --git a/rrdformat/marshal_xml.go b/rrdformat/marshal_xml.go index fb6c9e1..2d0269b 100644 --- a/rrdformat/marshal_xml.go +++ b/rrdformat/marshal_xml.go @@ -1,8 +1,8 @@ package rrdformat import ( - "strconv" "encoding/xml" + "strconv" ) //const XMLNS = "https://oss.oetiker.ch/rrdtool/rrdtool-dump.xml" diff --git a/rrdformat/rrdbinary/types.go b/rrdformat/rrdbinary/types.go index 47df26e..e3011d5 100644 --- a/rrdformat/rrdbinary/types.go +++ b/rrdformat/rrdbinary/types.go @@ -2,8 +2,9 @@ package rrdbinary import ( "encoding/binary" - "encoding/json" + "fmt" "math" + "strconv" ) type Architecture struct { @@ -30,16 +31,30 @@ type Unival uint64 // 8 bytes type Time int64 // 4 or 8 bytes, only has second-precision type EOF struct{} // 0 bytes -func (u Unival) AsUint64() uint64 { return uint64(u) } -func (u Unival) AsFloat64() float64 { return math.Float64frombits(uint64(u)) } +func (u Unival) AsULong() ULong { return ULong(u) } +func (u Unival) AsDouble() Double { return Double(math.Float64frombits(uint64(u))) } + +func (u Unival) String() string { + return fmt.Sprintf("{ .ulong=%d; .double=%s }", u.AsULong(), u.AsDouble().String()) +} + +func (u Unival) MarshalText() ([]byte, error) { + return []byte(u.String()), nil +} -// MarshalJSON is for my own debugging. func (f Double) MarshalJSON() ([]byte, error) { - raw := float64(f) - if math.IsNaN(raw) { - return json.Marshal("NaN") + if math.IsNaN(float64(f)) { + return []byte(`"NaN"`), nil } - return json.Marshal(raw) + return f.MarshalText() +} + +func (f Double) MarshalText() ([]byte, error) { + return []byte(f.String()), nil +} + +func (f Double) String() string { + return strconv.FormatFloat(float64(f), 'e', 10, 64) } // Statically assert that each of the above types implements the -- cgit v1.2.3