File: json.go

package info (click to toggle)
wego 2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: makefile: 4
file content (36 lines) | stat: -rw-r--r-- 594 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package frontends

import (
	"encoding/json"
	"flag"
	"log"
	"os"

	"github.com/schachmat/wego/iface"
)

type jsnConfig struct {
	noIndent bool
}

func (c *jsnConfig) Setup() {
	flag.BoolVar(&c.noIndent, "jsn-no-indent", false, "json frontend: do not indent the output")
}

func (c *jsnConfig) Render(r iface.Data, unitSystem iface.UnitSystem) {
	var b []byte
	var err error
	if c.noIndent {
		b, err = json.Marshal(r)
	} else {
		b, err = json.MarshalIndent(r, "", "\t")
	}
	if err != nil {
		log.Fatal(err)
	}
	os.Stdout.Write(b)
}

func init() {
	iface.AllFrontends["json"] = &jsnConfig{}
}