forked from zhemao/glisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
180 lines (164 loc) · 3.45 KB
/
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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package glisp
import (
"bytes"
"encoding/json"
)
func Marshal(a Sexp) ([]byte, error) {
switch expr := a.(type) {
case SexpInt:
return expr.MarshalJSON()
case SexpBool:
return expr.MarshalJSON()
case SexpSentinel:
return expr.MarshalJSON()
case SexpFloat:
return expr.MarshalJSON()
case SexpArray:
return expr.MarshalJSON()
case SexpChar:
return expr.MarshalJSON()
case *SexpFunction:
return expr.MarshalJSON()
case *SexpHash:
return expr.MarshalJSON()
case *SexpPair:
return expr.MarshalJSON()
case SexpStr:
return expr.MarshalJSON()
case SexpSymbol:
return expr.MarshalJSON()
case SexpBytes:
return expr.MarshalJSON()
}
if m, ok := a.(json.Marshaler); ok {
return m.MarshalJSON()
}
return stdMarshal(a)
}
func (a SexpInt) MarshalJSON() ([]byte, error) {
return a.v.MarshalText()
}
func (a *SexpFunction) MarshalJSON() ([]byte, error) {
return stdMarshal(a.SexpString())
}
func (a SexpBytes) MarshalJSON() ([]byte, error) {
return stdMarshal(a.bytes)
}
func (a SexpBool) MarshalJSON() ([]byte, error) {
if bool(a) {
return []byte(`true`), nil
}
return []byte(`false`), nil
}
func (a SexpStr) MarshalJSON() ([]byte, error) {
return stdMarshal(string(a))
}
func (a SexpSymbol) MarshalJSON() ([]byte, error) {
return stdMarshal(a.Name())
}
func (a SexpFloat) MarshalJSON() ([]byte, error) {
if a.rawStr != "" {
return []byte(a.rawStr), nil
}
return a.v.MarshalText()
}
func (a SexpChar) MarshalJSON() ([]byte, error) {
return stdMarshal(rune(a))
}
func (a SexpSentinel) MarshalJSON() ([]byte, error) {
return []byte(`null`), nil
}
func (a *SexpPair) MarshalJSON() ([]byte, error) {
buffer := &bytes.Buffer{}
buffer.WriteByte('[')
pair := a
for {
switch pair.tail.(type) {
case *SexpPair:
data, err := Marshal(pair.head)
if err != nil {
return nil, err
}
buffer.Write(data)
buffer.WriteByte(',')
pair = pair.tail.(*SexpPair)
continue
}
break
}
data, err := Marshal(pair.head)
if err != nil {
return nil, err
}
buffer.Write(data)
if pair.tail != SexpNull {
data, err := Marshal(pair.tail)
if err != nil {
return nil, err
}
buffer.WriteByte(',')
buffer.Write(data)
}
buffer.WriteByte(']')
return buffer.Bytes(), nil
}
func (a *SexpHash) MarshalJSON() ([]byte, error) {
buffer := &bytes.Buffer{}
buffer.WriteByte('{')
keys := a.KeyOrder
var addComma bool
for _, key := range keys {
val, _ := a.HashGet(key)
keyj, err := Marshal(key)
if err != nil {
return nil, err
}
valj, err := Marshal(val)
if err != nil {
return nil, err
}
if addComma {
buffer.WriteByte(',')
} else {
addComma = true
}
buffer.Write(keyj)
buffer.WriteByte(':')
buffer.Write(valj)
}
buffer.WriteByte('}')
return buffer.Bytes(), nil
}
func (a SexpArray) MarshalJSON() ([]byte, error) {
buffer := &bytes.Buffer{}
buffer.WriteByte('[')
for i, expr := range a {
data, err := Marshal(expr)
if err != nil {
return nil, err
}
if i > 0 {
buffer.WriteByte(',')
}
buffer.Write(data)
}
buffer.WriteByte(']')
return buffer.Bytes(), nil
}
func stdMarshal(t interface{}) ([]byte, error) {
if t == nil {
return nil, nil
}
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(t); err != nil {
return nil, err
}
ret := buffer.Bytes()
// golang's encoder would always append a '\n', so we should drop it
if size := len(ret); size > 0 && ret[size-1] == '\n' {
ret = ret[:size-1]
}
return ret, nil
}