Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
encoding/json: simplify encodeState.{string, stringBytes}
Browse files Browse the repository at this point in the history
As correctly mentioned in golang#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 golang#11883

Change-Id: I1d1853d09631c545b68b5eea86ff7daa2e0ca10b
Reviewed-on: https://go-review.googlesource.com/15836
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
nodirt authored and bradfitz committed Oct 14, 2015
1 parent 20736fc commit 0731471
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
16 changes: 6 additions & 10 deletions src/encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,10 @@ func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
}
m := v.Interface().(encoding.TextMarshaler)
b, err := m.MarshalText()
if err == nil {
_, err = e.stringBytes(b)
}
if err != nil {
e.error(&MarshalerError{v.Type(), err})
}
e.stringBytes(b)
}

func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
Expand All @@ -464,12 +462,10 @@ func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
}
m := va.Interface().(encoding.TextMarshaler)
b, err := m.MarshalText()
if err == nil {
_, err = e.stringBytes(b)
}
if err != nil {
e.error(&MarshalerError{v.Type(), err})
}
e.stringBytes(b)
}

func boolEncoder(e *encodeState, v reflect.Value, quoted bool) {
Expand Down Expand Up @@ -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() }

// 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()
e.WriteByte('"')
start := 0
Expand Down Expand Up @@ -855,11 +851,11 @@ func (e *encodeState) string(s string) (int, error) {
e.WriteString(s[start:])
}
e.WriteByte('"')
return e.Len() - len0, nil
return e.Len() - len0
}

// 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()
e.WriteByte('"')
start := 0
Expand Down Expand Up @@ -931,7 +927,7 @@ func (e *encodeState) stringBytes(s []byte) (int, error) {
e.Write(s[start:])
}
e.WriteByte('"')
return e.Len() - len0, nil
return e.Len() - len0
}

// A field represents a single field found in a struct.
Expand Down
10 changes: 2 additions & 8 deletions src/encoding/json/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,10 @@ func TestStringBytes(t *testing.T) {
r = append(r, i)
}
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
_, err := es.string(s)
if err != nil {
t.Fatal(err)
}
es.string(s)

esBytes := &encodeState{}
_, err = esBytes.stringBytes([]byte(s))
if err != nil {
t.Fatal(err)
}
esBytes.stringBytes([]byte(s))

enc := es.Buffer.String()
encBytes := esBytes.Buffer.String()
Expand Down

0 comments on commit 0731471

Please sign in to comment.