Write serializes data into JSON output written to w. If the object passed as data implements exportable, or if data is a map or slice of exportable object, ExportData() will be called on each object to obtain raw data for serialization.
(ios *iostreams.IOStreams, data interface{})
| 223 | // or if data is a map or slice of exportable object, ExportData() will be called on each object to obtain |
| 224 | // raw data for serialization. |
| 225 | func (e *jsonExporter) Write(ios *iostreams.IOStreams, data interface{}) error { |
| 226 | buf := bytes.Buffer{} |
| 227 | encoder := json.NewEncoder(&buf) |
| 228 | encoder.SetEscapeHTML(false) |
| 229 | if err := encoder.Encode(e.exportData(reflect.ValueOf(data))); err != nil { |
| 230 | return err |
| 231 | } |
| 232 | |
| 233 | w := ios.Out |
| 234 | if e.filter != "" { |
| 235 | indent := "" |
| 236 | if ios.IsStdoutTTY() { |
| 237 | indent = " " |
| 238 | } |
| 239 | if err := jq.EvaluateFormatted(&buf, w, e.filter, indent, ios.ColorEnabled()); err != nil { |
| 240 | return err |
| 241 | } |
| 242 | } else if e.template != "" { |
| 243 | t := template.New(w, ios.TerminalWidth(), ios.ColorEnabled()) |
| 244 | if err := t.Parse(e.template); err != nil { |
| 245 | return err |
| 246 | } |
| 247 | if err := t.Execute(&buf); err != nil { |
| 248 | return err |
| 249 | } |
| 250 | return t.Flush() |
| 251 | } else if ios.ColorEnabled() { |
| 252 | return jsoncolor.Write(w, &buf, " ") |
| 253 | } |
| 254 | |
| 255 | _, err := io.Copy(w, &buf) |
| 256 | return err |
| 257 | } |
| 258 | |
| 259 | func (e *jsonExporter) exportData(v reflect.Value) interface{} { |
| 260 | switch v.Kind() { |
nothing calls this directly
no test coverage detected