-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (46 loc) · 1.19 KB
/
main.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package canonjson
import (
"bytes"
"canon-json/utils"
"encoding/json"
"io"
)
// Marshal - Returns the canon json encoding of an input
func Marshal(input interface{}) ([]byte, error) {
var buf bytes.Buffer
err := CreateEncoder(&buf).Encode(input)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Encoder - Writes canon JSON values to an output stream.
type Encoder struct {
canon utils.Inputs
out io.Writer
spacing bool
}
// CreateEncoder - Creates an encoder for a writer
func CreateEncoder(w io.Writer) *Encoder {
return &Encoder{
out: w,
spacing: true,
}
}
// Encode - Write marshaled Json of input to the stream.
// - Separates multiple inputs with spaces (Enabled by default)
// - Uses encoding/json.Marshal to convert Go Values to Json.
func (enc *Encoder) Encode(input interface{}) error {
marshaled, err := json.Marshal(input)
if err != nil {
return err
}
enc.canon.Decoder = json.NewDecoder(bytes.NewBuffer(marshaled))
_, err = enc.canon.Process(enc.out)
enc.canon.Spaced = enc.canon.Spaced && enc.spacing
return err
}
// SetSpacing - Enable or Disable spacing between multiple inputs
func (enc *Encoder) SetSpacing(on bool) {
enc.spacing = on
}