@@ -18,8 +18,16 @@ import (
18
18
"fmt"
19
19
"strconv"
20
20
"strings"
21
+ "unsafe"
22
+
23
+ jsoniter "github.com/json-iterator/go"
21
24
)
22
25
26
+ func init () {
27
+ jsoniter .RegisterTypeEncoderFunc ("model.HistogramBucket" , marshalHistogramBucketJSON , marshalJSONIsEmpty )
28
+ jsoniter .RegisterTypeEncoderFunc ("model.SampleHistogramPair" , marshalSampleHistogramPairJSON , marshalJSONIsEmpty )
29
+ }
30
+
23
31
type FloatString float64
24
32
25
33
func (v FloatString ) String () string {
@@ -49,24 +57,10 @@ type HistogramBucket struct {
49
57
Count FloatString
50
58
}
51
59
52
- func (s HistogramBucket ) MarshalJSON () ([]byte , error ) {
53
- b , err := json .Marshal (s .Boundaries )
54
- if err != nil {
55
- return nil , err
56
- }
57
- l , err := json .Marshal (s .Lower )
58
- if err != nil {
59
- return nil , err
60
- }
61
- u , err := json .Marshal (s .Upper )
62
- if err != nil {
63
- return nil , err
64
- }
65
- c , err := json .Marshal (s .Count )
66
- if err != nil {
67
- return nil , err
68
- }
69
- return []byte (fmt .Sprintf ("[%s,%s,%s,%s]" , b , l , u , c )), nil
60
+ // marshalHistogramBucketJSON writes fmt.Sprintf("[%s,%s,%s,%s]", b.Boundaries, b.Lower, b.Upper, b.Count).
61
+ func marshalHistogramBucketJSON (ptr unsafe.Pointer , stream * jsoniter.Stream ) {
62
+ b := * ((* HistogramBucket )(ptr ))
63
+ MarshalHistogramBucket (b , stream )
70
64
}
71
65
72
66
func (s * HistogramBucket ) UnmarshalJSON (buf []byte ) error {
@@ -139,19 +133,21 @@ type SampleHistogramPair struct {
139
133
Histogram * SampleHistogram
140
134
}
141
135
136
+ // marshalSampleHistogramPairJSON writes `[ts, "val"]`.
137
+ func marshalSampleHistogramPairJSON (ptr unsafe.Pointer , stream * jsoniter.Stream ) {
138
+ p := * ((* SampleHistogramPair )(ptr ))
139
+ stream .WriteArrayStart ()
140
+ MarshalTimestamp (int64 (p .Timestamp ), stream )
141
+ stream .WriteMore ()
142
+ MarshalHistogram (* p .Histogram , stream )
143
+ stream .WriteArrayEnd ()
144
+ }
145
+
142
146
func (s SampleHistogramPair ) MarshalJSON () ([]byte , error ) {
143
- t , err := json .Marshal (s .Timestamp )
144
- if err != nil {
145
- return nil , err
146
- }
147
147
if s .Histogram == nil {
148
148
return nil , fmt .Errorf ("histogram is nil" )
149
149
}
150
- v , err := json .Marshal (s .Histogram )
151
- if err != nil {
152
- return nil , err
153
- }
154
- return []byte (fmt .Sprintf ("[%s,%s]" , t , v )), nil
150
+ return jsoniter .ConfigCompatibleWithStandardLibrary .Marshal (s )
155
151
}
156
152
157
153
func (s * SampleHistogramPair ) UnmarshalJSON (buf []byte ) error {
0 commit comments