Debian Codegraph @

parsing/json/json_writer.go

raw symbols SCIP
1 package jsonscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.25 bytes/"
5 "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/"
6 jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/ "github.com/goccy/go-jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/"
7 "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/"
8 "github.com/tomwright/dasel/v3/parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/"
9 "ioscip-go gomod github.com/golang/go/src go1.25 io/"
10 "stringsscip-go gomod github.com/golang/go/src go1.25 strings/"
11 )
12
13 var _ parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer# = (*jsonWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#)(nil)
14
15 // NewJSONWriter creates a new JSON writer.
16 func newJSONWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/newJSONWriter().(options parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#) (parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#, error) {
17 return &jsonWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#{
18 optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#options.: options,
19 }, nil
20 }
21
22 type jsonWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter# struct {
23 optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#options. parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#
24 }
25
26 // Write writes a value to a byte slice.
27 func (j *jsonWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#) Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#Write().(value *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) ([]byte, error) {
28 buf := new(bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#)
29
30 es := encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#{indentStrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indentStr.: " "}
31 if j.optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#options..Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Compact. {
32 es.indentStrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indentStr. = ""
33 es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. = true
34 }
35
36 encoderFn := func(v any) error {
37 res, err := jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.Marshalscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Marshal().(v)
38 if err != nil {
39 return err
40 }
41 _, err = buf.Writescip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Write().(res)
42 return err
43 }
44
45 if err := j.writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#write().(buf, encoderFn, es, value); err != nil {
46 return nil, err
47 }
48
49 if _, err := buf.Writescip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Write().([]byte("\n")); err != nil {
50 return nil, err
51 }
52
53 return buf.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().(), nil
54 }
55
56 type encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState# struct {
57 indentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indent. int
58 indentStrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indentStr. string
59 compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. bool
60 }
61
62 func (es encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#) incscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#inc().() encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState# {
63 es.indentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indent.++
64 return es
65 }
66
67 func (es encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#) writeIndentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#writeIndent().(w ioscip-go gomod github.com/golang/go/src go1.25 io/.Writerscip-go gomod github.com/golang/go/src go1.25 io/Writer#) error {
68 if es.indentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indent. == 0 || es.indentStrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indentStr. == "" {
69 return nil
70 }
71 i := stringsscip-go gomod github.com/golang/go/src go1.25 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25 strings/Repeat().(es.indentStrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indentStr., es.indentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#indent.)
72 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(i)); err != nil {
73 return err
74 }
75 return nil
76 }
77
78 type encoderFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderFn# func(v any) error
79
80 func (j *jsonWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#) writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#write().(w ioscip-go gomod github.com/golang/go/src go1.25 io/.Writerscip-go gomod github.com/golang/go/src go1.25 io/Writer#, encoder encoderFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderFn#, es encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#, value *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error {
81 switch value.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().() {
82 case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeMap.:
83 return j.writeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#writeMap().(w, encoder, es, value)
84 case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeSlice.:
85 return j.writeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#writeSlice().(w, encoder, es, value)
86 case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeString.:
87 val, err := value.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
88 if err != nil {
89 return err
90 }
91 return encoder(val)
92 case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeIntscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeInt.:
93 val, err := value.IntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IntValue().()
94 if err != nil {
95 return err
96 }
97 return encoder(val)
98 case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeFloatscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeFloat.:
99 val, err := value.FloatValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#FloatValue().()
100 if err != nil {
101 return err
102 }
103 return encoder(val)
104 case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeBoolscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeBool.:
105 val, err := value.BoolValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#BoolValue().()
106 if err != nil {
107 return err
108 }
109 return encoder(val)
110 case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeNullscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeNull.:
111 return encoder(nil)
112 default:
113 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unsupported type: %s", value.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().())
114 }
115 }
116
117 func (j *jsonWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#) writeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#writeMap().(w ioscip-go gomod github.com/golang/go/src go1.25 io/.Writerscip-go gomod github.com/golang/go/src go1.25 io/Writer#, encoder encoderFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderFn#, es encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#, value *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error {
118 kvs, err := value.MapKeyValuesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyValues().()
119 if err != nil {
120 return err
121 }
122
123 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(`{`)); err != nil {
124 return err
125 }
126
127 if len(kvs) > 0 {
128 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
129 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte("\n")); err != nil {
130 return err
131 }
132 }
133
134 incEs := es.incscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#inc().()
135 for i, kv := range kvs {
136 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
137 if err := incEs.writeIndentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#writeIndent().(w); err != nil {
138 return err
139 }
140 }
141
142 if _, err := fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Fprintf().(w, `"%s":`, kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.); err != nil {
143 return err
144 }
145 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
146 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(" ")); err != nil {
147 return err
148 }
149 }
150
151 if err := j.writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#write().(w, encoder, incEs, kv.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Value.); err != nil {
152 return err
153 }
154
155 if i < len(kvs)-1 {
156 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(`,`)); err != nil {
157 return err
158 }
159 }
160
161 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
162 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte("\n")); err != nil {
163 return err
164 }
165 }
166 }
167 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
168 if err := es.writeIndentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#writeIndent().(w); err != nil {
169 return err
170 }
171 }
172 }
173
174 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(`}`)); err != nil {
175 return err
176 }
177
178 return nil
179 }
180
181 func (j *jsonWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#) writeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#writeSlice().(w ioscip-go gomod github.com/golang/go/src go1.25 io/.Writerscip-go gomod github.com/golang/go/src go1.25 io/Writer#, encoder encoderFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderFn#, es encoderStatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#, value *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error {
182 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(`[`)); err != nil {
183 return err
184 }
185
186 length, err := value.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
187 if err != nil {
188 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error getting slice length: %w", err)
189 }
190
191 if length > 0 {
192 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
193 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte("\n")); err != nil {
194 return err
195 }
196 }
197 incEs := es.incscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#inc().()
198 for i := 0; i < length; i++ {
199 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
200 if err := incEs.writeIndentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#writeIndent().(w); err != nil {
201 return err
202 }
203 }
204 va, err := value.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(i)
205 if err != nil {
206 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error getting slice index %d: %w", i, err)
207 }
208 if err := j.writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonWriter#write().(w, encoder, incEs, va); err != nil {
209 return err
210 }
211 if i < length-1 {
212 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(`,`)); err != nil {
213 return err
214 }
215 }
216 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
217 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte("\n")); err != nil {
218 return err
219 }
220 }
221 }
222 if !es.compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#compact. {
223 if err := es.writeIndentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/encoderState#writeIndent().(w); err != nil {
224 return err
225 }
226 }
227 }
228
229 if _, err := w.Writescip-go gomod github.com/golang/go/src go1.25 io/Writer#Write().([]byte(`]`)); err != nil {
230 return err
231 }
232
233 return nil
234 }