Commit 07314714 authored by Nodir Turakulov's avatar Nodir Turakulov Committed by Brad Fitzpatrick

encoding/json: simplify encodeState.{string, stringBytes}

As correctly mentioned in #11883, encodeState.string and
encodeState.stringBytes never return an error.
This CL removes the error from the function signatures and somewhat
simplifies call sites.

Fixes #11883

Change-Id: I1d1853d09631c545b68b5eea86ff7daa2e0ca10b
Reviewed-on: https://go-review.googlesource.com/15836
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 20736fca
...@@ -448,12 +448,10 @@ func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { ...@@ -448,12 +448,10 @@ func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
} }
m := v.Interface().(encoding.TextMarshaler) m := v.Interface().(encoding.TextMarshaler)
b, err := m.MarshalText() b, err := m.MarshalText()
if err == nil {
_, err = e.stringBytes(b)
}
if err != nil { if err != nil {
e.error(&MarshalerError{v.Type(), err}) e.error(&MarshalerError{v.Type(), err})
} }
e.stringBytes(b)
} }
func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
...@@ -464,12 +462,10 @@ func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { ...@@ -464,12 +462,10 @@ func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
} }
m := va.Interface().(encoding.TextMarshaler) m := va.Interface().(encoding.TextMarshaler)
b, err := m.MarshalText() b, err := m.MarshalText()
if err == nil {
_, err = e.stringBytes(b)
}
if err != nil { if err != nil {
e.error(&MarshalerError{v.Type(), err}) e.error(&MarshalerError{v.Type(), err})
} }
e.stringBytes(b)
} }
func boolEncoder(e *encodeState, v reflect.Value, quoted bool) { func boolEncoder(e *encodeState, v reflect.Value, quoted bool) {
...@@ -783,7 +779,7 @@ func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) } ...@@ -783,7 +779,7 @@ func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
func (sv stringValues) get(i int) string { return sv[i].String() } func (sv stringValues) get(i int) string { return sv[i].String() }
// NOTE: keep in sync with stringBytes below. // NOTE: keep in sync with stringBytes below.
func (e *encodeState) string(s string) (int, error) { func (e *encodeState) string(s string) int {
len0 := e.Len() len0 := e.Len()
e.WriteByte('"') e.WriteByte('"')
start := 0 start := 0
...@@ -855,11 +851,11 @@ func (e *encodeState) string(s string) (int, error) { ...@@ -855,11 +851,11 @@ func (e *encodeState) string(s string) (int, error) {
e.WriteString(s[start:]) e.WriteString(s[start:])
} }
e.WriteByte('"') e.WriteByte('"')
return e.Len() - len0, nil return e.Len() - len0
} }
// NOTE: keep in sync with string above. // NOTE: keep in sync with string above.
func (e *encodeState) stringBytes(s []byte) (int, error) { func (e *encodeState) stringBytes(s []byte) int {
len0 := e.Len() len0 := e.Len()
e.WriteByte('"') e.WriteByte('"')
start := 0 start := 0
...@@ -931,7 +927,7 @@ func (e *encodeState) stringBytes(s []byte) (int, error) { ...@@ -931,7 +927,7 @@ func (e *encodeState) stringBytes(s []byte) (int, error) {
e.Write(s[start:]) e.Write(s[start:])
} }
e.WriteByte('"') e.WriteByte('"')
return e.Len() - len0, nil return e.Len() - len0
} }
// A field represents a single field found in a struct. // A field represents a single field found in a struct.
......
...@@ -381,16 +381,10 @@ func TestStringBytes(t *testing.T) { ...@@ -381,16 +381,10 @@ func TestStringBytes(t *testing.T) {
r = append(r, i) r = append(r, i)
} }
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
_, err := es.string(s) es.string(s)
if err != nil {
t.Fatal(err)
}
esBytes := &encodeState{} esBytes := &encodeState{}
_, err = esBytes.stringBytes([]byte(s)) esBytes.stringBytes([]byte(s))
if err != nil {
t.Fatal(err)
}
enc := es.Buffer.String() enc := es.Buffer.String()
encBytes := esBytes.Buffer.String() encBytes := esBytes.Buffer.String()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment