author | Mikael Berthe <mikael@lilotux.net> |
Sun, 28 May 2017 10:29:07 +0200 | |
changeset 156 | bafe489779e5 |
parent 84 | 5a894a10304c |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package printer |
|
7 |
||
8 |
import ( |
|
9 |
"encoding/json" |
|
10 |
"io" |
|
11 |
"os" |
|
12 |
) |
|
13 |
||
14 |
// JSONPrinter represents a JSON printer |
|
15 |
type JSONPrinter struct { |
|
16 |
} |
|
17 |
||
18 |
// NewPrinterJSON returns a JSON ResourcePrinter |
|
83
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
19 |
func NewPrinterJSON(options Options) (*JSONPrinter, error) { |
0 | 20 |
return &JSONPrinter{}, nil |
21 |
} |
|
22 |
||
23 |
// PrintObj sends the object as text to the writer |
|
24 |
// If the writer w is nil, standard output will be used. |
|
25 |
// For JSONPrinter, the option parameter is currently not used. |
|
26 |
func (p *JSONPrinter) PrintObj(obj interface{}, w io.Writer, option string) error { |
|
27 |
if w == nil { |
|
28 |
w = os.Stdout |
|
29 |
} |
|
30 |
||
31 |
jsonEncoder := json.NewEncoder(w) |
|
32 |
//jsonEncoder.SetIndent("", " ") |
|
33 |
return jsonEncoder.Encode(obj) |
|
34 |
} |