...
1
2
3
4 package jwriter
5
6
7
8
9
10
11
12 import (
13 "encoding/json"
14 "io"
15
16 ejwriter "github.com/mailru/easyjson/jwriter"
17 )
18
19 var nullToken = []byte("null")
20
21 type tokenWriter struct {
22
23 pWriter *ejwriter.Writer
24
25
26 inlineWriter ejwriter.Writer
27 targetIOWriter io.Writer
28 targetBufferSize int
29 }
30
31 func newTokenWriter() tokenWriter {
32 return tokenWriter{}
33 }
34
35 func newTokenWriterFromEasyjsonWriter(writer *ejwriter.Writer) tokenWriter {
36 return tokenWriter{pWriter: writer}
37 }
38
39 func newStreamingTokenWriter(dest io.Writer, bufferSize int) tokenWriter {
40 tw := tokenWriter{targetIOWriter: dest, targetBufferSize: bufferSize}
41 tw.inlineWriter.Buffer.EnsureSpace(bufferSize)
42 return tw
43 }
44
45 func (tw *tokenWriter) Bytes() []byte {
46 pWriter := tw.pWriter
47 if pWriter == nil {
48 pWriter = &tw.inlineWriter
49 }
50 bytes, _ := pWriter.BuildBytes(nil)
51 return bytes
52 }
53
54 func (tw *tokenWriter) Grow(n int) {
55 pWriter := tw.pWriter
56 if pWriter == nil {
57 pWriter = &tw.inlineWriter
58 }
59 pWriter.Buffer.EnsureSpace(n)
60 }
61
62 func (tw *tokenWriter) Flush() error {
63 if tw.targetIOWriter == nil {
64 return nil
65 }
66 _, err := tw.inlineWriter.DumpTo(tw.targetIOWriter)
67 return err
68 }
69
70 func (tw *tokenWriter) Null() error {
71 pWriter := tw.pWriter
72 if pWriter == nil {
73 pWriter = &tw.inlineWriter
74 }
75 pWriter.Raw(nullToken, nil)
76 return tw.maybeFlush()
77 }
78
79 func (tw *tokenWriter) Bool(value bool) error {
80 pWriter := tw.pWriter
81 if pWriter == nil {
82 pWriter = &tw.inlineWriter
83 }
84 pWriter.Bool(value)
85 return tw.maybeFlush()
86 }
87
88 func (tw *tokenWriter) Int(value int) error {
89 pWriter := tw.pWriter
90 if pWriter == nil {
91 pWriter = &tw.inlineWriter
92 }
93 pWriter.Int(value)
94 return tw.maybeFlush()
95 }
96
97 func (tw *tokenWriter) Float64(value float64) error {
98 i := int(value)
99 if float64(i) == value {
100 return tw.Int(i)
101 }
102 pWriter := tw.pWriter
103 if pWriter == nil {
104 pWriter = &tw.inlineWriter
105 }
106 pWriter.Float64(value)
107 return tw.maybeFlush()
108 }
109
110 func (tw *tokenWriter) String(value string) error {
111 pWriter := tw.pWriter
112 if pWriter == nil {
113 pWriter = &tw.inlineWriter
114 }
115 pWriter.String(value)
116 return tw.maybeFlush()
117 }
118
119 func (tw *tokenWriter) Raw(value json.RawMessage) error {
120 pWriter := tw.pWriter
121 if pWriter == nil {
122 pWriter = &tw.inlineWriter
123 }
124 pWriter.Raw(value, nil)
125 return tw.maybeFlush()
126 }
127
128 func (tw *tokenWriter) PropertyName(name string) error {
129 pWriter := tw.pWriter
130 if pWriter == nil {
131 pWriter = &tw.inlineWriter
132 }
133 pWriter.String(name)
134 pWriter.RawByte(':')
135 return tw.maybeFlush()
136 }
137
138 func (tw *tokenWriter) Delimiter(delimiter byte) error {
139 pWriter := tw.pWriter
140 if pWriter == nil {
141 pWriter = &tw.inlineWriter
142 }
143 pWriter.RawByte(delimiter)
144 return tw.maybeFlush()
145 }
146
147 func (tw *tokenWriter) maybeFlush() error {
148 pWriter := tw.pWriter
149 if pWriter == nil {
150 pWriter = &tw.inlineWriter
151 }
152 if pWriter.Error != nil {
153 return pWriter.Error
154 }
155 if tw.targetIOWriter == nil || pWriter.Buffer.Size() < tw.targetBufferSize {
156 return nil
157 }
158 return tw.Flush()
159 }
160
View as plain text