Skip to content

Commit e960845

Browse files
json: consistent use of encodeNull (should inline well)
1 parent fd40685 commit e960845

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

json/encode.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (e encoder) encodeToString(b []byte, p unsafe.Pointer, encode encodeFunc) (
241241
func (e encoder) encodeBytes(b []byte, p unsafe.Pointer) ([]byte, error) {
242242
v := *(*[]byte)(p)
243243
if v == nil {
244-
return append(b, "null"...), nil
244+
return e.encodeNull(b, nil)
245245
}
246246

247247
n := base64.StdEncoding.EncodedLen(len(v)) + 2
@@ -299,7 +299,7 @@ func (e encoder) encodeSlice(b []byte, p unsafe.Pointer, size uintptr, t reflect
299299
s := (*slice)(p)
300300

301301
if s.data == nil && s.len == 0 && s.cap == 0 {
302-
return append(b, "null"...), nil
302+
return e.encodeNull(b, nil)
303303
}
304304

305305
return e.encodeArray(b, s.data, s.len, size, t, encode)
@@ -308,7 +308,7 @@ func (e encoder) encodeSlice(b []byte, p unsafe.Pointer, size uintptr, t reflect
308308
func (e encoder) encodeMap(b []byte, p unsafe.Pointer, t reflect.Type, encodeKey, encodeValue encodeFunc, sortKeys sortFunc) ([]byte, error) {
309309
m := reflect.NewAt(t, p).Elem()
310310
if m.IsNil() {
311-
return append(b, "null"...), nil
311+
return e.encodeNull(b, nil)
312312
}
313313

314314
keys := m.MapKeys()
@@ -363,7 +363,7 @@ var mapslicePool = sync.Pool{
363363
func (e encoder) encodeMapStringInterface(b []byte, p unsafe.Pointer) ([]byte, error) {
364364
m := *(*map[string]any)(p)
365365
if m == nil {
366-
return append(b, "null"...), nil
366+
return e.encodeNull(b, nil)
367367
}
368368

369369
if (e.flags & SortMapKeys) == 0 {
@@ -441,7 +441,7 @@ func (e encoder) encodeMapStringInterface(b []byte, p unsafe.Pointer) ([]byte, e
441441
func (e encoder) encodeMapStringRawMessage(b []byte, p unsafe.Pointer) ([]byte, error) {
442442
m := *(*map[string]RawMessage)(p)
443443
if m == nil {
444-
return append(b, "null"...), nil
444+
return e.encodeNull(b, nil)
445445
}
446446

447447
if (e.flags & SortMapKeys) == 0 {
@@ -520,7 +520,7 @@ func (e encoder) encodeMapStringRawMessage(b []byte, p unsafe.Pointer) ([]byte,
520520
func (e encoder) encodeMapStringString(b []byte, p unsafe.Pointer) ([]byte, error) {
521521
m := *(*map[string]string)(p)
522522
if m == nil {
523-
return append(b, "null"...), nil
523+
return e.encodeNull(b, nil)
524524
}
525525

526526
if (e.flags & SortMapKeys) == 0 {
@@ -586,7 +586,7 @@ func (e encoder) encodeMapStringString(b []byte, p unsafe.Pointer) ([]byte, erro
586586
func (e encoder) encodeMapStringStringSlice(b []byte, p unsafe.Pointer) ([]byte, error) {
587587
m := *(*map[string][]string)(p)
588588
if m == nil {
589-
return append(b, "null"...), nil
589+
return e.encodeNull(b, nil)
590590
}
591591

592592
stringSize := unsafe.Sizeof("")
@@ -667,7 +667,7 @@ func (e encoder) encodeMapStringStringSlice(b []byte, p unsafe.Pointer) ([]byte,
667667
func (e encoder) encodeMapStringBool(b []byte, p unsafe.Pointer) ([]byte, error) {
668668
m := *(*map[string]bool)(p)
669669
if m == nil {
670-
return append(b, "null"...), nil
670+
return e.encodeNull(b, nil)
671671
}
672672

673673
if (e.flags & SortMapKeys) == 0 {
@@ -828,7 +828,7 @@ func (e encoder) encodeRawMessage(b []byte, p unsafe.Pointer) ([]byte, error) {
828828
v := *(*RawMessage)(p)
829829

830830
if v == nil {
831-
return append(b, "null"...), nil
831+
return e.encodeNull(b, nil)
832832
}
833833

834834
var s []byte
@@ -862,7 +862,7 @@ func (e encoder) encodeJSONMarshaler(b []byte, p unsafe.Pointer, t reflect.Type,
862862
switch v.Kind() {
863863
case reflect.Ptr, reflect.Interface:
864864
if v.IsNil() {
865-
return append(b, "null"...), nil
865+
return e.encodeNull(b, nil)
866866
}
867867
}
868868

0 commit comments

Comments
 (0)