-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjson.go
More file actions
31 lines (25 loc) · 709 Bytes
/
Copy pathjson.go
File metadata and controls
31 lines (25 loc) · 709 Bytes
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
// Copyright (c) bwplotka/mimic Authors
// Licensed under the Apache License 2.0.
package encoding
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
// jsonEncoder implements the Encoder interface.
type jsonEncoder struct {
io.Reader
}
// EncodeComment is a no-op for JSON encoder, as JSON doesn't support comments.
func (jsonEncoder) EncodeComment(lines string) []byte {
return []byte{}
}
// JSON returns reader that encodes anything to JSON.
func JSON(in interface{}) jsonEncoder {
b, err := json.MarshalIndent(in, "", " ")
if err != nil {
return jsonEncoder{Reader: errReader{err: fmt.Errorf("unable to marshal to JSON: %v: %w", in, err)}}
}
return jsonEncoder{Reader: bytes.NewBuffer(b)}
}