@@ -148,30 +148,120 @@ func (p *OutputProcessor) ToCompactJSON(obj interface{}) (string, error) {
148148}
149149
150150func (p * OutputProcessor ) neatJSON (prefix string , obj interface {}) (string , error ) {
151+ var err error
152+
151153 switch t := obj .(type ) {
154+ case * yamlv3.Node :
155+ err = p .neatJSONofNode (prefix , t )
156+
152157 case yamlv2.MapSlice :
153- if err := p .neatJSONofYAMLMapSlice (prefix , t ); err != nil {
154- return "" , err
155- }
158+ err = p .neatJSONofYAMLMapSlice (prefix , t )
156159
157160 case []interface {}:
158- if err := p .neatJSONofSlice (prefix , t ); err != nil {
159- return "" , err
161+ err = p .neatJSONofSlice (prefix , t )
162+
163+ default :
164+ err = p .neatJSONofScalar (prefix , obj )
165+ }
166+
167+ if err != nil {
168+ return "" , err
169+ }
170+
171+ p .out .Flush ()
172+ return p .data .String (), nil
173+ }
174+
175+ func (p * OutputProcessor ) neatJSONofNode (prefix string , node * yamlv3.Node ) error {
176+ switch node .Kind {
177+ case yamlv3 .DocumentNode :
178+ return p .neatJSONofNode (prefix , node .Content [0 ])
179+
180+ case yamlv3 .MappingNode :
181+ if len (node .Content ) == 0 {
182+ fmt .Fprint (p .out , p .colorize ("{}" , "emptyStructures" ))
183+ return nil
160184 }
161185
162- case []yamlv2.MapSlice :
163- if err := p .neatJSONofSlice (prefix , p .simplify (t )); err != nil {
164- return "" , err
186+ bunt .Fprint (p .out , "*{*\n " )
187+ for i := 0 ; i < len (node .Content ); i += 2 {
188+ k , v := followAlias (node .Content [i ]), followAlias (node .Content [i + 1 ])
189+
190+ fmt .Fprint (p .out ,
191+ prefix ,
192+ p .prefixAdd (),
193+ p .colorize (`"` + k .Value + `"` , "keyColor" ), ": " ,
194+ )
195+
196+ if p .isScalar (v ) {
197+ p .neatJSON ("" , v )
198+
199+ } else {
200+ p .neatJSON (prefix + p .prefixAdd (), v )
201+ }
202+
203+ if i < len (node .Content )- 2 {
204+ fmt .Fprint (p .out , "," )
205+ }
206+
207+ fmt .Fprint (p .out , "\n " )
165208 }
209+ bunt .Fprint (p .out , prefix , "*}*" )
166210
167- default :
168- if err := p .neatJSONofScalar (prefix , obj ); err != nil {
169- return "" , nil
211+ case yamlv3 .SequenceNode :
212+ if len (node .Content ) == 0 {
213+ fmt .Fprint (p .out , p .colorize ("[]" , "emptyStructures" ))
214+ return nil
215+ }
216+
217+ bunt .Fprint (p .out , "*[*\n " )
218+ for i := range node .Content {
219+ entry := followAlias (node .Content [i ])
220+
221+ if p .isScalar (entry ) {
222+ p .neatJSON ("" , entry )
223+
224+ } else {
225+ fmt .Fprint (p .out , prefix , p .prefixAdd ())
226+ p .neatJSON (prefix + p .prefixAdd (), entry )
227+ }
228+
229+ if i < len (node .Content )- 1 {
230+ fmt .Fprint (p .out , "," )
231+ }
232+
233+ fmt .Fprint (p .out , "\n " )
234+ }
235+ bunt .Fprint (p .out , prefix , "*]*" )
236+
237+ case yamlv3 .ScalarNode :
238+ if node .Tag == "!!null" {
239+ fmt .Fprint (p .out , p .colorize ("null" , "nullColor" ))
240+ return nil
170241 }
242+
243+ color := p .determineColorByType (node )
244+ quotes := func () string {
245+ if node .Tag == "!!str" {
246+ return p .colorize (`"` , color )
247+ }
248+
249+ return ""
250+ }
251+
252+ fmt .Fprint (p .out , prefix , quotes ())
253+ parts := strings .Split (node .Value , "\n " )
254+ for idx , part := range parts {
255+ fmt .Fprint (p .out , p .colorize (part , color ))
256+
257+ if idx < len (parts )- 1 {
258+ fmt .Fprint (p .out , p .colorize ("\\ n" , "emptyStructures" ))
259+ }
260+ }
261+ fmt .Fprint (p .out , quotes ())
171262 }
172263
173- p .out .Flush ()
174- return p .data .String (), nil
264+ return nil
175265}
176266
177267func (p * OutputProcessor ) neatJSONofYAMLMapSlice (prefix string , mapslice yamlv2.MapSlice ) error {
0 commit comments