-
Notifications
You must be signed in to change notification settings - Fork 4
/
primitives.go
166 lines (142 loc) · 3.41 KB
/
primitives.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package jsony
import (
"math"
"strconv"
)
type (
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
UInt uint
UInt8 uint8
UInt16 uint16
UInt32 uint32
UInt64 uint64
UIntPtr uintptr
Float32 float32
Float64 float64
comment string
String string
safeString string
null bool
)
const Null null = false
var (
_ Encoder = Bool(true)
_ Encoder = Int(0)
_ Encoder = Int8(0)
_ Encoder = Int16(0)
_ Encoder = Int32(0)
_ Encoder = Int64(0)
_ Encoder = UInt(0)
_ Encoder = UInt8(0)
_ Encoder = UInt16(0)
_ Encoder = UInt32(0)
_ Encoder = UInt64(0)
_ Encoder = UIntPtr(0)
_ Encoder = Float32(0)
_ Encoder = Float64(0)
_ Encoder = comment("")
_ Encoder = String("")
_ Encoder = SafeString("")
)
func SafeString(s safeString) safeString {
return s
}
func Comment(s comment) comment {
return s
}
// EncodeJSON implements [Encoder].
func (v null) EncodeJSON(w *Bytes) {
w.Extend([]byte("null"))
}
// EncodeJSON implements [Encoder].
func (v Bool) EncodeJSON(w *Bytes) {
if v {
w.Extend([]byte("true"))
} else {
w.Extend([]byte("false"))
}
}
// EncodeJSON implements [Encoder].
func (v Int) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendInt(w.buf, int64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v Int8) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendInt(w.buf, int64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v Int16) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendInt(w.buf, int64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v Int32) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendInt(w.buf, int64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v Int64) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendInt(w.buf, int64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v UInt) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendUint(w.buf, uint64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v UInt8) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendUint(w.buf, uint64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v UInt16) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendUint(w.buf, uint64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v UInt32) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendUint(w.buf, uint64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v UInt64) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendUint(w.buf, uint64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v UIntPtr) EncodeJSON(w *Bytes) {
w.buf = strconv.AppendUint(w.buf, uint64(v), 10)
}
// EncodeJSON implements [Encoder].
func (v Float32) EncodeJSON(w *Bytes) {
abs := float32(math.Abs(float64(v)))
fmt := byte('f')
if abs != 0 && (abs < 1e-6 || abs >= 1e21) {
fmt = 'e'
}
w.buf = strconv.AppendFloat(w.buf, float64(v), fmt, -1, 32)
}
// EncodeJSON implements [Encoder].
func (v Float64) EncodeJSON(w *Bytes) {
abs := math.Abs(float64(v))
fmt := byte('f')
if abs != 0 && (abs < 1e-6 || abs >= 1e21) {
fmt = 'e'
}
w.buf = strconv.AppendFloat(w.buf, float64(v), fmt, -1, 64)
}
// EncodeJSON implements [Encoder].
func (v comment) EncodeJSON(w *Bytes) {
w.Extend([]byte{'/', '/'})
// we assume that comment has no newlines.
w.Extend([]byte(v))
w.Append('\n')
}
// EncodeJSON implements [Encoder].
func (v String) EncodeJSON(w *Bytes) {
writeString(w, []byte(v))
}
// EncodeJSON implements [Encoder].
func (v safeString) EncodeJSON(w *Bytes) {
w.Append('"')
w.Extend([]byte(v))
w.Append('"')
}