forked from johnkerl/miller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlrmap_json.go
177 lines (154 loc) · 5.04 KB
/
mlrmap_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
// ================================================================
// See mlrval_json.go for details. This is the unmarshal/marshal solely for Mlrmap.
// ================================================================
package mlrval
import (
"bytes"
"github.com/johnkerl/miller/internal/pkg/colorizer"
"github.com/johnkerl/miller/internal/pkg/lib"
)
// ----------------------------------------------------------------
func (mlrmap *Mlrmap) MarshalJSON(
jsonFormatting TJSONFormatting,
outputIsStdout bool,
) (string, error) {
var buffer bytes.Buffer
s, err := mlrmap.marshalJSONAux(jsonFormatting, 1, outputIsStdout)
if err != nil {
return "", err
}
buffer.WriteString(s)
// Do not write the final newline here, so the caller can write commas
// in the right place if desired.
// buffer.WriteString("\n")
return buffer.String(), nil
}
// For a map we only write from opening curly brace to closing curly brace. In
// nested-map contexts, mlrmap particular map might be written with a comma
// immediately after its closing curly brace, or a newline, and only the caller
// can know that.
//
// The element nesting depth is how deeply our element should be indented. Our
// closing curly brace is indented one less than that. For example, a
// root-level record '{"a":1,"b":2}' should be formatted as
//
// {
// "a": 1, <-- element nesting depth is 1 for root-level map
// "b": 2 <-- element nesting depth is 1 for root-level map
// } <-- closing curly brace nesting depth is 0 for root-level map
func (mlrmap *Mlrmap) marshalJSONAux(
jsonFormatting TJSONFormatting,
elementNestingDepth int,
outputIsStdout bool,
) (string, error) {
if jsonFormatting == JSON_MULTILINE {
return mlrmap.marshalJSONAuxMultiline(jsonFormatting, elementNestingDepth, outputIsStdout)
} else if jsonFormatting == JSON_SINGLE_LINE {
return mlrmap.marshalJSONAuxSingleLine(jsonFormatting, elementNestingDepth, outputIsStdout)
} else {
lib.InternalCodingErrorIf(true)
return "", nil // not reached
}
}
func (mlrmap *Mlrmap) marshalJSONAuxMultiline(
jsonFormatting TJSONFormatting,
elementNestingDepth int,
outputIsStdout bool,
) (string, error) {
var buffer bytes.Buffer
buffer.WriteString("{")
// Write empty map as '{}'. For anything else, opening curly brace in a
// line of its own, one key-value pair per line, closing curly brace on a
// line of its own.
if mlrmap.Head != nil {
buffer.WriteString("\n")
}
for pe := mlrmap.Head; pe != nil; pe = pe.Next {
// Write the key which is necessarily string-valued in Miller, and in
// JSON for that matter :)
for i := 0; i < elementNestingDepth; i++ {
buffer.WriteString(JSON_INDENT_STRING)
}
encoded := string(millerJSONEncodeString(pe.Key))
colorized := colorizer.MaybeColorizeKey(encoded, outputIsStdout)
buffer.WriteString(colorized)
buffer.WriteString(": ")
// Write the value which is a mlrval
valueString, err := pe.Value.marshalJSONAux(jsonFormatting, elementNestingDepth+1, outputIsStdout)
if err != nil {
return "", err
}
_, err = buffer.WriteString(valueString)
if err != nil {
return "", err
}
if pe.Next != nil {
buffer.WriteString(",")
}
buffer.WriteString("\n")
}
// Write empty map as '{}'.
if mlrmap.Head != nil {
for i := 0; i < elementNestingDepth-1; i++ {
buffer.WriteString(JSON_INDENT_STRING)
}
}
buffer.WriteString("}")
return buffer.String(), nil
}
func (mlrmap *Mlrmap) marshalJSONAuxSingleLine(
jsonFormatting TJSONFormatting,
elementNestingDepth int,
outputIsStdout bool,
) (string, error) {
var buffer bytes.Buffer
buffer.WriteString("{")
for pe := mlrmap.Head; pe != nil; pe = pe.Next {
// Write the key which is necessarily string-valued in Miller, and in
// JSON for that matter :)
buffer.WriteString("\"")
buffer.WriteString(colorizer.MaybeColorizeKey(pe.Key, outputIsStdout))
buffer.WriteString("\": ")
// Write the value which is a mlrval
valueString, err := pe.Value.marshalJSONAux(jsonFormatting, elementNestingDepth+1, outputIsStdout)
if err != nil {
return "", err
}
_, err = buffer.WriteString(valueString)
if err != nil {
return "", err
}
if pe.Next != nil {
buffer.WriteString(", ")
}
}
buffer.WriteString("}")
return buffer.String(), nil
}
// ----------------------------------------------------------------
// JSON-stringifies a single field of a record
func (entry *MlrmapEntry) JSONStringifyInPlace(
jsonFormatting TJSONFormatting,
) {
outputBytes, err := entry.Value.MarshalJSON(jsonFormatting, false)
if err != nil {
entry.Value = ERROR
} else {
entry.Value = FromString(string(outputBytes))
}
}
// ----------------------------------------------------------------
// JSON-parses a single field of a record
func (entry *MlrmapEntry) JSONParseInPlace() {
input := entry.Value.String()
err := entry.Value.UnmarshalJSON([]byte(input))
if err != nil {
entry.Value = ERROR
}
}
// StringifyValuesRecursively is nominally for the `--jvquoteall` flag.
func (mlrmap *Mlrmap) StringifyValuesRecursively() {
for pe := mlrmap.Head; pe != nil; pe = pe.Next {
pe.Value.StringifyValuesRecursively()
}
}