-
Notifications
You must be signed in to change notification settings - Fork 288
/
json.go
64 lines (52 loc) · 1.52 KB
/
json.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
package store
import (
"fmt"
"io"
"unsafe"
json "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
"github.com/modern-go/reflect2"
"github.com/windmilleng/tilt/pkg/model"
)
var defaultJSONIterator = json.Config{}.Froze()
func CreateEngineStateEncoder(w io.Writer) *json.Encoder {
config := json.Config{SortMapKeys: true}.Froze()
config.RegisterExtension(&extra.BinaryAsStringExtension{})
config.RegisterExtension(newEngineStateExtension())
config.RegisterExtension(&privateFieldsExtension{})
return config.NewEncoder(w)
}
type targetIDEncoder struct {
delegate json.ValEncoder
}
func (targetIDEncoder) IsEmpty(ptr unsafe.Pointer) bool {
tID := (*model.TargetID)(ptr)
return tID.Empty()
}
func (e targetIDEncoder) Encode(ptr unsafe.Pointer, stream *json.Stream) {
tID := (*model.TargetID)(ptr)
s := tID.String()
stream.WriteString(fmt.Sprintf("%q", s))
}
type engineStateExtension struct {
*json.DummyExtension
targetIDType reflect2.Type
}
func newEngineStateExtension() engineStateExtension {
return engineStateExtension{
// memoize the type lookup
targetIDType: reflect2.TypeOf(model.TargetID{}),
}
}
func (e engineStateExtension) CreateMapKeyEncoder(typ reflect2.Type) json.ValEncoder {
if e.targetIDType == typ {
return targetIDEncoder{delegate: defaultJSONIterator.EncoderOf(typ)}
}
return nil
}
func (e engineStateExtension) CreateEncoder(typ reflect2.Type) json.ValEncoder {
if e.targetIDType == typ {
return targetIDEncoder{delegate: defaultJSONIterator.EncoderOf(typ)}
}
return nil
}