Skip to content

Commit a1d2f05

Browse files
committed
Merge branch 'json-prettyprint-example'
2 parents f5f097f + 1bb96c3 commit a1d2f05

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

example_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ func ExampleNewMarshaller_json() {
2525
// <nil>
2626
}
2727

28+
func ExampleNewMarshaller_json_prettyprint() {
29+
var buf bytes.Buffer
30+
encoder := refmt.NewMarshaller(json.EncodeOptions{
31+
Line: []byte{'\n'},
32+
Indent: []byte{'\t'},
33+
}, &buf)
34+
err := encoder.Marshal(map[string]interface{}{
35+
"x": "a",
36+
"y": 1,
37+
})
38+
fmt.Println(buf.String())
39+
fmt.Printf("%v\n", err)
40+
41+
// Output:
42+
// {
43+
// "x": "a",
44+
// "y": 1
45+
// }
46+
//
47+
// <nil>
48+
}
49+
2850
func ExampleNewMarshallerAtlased_autogeneratedAtlas() {
2951
type MyType struct {
3052
X string

marshalHelpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ type Marshaller interface {
3939
}
4040

4141
func NewMarshaller(opts EncodeOptions, wr io.Writer) Marshaller {
42-
switch opts.(type) {
42+
switch o2 := opts.(type) {
4343
case json.EncodeOptions:
44-
return json.NewMarshaller(wr)
44+
return json.NewMarshallerAtlased(wr, o2, atlas.MustBuild())
4545
case cbor.EncodeOptions:
4646
return cbor.NewMarshaller(wr)
4747
default:

0 commit comments

Comments
 (0)