-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
101 lines (79 loc) · 2.15 KB
/
example_test.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package jsoncjson_test
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/hedhyw/jsoncjson"
)
// This example demonstrates decoding of JSON with comments input with
// a standart "encoding/json" decoder to a map[string]interface{}
// structure.
func Example_jsoncDecode() {
const jsoncInStr = `
{
// A "Hello, World!" program generally is a computer program
// that outputs or displays the message "Hello, World!".
"Hello": "World"
}
`
var r = jsoncjson.NewReader(strings.NewReader(jsoncInStr))
var data map[string]interface{}
_ = json.NewDecoder(r).Decode(&data)
fmt.Printf("%+v", data)
// Output: map[Hello:World]
}
// This example demonstrates reading JSON with comments from the bytes
// and printing then JSON without comments output.
func Example_jsoncFromBytes() {
var in = []byte(`
{/*
"If you cannot do great things, do small things in a great way."
- Napoleon Hill
*/ "Hello": "World" }
`)
var r = jsoncjson.NewReader(bytes.NewReader(in))
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
fmt.Printf("%s", buf.String())
// Output: { "Hello": "World" }
}
// This example demonstrates reading JSON with comments from the string
// and printing then JSON without comments output.
func Example_jsoncFromString() {
var in = strings.ReplaceAll(`
{
// Between saying and doing, many a pair of shoes is worn out.
"Hello": "World"
}
`, "\t", "")
var r = jsoncjson.NewReader(strings.NewReader(in))
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
fmt.Printf("%s", buf.String())
// Output:
// {
// "Hello": "World"
// }
}
// This example demonstrates reading JSON with comments from the file
// and decoding with a standart "encoding/json" decoder to a
// map[string]interface{} structure.
func Example_jsoncFromFile() {
const exampleFile = "./example.jsonc"
var f, err = os.Open(exampleFile)
if err != nil {
log.Fatalf("openning: %s: %s", exampleFile, err)
}
defer f.Close()
var r = jsoncjson.NewReader(f)
var data map[string]interface{}
err = json.NewDecoder(r).Decode(&data)
if err != nil {
log.Fatalf("decoding: %s", err)
}
fmt.Printf("%+v\n", data)
// Output: map[Hello:world]
}