mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json/v2: cleanup error constructors
There is no need to explicitly pass in the options since this contained within the Encoder or Decoder struct ever since https://github.com/go-json-experiment/json/pull/163. Thus, remove it as an argument and fetch it from the coder. This only modifies code that is compiled in under goexperiment.jsonv2. Change-Id: I6c928b864bf7869889d7ee7d5c1d396fbe71296b Reviewed-on: https://go-review.googlesource.com/c/go/+/695278 Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
parent
af8870708b
commit
4b1800e476
4 changed files with 45 additions and 42 deletions
|
|
@ -128,7 +128,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
||||||
xe := export.Encoder(enc)
|
xe := export.Encoder(enc)
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimize for marshaling without preceding whitespace.
|
// Optimize for marshaling without preceding whitespace.
|
||||||
|
|
@ -153,7 +153,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
||||||
xd := export.Decoder(dec)
|
xd := export.Decoder(dec)
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
tok, err := dec.ReadToken()
|
tok, err := dec.ReadToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -190,7 +190,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
|
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
|
||||||
}
|
}
|
||||||
return &fncs
|
return &fncs
|
||||||
}
|
}
|
||||||
|
|
@ -200,7 +200,7 @@ func makeStringArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
||||||
xe := export.Encoder(enc)
|
xe := export.Encoder(enc)
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimize for marshaling without preceding whitespace.
|
// Optimize for marshaling without preceding whitespace.
|
||||||
|
|
@ -237,7 +237,7 @@ func makeStringArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
||||||
xd := export.Decoder(dec)
|
xd := export.Decoder(dec)
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
var flags jsonwire.ValueFlags
|
var flags jsonwire.ValueFlags
|
||||||
val, err := xd.ReadValue(&flags)
|
val, err := xd.ReadValue(&flags)
|
||||||
|
|
@ -327,7 +327,7 @@ func makeBytesArshaler(t reflect.Type, fncs *arshaler) *arshaler {
|
||||||
mo.Format = ""
|
mo.Format = ""
|
||||||
return marshalArray(enc, va, mo)
|
return marshalArray(enc, va, mo)
|
||||||
default:
|
default:
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
} else if mo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
|
} else if mo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
|
||||||
return marshalArray(enc, va, mo)
|
return marshalArray(enc, va, mo)
|
||||||
|
|
@ -365,7 +365,7 @@ func makeBytesArshaler(t reflect.Type, fncs *arshaler) *arshaler {
|
||||||
uo.Format = ""
|
uo.Format = ""
|
||||||
return unmarshalArray(dec, va, uo)
|
return unmarshalArray(dec, va, uo)
|
||||||
default:
|
default:
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
} else if uo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
|
} else if uo.Flags.Get(jsonflags.FormatByteArrayAsArray) && va.Kind() == reflect.Array {
|
||||||
return unmarshalArray(dec, va, uo)
|
return unmarshalArray(dec, va, uo)
|
||||||
|
|
@ -433,7 +433,7 @@ func makeIntArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
||||||
xe := export.Encoder(enc)
|
xe := export.Encoder(enc)
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimize for marshaling without preceding whitespace or string escaping.
|
// Optimize for marshaling without preceding whitespace or string escaping.
|
||||||
|
|
@ -454,7 +454,7 @@ func makeIntArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
||||||
xd := export.Decoder(dec)
|
xd := export.Decoder(dec)
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
||||||
var flags jsonwire.ValueFlags
|
var flags jsonwire.ValueFlags
|
||||||
|
|
@ -520,7 +520,7 @@ func makeUintArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
||||||
xe := export.Encoder(enc)
|
xe := export.Encoder(enc)
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimize for marshaling without preceding whitespace or string escaping.
|
// Optimize for marshaling without preceding whitespace or string escaping.
|
||||||
|
|
@ -541,7 +541,7 @@ func makeUintArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
||||||
xd := export.Decoder(dec)
|
xd := export.Decoder(dec)
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
||||||
var flags jsonwire.ValueFlags
|
var flags jsonwire.ValueFlags
|
||||||
|
|
@ -602,7 +602,7 @@ func makeFloatArshaler(t reflect.Type) *arshaler {
|
||||||
if mo.Format == "nonfinite" {
|
if mo.Format == "nonfinite" {
|
||||||
allowNonFinite = true
|
allowNonFinite = true
|
||||||
} else {
|
} else {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -637,7 +637,7 @@ func makeFloatArshaler(t reflect.Type) *arshaler {
|
||||||
if uo.Format == "nonfinite" {
|
if uo.Format == "nonfinite" {
|
||||||
allowNonFinite = true
|
allowNonFinite = true
|
||||||
} else {
|
} else {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
stringify := xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
||||||
|
|
@ -737,7 +737,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
|
||||||
emitNull = false
|
emitNull = false
|
||||||
mo.Format = ""
|
mo.Format = ""
|
||||||
default:
|
default:
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -882,7 +882,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
|
||||||
case "emitnull", "emitempty":
|
case "emitnull", "emitempty":
|
||||||
uo.Format = "" // only relevant for marshaling
|
uo.Format = "" // only relevant for marshaling
|
||||||
default:
|
default:
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tok, err := dec.ReadToken()
|
tok, err := dec.ReadToken()
|
||||||
|
|
@ -992,7 +992,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
|
||||||
}
|
}
|
||||||
return errUnmarshal
|
return errUnmarshal
|
||||||
}
|
}
|
||||||
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
|
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
|
||||||
}
|
}
|
||||||
return &fncs
|
return &fncs
|
||||||
}
|
}
|
||||||
|
|
@ -1037,7 +1037,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
||||||
xe := export.Encoder(enc)
|
xe := export.Encoder(enc)
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
once.Do(init)
|
once.Do(init)
|
||||||
if errInit != nil && !mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
|
if errInit != nil && !mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
|
||||||
|
|
@ -1199,7 +1199,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
||||||
xd := export.Decoder(dec)
|
xd := export.Decoder(dec)
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
tok, err := dec.ReadToken()
|
tok, err := dec.ReadToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -1317,7 +1317,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
|
||||||
}
|
}
|
||||||
return errUnmarshal
|
return errUnmarshal
|
||||||
}
|
}
|
||||||
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
|
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
|
||||||
}
|
}
|
||||||
return &fncs
|
return &fncs
|
||||||
}
|
}
|
||||||
|
|
@ -1414,7 +1414,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
|
||||||
emitNull = false
|
emitNull = false
|
||||||
mo.Format = ""
|
mo.Format = ""
|
||||||
default:
|
default:
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1462,7 +1462,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
|
||||||
case "emitnull", "emitempty":
|
case "emitnull", "emitempty":
|
||||||
uo.Format = "" // only relevant for marshaling
|
uo.Format = "" // only relevant for marshaling
|
||||||
default:
|
default:
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1518,7 +1518,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
|
||||||
}
|
}
|
||||||
return errUnmarshal
|
return errUnmarshal
|
||||||
}
|
}
|
||||||
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
|
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
|
||||||
}
|
}
|
||||||
return &fncs
|
return &fncs
|
||||||
}
|
}
|
||||||
|
|
@ -1539,7 +1539,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
||||||
xe := export.Encoder(enc)
|
xe := export.Encoder(enc)
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
once.Do(init)
|
once.Do(init)
|
||||||
if err := enc.WriteToken(jsontext.BeginArray); err != nil {
|
if err := enc.WriteToken(jsontext.BeginArray); err != nil {
|
||||||
|
|
@ -1563,7 +1563,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
||||||
xd := export.Decoder(dec)
|
xd := export.Decoder(dec)
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
tok, err := dec.ReadToken()
|
tok, err := dec.ReadToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -1616,7 +1616,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
|
||||||
}
|
}
|
||||||
return errUnmarshal
|
return errUnmarshal
|
||||||
}
|
}
|
||||||
return newUnmarshalErrorAfterWithSkipping(dec, uo, t, nil)
|
return newUnmarshalErrorAfterWithSkipping(dec, t, nil)
|
||||||
}
|
}
|
||||||
return &fncs
|
return &fncs
|
||||||
}
|
}
|
||||||
|
|
@ -1706,7 +1706,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
|
||||||
xe := export.Encoder(enc)
|
xe := export.Encoder(enc)
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
if va.IsNil() {
|
if va.IsNil() {
|
||||||
return enc.WriteToken(jsontext.Null)
|
return enc.WriteToken(jsontext.Null)
|
||||||
|
|
@ -1746,7 +1746,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
|
||||||
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
|
||||||
xd := export.Decoder(dec)
|
xd := export.Decoder(dec)
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
if uo.Flags.Get(jsonflags.MergeWithLegacySemantics) && !va.IsNil() {
|
if uo.Flags.Get(jsonflags.MergeWithLegacySemantics) && !va.IsNil() {
|
||||||
// Legacy merge behavior is difficult to explain.
|
// Legacy merge behavior is difficult to explain.
|
||||||
|
|
@ -1795,7 +1795,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
|
||||||
|
|
||||||
k := dec.PeekKind()
|
k := dec.PeekKind()
|
||||||
if !isAnyType(t) {
|
if !isAnyType(t) {
|
||||||
return newUnmarshalErrorBeforeWithSkipping(dec, uo, t, internal.ErrNilInterface)
|
return newUnmarshalErrorBeforeWithSkipping(dec, t, internal.ErrNilInterface)
|
||||||
}
|
}
|
||||||
switch k {
|
switch k {
|
||||||
case 'f', 't':
|
case 'f', 't':
|
||||||
|
|
|
||||||
|
|
@ -188,7 +188,7 @@ func unmarshalInlinedFallbackNext(dec *jsontext.Decoder, va addressableValue, uo
|
||||||
*b = append(*b, ',')
|
*b = append(*b, ',')
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return newUnmarshalErrorAfterWithSkipping(dec, uo, v.Type(), errRawInlinedNotObject)
|
return newUnmarshalErrorAfterWithSkipping(dec, v.Type(), errRawInlinedNotObject)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*b = append(*b, quotedName...)
|
*b = append(*b, quotedName...)
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
|
||||||
var m durationArshaler
|
var m durationArshaler
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
if !m.initFormat(mo.Format) {
|
if !m.initFormat(mo.Format) {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
} else if mo.Flags.Get(jsonflags.FormatDurationAsNano) {
|
} else if mo.Flags.Get(jsonflags.FormatDurationAsNano) {
|
||||||
return marshalNano(enc, va, mo)
|
return marshalNano(enc, va, mo)
|
||||||
|
|
@ -74,13 +74,13 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
|
||||||
var u durationArshaler
|
var u durationArshaler
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
if !u.initFormat(uo.Format) {
|
if !u.initFormat(uo.Format) {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
} else if uo.Flags.Get(jsonflags.FormatDurationAsNano) {
|
} else if uo.Flags.Get(jsonflags.FormatDurationAsNano) {
|
||||||
return unmarshalNano(dec, va, uo)
|
return unmarshalNano(dec, va, uo)
|
||||||
} else {
|
} else {
|
||||||
// TODO(https://go.dev/issue/71631): Decide on default duration representation.
|
// TODO(https://go.dev/issue/71631): Decide on default duration representation.
|
||||||
return newUnmarshalErrorBeforeWithSkipping(dec, uo, t, errors.New("no default representation (see https://go.dev/issue/71631); specify an explicit format"))
|
return newUnmarshalErrorBeforeWithSkipping(dec, t, errors.New("no default representation (see https://go.dev/issue/71631); specify an explicit format"))
|
||||||
}
|
}
|
||||||
|
|
||||||
stringify := !u.isNumeric() || xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
stringify := !u.isNumeric() || xd.Tokens.Last.NeedObjectName() || uo.Flags.Get(jsonflags.StringifyNumbers)
|
||||||
|
|
@ -125,7 +125,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
|
||||||
var m timeArshaler
|
var m timeArshaler
|
||||||
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
if mo.Format != "" && mo.FormatDepth == xe.Tokens.Depth() {
|
||||||
if !m.initFormat(mo.Format) {
|
if !m.initFormat(mo.Format) {
|
||||||
return newInvalidFormatError(enc, t, mo)
|
return newInvalidFormatError(enc, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -148,7 +148,7 @@ func makeTimeArshaler(fncs *arshaler, t reflect.Type) *arshaler {
|
||||||
var u timeArshaler
|
var u timeArshaler
|
||||||
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
if uo.Format != "" && uo.FormatDepth == xd.Tokens.Depth() {
|
||||||
if !u.initFormat(uo.Format) {
|
if !u.initFormat(uo.Format) {
|
||||||
return newInvalidFormatError(dec, t, uo)
|
return newInvalidFormatError(dec, t)
|
||||||
}
|
}
|
||||||
} else if uo.Flags.Get(jsonflags.ParseTimeWithLooseRFC3339) {
|
} else if uo.Flags.Get(jsonflags.ParseTimeWithLooseRFC3339) {
|
||||||
u.looseRFC3339 = true
|
u.looseRFC3339 = true
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,10 @@ type SemanticError struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// coder is implemented by [jsontext.Encoder] or [jsontext.Decoder].
|
// coder is implemented by [jsontext.Encoder] or [jsontext.Decoder].
|
||||||
type coder interface{ StackPointer() jsontext.Pointer }
|
type coder interface {
|
||||||
|
StackPointer() jsontext.Pointer
|
||||||
|
Options() Options
|
||||||
|
}
|
||||||
|
|
||||||
// newInvalidFormatError wraps err in a SemanticError because
|
// newInvalidFormatError wraps err in a SemanticError because
|
||||||
// the current type t cannot handle the provided options format.
|
// the current type t cannot handle the provided options format.
|
||||||
|
|
@ -97,13 +100,13 @@ type coder interface{ StackPointer() jsontext.Pointer }
|
||||||
// If [jsonflags.ReportErrorsWithLegacySemantics] is specified,
|
// If [jsonflags.ReportErrorsWithLegacySemantics] is specified,
|
||||||
// then this automatically skips the next value when unmarshaling
|
// then this automatically skips the next value when unmarshaling
|
||||||
// to ensure that the value is fully consumed.
|
// to ensure that the value is fully consumed.
|
||||||
func newInvalidFormatError(c coder, t reflect.Type, o *jsonopts.Struct) error {
|
func newInvalidFormatError(c coder, t reflect.Type) error {
|
||||||
err := fmt.Errorf("invalid format flag %q", o.Format)
|
err := fmt.Errorf("invalid format flag %q", c.Options().(*jsonopts.Struct).Format)
|
||||||
switch c := c.(type) {
|
switch c := c.(type) {
|
||||||
case *jsontext.Encoder:
|
case *jsontext.Encoder:
|
||||||
err = newMarshalErrorBefore(c, t, err)
|
err = newMarshalErrorBefore(c, t, err)
|
||||||
case *jsontext.Decoder:
|
case *jsontext.Decoder:
|
||||||
err = newUnmarshalErrorBeforeWithSkipping(c, o, t, err)
|
err = newUnmarshalErrorBeforeWithSkipping(c, t, err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -136,9 +139,9 @@ func newUnmarshalErrorBefore(d *jsontext.Decoder, t reflect.Type, err error) err
|
||||||
// newUnmarshalErrorBeforeWithSkipping is like [newUnmarshalErrorBefore],
|
// newUnmarshalErrorBeforeWithSkipping is like [newUnmarshalErrorBefore],
|
||||||
// but automatically skips the next value if
|
// but automatically skips the next value if
|
||||||
// [jsonflags.ReportErrorsWithLegacySemantics] is specified.
|
// [jsonflags.ReportErrorsWithLegacySemantics] is specified.
|
||||||
func newUnmarshalErrorBeforeWithSkipping(d *jsontext.Decoder, o *jsonopts.Struct, t reflect.Type, err error) error {
|
func newUnmarshalErrorBeforeWithSkipping(d *jsontext.Decoder, t reflect.Type, err error) error {
|
||||||
err = newUnmarshalErrorBefore(d, t, err)
|
err = newUnmarshalErrorBefore(d, t, err)
|
||||||
if o.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
|
if export.Decoder(d).Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
|
||||||
if err2 := export.Decoder(d).SkipValue(); err2 != nil {
|
if err2 := export.Decoder(d).SkipValue(); err2 != nil {
|
||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
|
|
@ -170,9 +173,9 @@ func newUnmarshalErrorAfterWithValue(d *jsontext.Decoder, t reflect.Type, err er
|
||||||
// newUnmarshalErrorAfterWithSkipping is like [newUnmarshalErrorAfter],
|
// newUnmarshalErrorAfterWithSkipping is like [newUnmarshalErrorAfter],
|
||||||
// but automatically skips the remainder of the current value if
|
// but automatically skips the remainder of the current value if
|
||||||
// [jsonflags.ReportErrorsWithLegacySemantics] is specified.
|
// [jsonflags.ReportErrorsWithLegacySemantics] is specified.
|
||||||
func newUnmarshalErrorAfterWithSkipping(d *jsontext.Decoder, o *jsonopts.Struct, t reflect.Type, err error) error {
|
func newUnmarshalErrorAfterWithSkipping(d *jsontext.Decoder, t reflect.Type, err error) error {
|
||||||
err = newUnmarshalErrorAfter(d, t, err)
|
err = newUnmarshalErrorAfter(d, t, err)
|
||||||
if o.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
|
if export.Decoder(d).Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
|
||||||
if err2 := export.Decoder(d).SkipValueRemainder(); err2 != nil {
|
if err2 := export.Decoder(d).SkipValueRemainder(); err2 != nil {
|
||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue