-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
48 lines (38 loc) · 892 Bytes
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package dec128
import (
"bytes"
"github.com/jokruger/dec128/state"
)
// MarshalJSON implements the json.Marshaler interface.
func (self Dec128) MarshalJSON() ([]byte, error) {
if self.state >= state.Error {
return NaNJsonStrBytes, nil
}
if self.IsZero() {
return ZeroJsonStrBytes, nil
}
buf := [MaxStrLen + 2]byte{}
buf[0] = '"'
sb, trim := self.appendString(buf[:1])
if trim {
sb = trimTrailingZeros(sb)
}
return append(sb, '"'), nil
}
var nullValue = []byte("null")
// UnmarshalJSON implements the json.Unmarshaler interface.
func (self *Dec128) UnmarshalJSON(data []byte) error {
if len(data) >= 2 && data[0] == '"' && data[len(data)-1] == '"' {
data = data[1 : len(data)-1]
}
if len(data) == 0 || bytes.Equal(data, nullValue) {
*self = Zero
return nil
}
t := FromString(data[:])
if t.IsNaN() {
return t.ErrorDetails()
}
*self = t
return nil
}