printer/jsonprinter.go
changeset 84 5a894a10304c
parent 83 57afac822019
equal deleted inserted replaced
83:57afac822019 84:5a894a10304c
       
     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
       
    19 func NewPrinterJSON(options Options) (*JSONPrinter, error) {
       
    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 }