@@ -18,8 +18,16 @@ import (
1818 "fmt"
1919 "strconv"
2020 "strings"
21+ "unsafe"
22+
23+ jsoniter "github.com/json-iterator/go"
2124)
2225
26+ func init () {
27+ jsoniter .RegisterTypeEncoderFunc ("model.HistogramBucket" , marshalHistogramBucketJSON , marshalJSONIsEmpty )
28+ jsoniter .RegisterTypeEncoderFunc ("model.SampleHistogramPair" , marshalSampleHistogramPairJSON , marshalJSONIsEmpty )
29+ }
30+
2331type FloatString float64
2432
2533func (v FloatString ) String () string {
@@ -49,24 +57,10 @@ type HistogramBucket struct {
4957 Count FloatString
5058}
5159
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 )
7064}
7165
7266func (s * HistogramBucket ) UnmarshalJSON (buf []byte ) error {
@@ -139,19 +133,21 @@ type SampleHistogramPair struct {
139133 Histogram * SampleHistogram
140134}
141135
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+
142146func (s SampleHistogramPair ) MarshalJSON () ([]byte , error ) {
143- t , err := json .Marshal (s .Timestamp )
144- if err != nil {
145- return nil , err
146- }
147147 if s .Histogram == nil {
148148 return nil , fmt .Errorf ("histogram is nil" )
149149 }
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 )
155151}
156152
157153func (s * SampleHistogramPair ) UnmarshalJSON (buf []byte ) error {
0 commit comments