-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
34 lines (28 loc) · 785 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
package multiverse
import (
"math"
"strconv"
)
func (pt usuallyNumeric) MarshalJSON() ([]byte, error) {
str := pt.Original
if !math.IsNaN(float64(pt.Val)) {
str = strconv.FormatFloat(float64(pt.Val), 'f', -1, 32)
}
return []byte(strconv.QuoteToASCII(str)), nil
}
// JsonDecode allows a Format to be restored using the encoding/json package.
func (f *Format) UnmarshalJSON(name []byte) error {
name = name[1:]
name = name[:len(name)-1]
for _, format := range Formats.List {
if format.Name == string(name) {
*f = *format
return nil
}
}
return unrecognizedFormatErr(string(name))
}
// JsonEncode allows a Format to be saveed using the encoding/json package.
func (f *Format) MarshalJSON() ([]byte, error) {
return []byte(strconv.QuoteToASCII(f.Name)), nil
}