-
-
Save minux/f9b68a62be8c5983b70ffc756824142f to your computer and use it in GitHub Desktop.
removing fmt dependency from encoding/json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/encoding/json/decode.go b/src/encoding/json/decode.go | |
index 434edf8..072b570 100644 | |
--- a/src/encoding/json/decode.go | |
+++ b/src/encoding/json/decode.go | |
@@ -12,7 +12,6 @@ import ( | |
"encoding" | |
"encoding/base64" | |
"errors" | |
- "fmt" | |
"reflect" | |
"runtime" | |
"strconv" | |
@@ -683,7 +682,7 @@ func (d *decodeState) object(v reflect.Value) { | |
case string: | |
d.literalStore([]byte(qv), subv, true) | |
default: | |
- d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type())) | |
+ d.saveError(errors.New("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into " + subv.Type().String())) | |
} | |
} else { | |
d.value(subv) | |
@@ -757,7 +756,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool | |
// Check for unmarshaler. | |
if len(item) == 0 { | |
//Empty string given | |
- d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
+ d.saveError(errors.New("json: invalid use of ,string struct tag, trying to unmarshal " + strconv.Quote(string(item)) + " into " + v.Type().String())) | |
return | |
} | |
wantptr := item[0] == 'n' // null | |
@@ -772,7 +771,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool | |
if ut != nil { | |
if item[0] != '"' { | |
if fromQuoted { | |
- d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
+ d.saveError(errors.New("json: invalid use of ,string struct tag, trying to unmarshal " + strconv.Quote(string(item)) + " into " + v.Type().String())) | |
} else { | |
d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) | |
} | |
@@ -781,7 +780,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool | |
s, ok := unquoteBytes(item) | |
if !ok { | |
if fromQuoted { | |
- d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
+ d.error(errors.New("json: invalid use of ,string struct tag, trying to unmarshal " + strconv.Quote(string(item)) + " into " + v.Type().String())) | |
} else { | |
d.error(errPhase) | |
} | |
@@ -807,7 +806,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool | |
switch v.Kind() { | |
default: | |
if fromQuoted { | |
- d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
+ d.saveError(errors.New("json: invalid use of ,string struct tag, trying to unmarshal " + strconv.Quote(string(item)) + " into " + v.Type().String())) | |
} else { | |
d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)}) | |
} | |
@@ -825,7 +824,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool | |
s, ok := unquoteBytes(item) | |
if !ok { | |
if fromQuoted { | |
- d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
+ d.error(errors.New("json: invalid use of ,string struct tag, trying to unmarshal " + strconv.Quote(string(item)) + " into " + v.Type().String())) | |
} else { | |
d.error(errPhase) | |
} | |
@@ -858,7 +857,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool | |
default: // number | |
if c != '-' && (c < '0' || c > '9') { | |
if fromQuoted { | |
- d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
+ d.error(errors.New("json: invalid use of ,string struct tag, trying to unmarshal " + strconv.Quote(string(item)) + " into " + v.Type().String())) | |
} else { | |
d.error(errPhase) | |
} | |
@@ -869,12 +868,12 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool | |
if v.Kind() == reflect.String && v.Type() == numberType { | |
v.SetString(s) | |
if !isValidNumber(s) { | |
- d.error(fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)) | |
+ d.error(errors.New("json: invalid number literal, trying to unmarshal " + strconv.Quote(string(item)) + " into Number")) | |
} | |
break | |
} | |
if fromQuoted { | |
- d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
+ d.error(errors.New("json: invalid use of ,string struct tag, trying to unmarshal " + strconv.Quote(string(item)) + " into " + v.Type().String())) | |
} else { | |
d.error(&UnmarshalTypeError{"number", v.Type(), int64(d.off)}) | |
} | |
diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go | |
index d8c7798..24c1582 100644 | |
--- a/src/encoding/json/encode.go | |
+++ b/src/encoding/json/encode.go | |
@@ -14,7 +14,7 @@ import ( | |
"bytes" | |
"encoding" | |
"encoding/base64" | |
- "fmt" | |
+ "errors" | |
"math" | |
"reflect" | |
"runtime" | |
@@ -544,7 +544,7 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) { | |
numStr = "0" // Number's zero-val | |
} | |
if !isValidNumber(numStr) { | |
- e.error(fmt.Errorf("json: invalid number literal %q", numStr)) | |
+ e.error(errors.New("json: invalid number literal " + strconv.Quote(numStr))) | |
} | |
e.WriteString(numStr) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment