-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_examples_test.go
More file actions
138 lines (119 loc) · 2.74 KB
/
Copy pathtype_examples_test.go
File metadata and controls
138 lines (119 loc) · 2.74 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package jsonkit_test
import (
"bytes"
"errors"
"fmt"
"strings"
jsonkit "github.com/forgemechanic/jsonkit"
"github.com/forgemechanic/jsonkit/exp/source"
)
func ExampleDecoder() {
dec := jsonkit.NewDecoder(strings.NewReader(`{"id":3}`))
var dst map[string]int
err := dec.Decode(&dst)
fmt.Println(err == nil)
fmt.Println(dst["id"])
// Output:
// true
// 3
}
func ExampleEncoder() {
var buf bytes.Buffer
enc := jsonkit.NewEncoder(&buf)
err := enc.Encode(map[string]int{"id": 3})
fmt.Println(err == nil)
fmt.Printf("%q\n", buf.String())
// Output:
// true
// "{\"id\":3}\n"
}
func ExampleDecodeResult() {
var dst struct {
Name string `json:"name"`
}
res, err := jsonkit.UnmarshalWithOptions(
[]byte(`{name:'jsonkit'}`),
&dst,
jsonkit.WithDecodeProfile(jsonkit.ProfileJSON5),
jsonkit.WithDecodeDiagnostics(true),
)
fmt.Println(err == nil)
fmt.Println(res.Profile() == jsonkit.ProfileJSON5)
fmt.Println(res.Mode() == jsonkit.RetentionFullCST)
// Output:
// true
// true
// true
}
func ExampleDecodeError() {
var dst map[string]any
_, err := jsonkit.UnmarshalWithOptions(
[]byte(`{"name": }`),
&dst,
jsonkit.WithDecodeDiagnostics(true),
)
var derr *jsonkit.DecodeError
fmt.Println(errors.As(err, &derr))
fmt.Println(len(derr.Diagnostics()) > 0)
// Output:
// true
// true
}
func ExampleEmbeddedBlock() {
host := []byte("start {\"id\":1} end")
start := int64(strings.Index(string(host), "{"))
end := int64(strings.LastIndex(string(host), "}") + 1)
src := source.NewBytes("host.txt", host)
res := jsonkit.Parse(src, jsonkit.WithEmbeddedBlock(jsonkit.EmbeddedBlock{
Start: start,
End: end,
UseHostOffsets: true,
}))
meta, _ := res.Embedded()
fmt.Println(res.OK())
fmt.Println(meta.EffectiveStart == start && meta.EffectiveEnd == end)
// Output:
// true
// true
}
func ExampleDiagnosticContext() {
ctx := jsonkit.DiagnosticContext{
Index: 2,
Depth: 1,
RecoveryEvents: 4,
}
fmt.Println(ctx.Index, ctx.Depth, ctx.RecoveryEvents)
// Output:
// 2 1 4
}
func ExampleDiagnosticDecision() {
decision := jsonkit.DiagnosticDecision{
Suppress: true,
EndEmbeddedBlock: false,
}
fmt.Println(decision.Suppress, decision.EndEmbeddedBlock)
// Output:
// true false
}
func ExampleEmbeddedMetadata() {
meta := jsonkit.EmbeddedMetadata{
EffectiveStart: 10,
EffectiveEnd: 20,
}
local, okLocal := meta.HostToLocal(15)
host, okHost := meta.LocalToHost(5)
fmt.Println(okLocal, local)
fmt.Println(okHost, host)
// Output:
// true 5
// true 15
}
func ExampleResult() {
src := source.NewBytes("doc.json", []byte(`{"ok":true}`))
res := jsonkit.Parse(src)
fmt.Println(res.OK())
fmt.Println(res.Profile() == jsonkit.ProfileStrict)
// Output:
// true
// true
}