@@ -79,14 +79,12 @@ func (enc *JSONEncoder) Reset() {
7979
8080// AppendEncoderBegin writes the start of an encoder section, represented as a JSON object.
8181func (enc * JSONEncoder ) AppendEncoderBegin () {
82- enc .last = jsonTokenObjectBegin
83- enc .buf .WriteByte ('{' )
82+ enc .AppendObjectBegin ()
8483}
8584
8685// AppendEncoderEnd writes the end of an encoder section (closes a JSON object).
8786func (enc * JSONEncoder ) AppendEncoderEnd () {
88- enc .last = jsonTokenObjectEnd
89- enc .buf .WriteByte ('}' )
87+ enc .AppendObjectEnd ()
9088}
9189
9290// AppendObjectBegin starts a new JSON object.
@@ -114,24 +112,15 @@ func (enc *JSONEncoder) AppendArrayEnd() {
114112}
115113
116114// appendSeparator inserts a comma if necessary before a key or value.
117- func (enc * JSONEncoder ) appendSeparator (curr jsonToken ) {
118- switch curr {
119- case jsonTokenKey :
120- // Insert a comma between key-value pairs or elements
121- if enc .last == jsonTokenObjectEnd || enc .last == jsonTokenArrayEnd || enc .last == jsonTokenValue {
122- enc .buf .WriteByte (',' )
123- }
124- case jsonTokenValue :
125- if enc .last == jsonTokenValue {
126- enc .buf .WriteByte (',' )
127- }
128- default : // for linter
115+ func (enc * JSONEncoder ) appendSeparator () {
116+ if enc .last == jsonTokenObjectEnd || enc .last == jsonTokenArrayEnd || enc .last == jsonTokenValue {
117+ enc .buf .WriteByte (',' )
129118 }
130119}
131120
132121// AppendKey writes a JSON key (as a string followed by a colon).
133122func (enc * JSONEncoder ) AppendKey (key string ) {
134- enc .appendSeparator (jsonTokenKey )
123+ enc .appendSeparator ()
135124 enc .last = jsonTokenKey
136125 enc .buf .WriteByte ('"' )
137126 enc .safeAddString (key )
@@ -141,35 +130,35 @@ func (enc *JSONEncoder) AppendKey(key string) {
141130
142131// AppendBool writes a boolean value.
143132func (enc * JSONEncoder ) AppendBool (v bool ) {
144- enc .appendSeparator (jsonTokenValue )
133+ enc .appendSeparator ()
145134 enc .last = jsonTokenValue
146135 enc .buf .WriteString (strconv .FormatBool (v ))
147136}
148137
149138// AppendInt64 writes a signed 64-bit integer.
150139func (enc * JSONEncoder ) AppendInt64 (v int64 ) {
151- enc .appendSeparator (jsonTokenValue )
140+ enc .appendSeparator ()
152141 enc .last = jsonTokenValue
153142 enc .buf .WriteString (strconv .FormatInt (v , 10 ))
154143}
155144
156145// AppendUint64 writes an unsigned 64-bit integer.
157146func (enc * JSONEncoder ) AppendUint64 (u uint64 ) {
158- enc .appendSeparator (jsonTokenValue )
147+ enc .appendSeparator ()
159148 enc .last = jsonTokenValue
160149 enc .buf .WriteString (strconv .FormatUint (u , 10 ))
161150}
162151
163152// AppendFloat64 writes a floating-point number.
164153func (enc * JSONEncoder ) AppendFloat64 (v float64 ) {
165- enc .appendSeparator (jsonTokenValue )
154+ enc .appendSeparator ()
166155 enc .last = jsonTokenValue
167156 enc .buf .WriteString (strconv .FormatFloat (v , 'f' , - 1 , 64 ))
168157}
169158
170159// AppendString writes a string value (properly escaped).
171160func (enc * JSONEncoder ) AppendString (v string ) {
172- enc .appendSeparator (jsonTokenValue )
161+ enc .appendSeparator ()
173162 enc .last = jsonTokenValue
174163 enc .buf .WriteByte ('"' )
175164 enc .safeAddString (v )
@@ -178,7 +167,7 @@ func (enc *JSONEncoder) AppendString(v string) {
178167
179168// AppendReflect marshals any Go value into JSON and appends it.
180169func (enc * JSONEncoder ) AppendReflect (v interface {}) {
181- enc .appendSeparator (jsonTokenValue )
170+ enc .appendSeparator ()
182171 enc .last = jsonTokenValue
183172 b , err := json .Marshal (v )
184173 if err != nil {
@@ -257,7 +246,7 @@ type TextEncoder struct {
257246 separator string // Separator used between top-level key-value pairs
258247 jsonEncoder * JSONEncoder // Embedded JSON encoder for nested objects/arrays
259248 jsonDepth int8 // Tracks depth of nested JSON structures
260- init bool // Tracks if the first key-value has been written
249+ firstField bool // Tracks if the first key-value has been written
261250}
262251
263252// NewTextEncoder creates a new TextEncoder writing to the given buffer, using the specified separator.
@@ -313,10 +302,11 @@ func (enc *TextEncoder) AppendKey(key string) {
313302 enc .jsonEncoder .AppendKey (key )
314303 return
315304 }
316- if enc .init {
305+ if enc .firstField {
317306 enc .buf .WriteString (enc .separator )
307+ } else {
308+ enc .firstField = true
318309 }
319- enc .init = true
320310 enc .buf .WriteString (key )
321311 enc .buf .WriteByte ('=' )
322312}
0 commit comments