-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshaler.go
176 lines (147 loc) · 3.63 KB
/
marshaler.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
package htmljson
import (
"bytes"
"errors"
"io"
"sort"
"strconv"
"strings"
)
// Marshaler converts JSON stored as Go `any` object represented into HTML.
// Visually HTML is similar to pretty printed JSON with indentation.
// Rendering is customized by providing renderers for specific JSON elements.
// This facilitates CSS styling, CSS animations, and JavaScript events.
// JSON element renderers receive JSON path and value of element.
// Should be used only for types: bool, float64, string, []any, map[string]any, nil.
// You can get allowed input easily with json.Unmarshal to any.
// Safe for repeated use.
// Not safe for concurrent use.
type Marshaler struct {
Null func(key string) string
Bool func(key string, v bool) string
String func(key string, v string) string
Number func(key string, v float64, s string) string
Array ArrayMarshaler
Map MapMarshaler
Row func(s string, padding int) string
*rowWriter
depth int
key string
err []error
}
type ArrayMarshaler struct {
OpenBracket string
CloseBracket string
Comma string
}
type MapMarshaler struct {
OpenBracket string
CloseBracket string
Comma string
Colon string
Key func(key string, v string) string
}
// Marshaler converts JSON stored as Go `any` object represented into HTML.
func (s *Marshaler) Marshal(v any) []byte {
b := bytes.Buffer{}
s.MarshalTo(&b, v)
return b.Bytes()
}
// MarshalTo converts JSON stored as Go `any` object represented into HTML.
func (s *Marshaler) MarshalTo(w io.Writer, v any) error {
s.depth = 0
s.key = "$"
s.rowWriter = &rowWriter{
b: strings.Builder{},
w: w,
Row: s.Row,
}
s.marshal(v)
s.flush(s.depth)
s.err = append(s.err, s.rowWriter.err...)
return errors.Join(s.err...)
}
func (s *Marshaler) marshal(v any) {
if v == nil {
s.encodeNull()
}
switch q := v.(type) {
case bool:
s.encodeBool(q)
case string:
s.encodeString(q)
case float64:
s.encodeFloat64(q)
case map[string]any:
s.encodeMap(q)
case []any:
s.encodeArray(q)
default:
s.err = append(s.err, errors.New("skip unsupported type at key("+s.key+")"))
}
}
func (s *Marshaler) encodeNull() { s.write(s.Null(s.key)) }
func (s *Marshaler) encodeBool(v bool) { s.write(s.Bool(s.key, v)) }
func (s *Marshaler) encodeString(v string) { s.write(s.String(s.key, v)) }
func (s *Marshaler) encodeFloat64(v float64) {
s.write(s.Number(s.key, v, strconv.FormatFloat(v, 'f', -1, 64)))
}
func (s *Marshaler) encodeArray(v []any) {
s.write(s.Array.OpenBracket)
if len(v) == 0 {
s.write(s.Array.CloseBracket)
return
}
// write array
k, d := s.key, s.depth
defer func() { s.key, s.depth = k, d }()
s.flush(d)
s.depth = d + 1
for i, q := range v {
if i > 0 {
s.write(s.Array.Comma)
s.flush(s.depth)
}
s.key = k + "[" + strconv.Itoa(i) + "]"
s.write("") // fake virtual key, to apply same offset logic as JSON Map
s.marshal(q)
}
s.flush(s.depth)
s.write(s.Array.CloseBracket)
}
func (s *Marshaler) encodeMap(v map[string]any) {
s.write(s.Map.OpenBracket)
if len(v) == 0 {
s.write(s.Map.CloseBracket)
return
}
// extract and sort the keys
type kv struct {
k string
v any
}
sv := make([]kv, 0, len(v))
for k, v := range v {
sv = append(sv, kv{k: k, v: v})
}
sort.Slice(sv, func(i, j int) bool { return sv[i].k < sv[j].k })
// write map
k, d := s.key, s.depth
defer func() { s.key, s.depth = k, d }()
s.flush(d)
s.depth = d + 1
for i, kv := range sv {
if i > 0 {
s.write(s.Map.Comma)
s.flush(s.depth)
}
s.key = k + "." + kv.k
// key
s.write(s.Map.Key(s.key, kv.k))
s.write(s.Map.Colon)
// value
s.marshal(kv.v)
}
s.flush(s.depth)
s.write(s.Map.CloseBracket)
}