Skip to content

Commit 23b16f8

Browse files
committed
Use arshal flags instead of coder flags
Instead of consulting the flags on the coder via: export.Encoder(enc).Flags export.Decoder(dec).Flags consult flags on the call-provided arshal options via: mo.Flags uo.Flags This avoids unnecessarily peaking at the internals flags of Encoder or Decoder and also allows us to simplify some flag checking logic. For example, the following are all equivalent: export.Encoder(enc).Flags.Get(Foo) || mo.Flags.Get(Bar) mo.Flags.Get(Foo) || mo.Flags.Get(Bar) mo.Flags.Get(Foo | Bar) A similar transform can also be done by applying De Morgan's law to the following: !export.Encoder(enc).Flags.Get(Foo) && !mo.Flags.Get(Bar) !mo.Flags.Get(Foo) && !mo.Flags.Get(Bar) !!(!mo.Flags.Get(Foo) && !mo.Flags.Get(Bar)) !(mo.Flags.Get(Foo) || mo.Flags.Get(Bar)) !(mo.Flags.Get(Foo | Bar)) !mo.Flags.Get(Foo | Bar) There should be no behavior changes as a result of this since the arshal option flags should be an exact superset of the coder option flags. There are 6 entry points to the "json" package: * Marshal * MarshalWrite * MarshaEncode * Unmarshal * UnmarshalRead * UnmarshalDecode 4 of them (Marshal, MarshalWrite, Unmarshal, UnmarshalRead) obtain a coder from an internal pool, where the arshal options plumbed down the call stack is a pointer to the one inside the coder. Therefore, there is no behavior difference for these 4 calls. 2 of them (MarshalEncode, UnmarshalDecode) take in a user-provided coder, where there could theoretically be a difference between the options struct and the coder options. However, in both functions, we obtain a jsonopts.Struct locally from an internal pool and then call jsonopts.Struct.CopyCoderOptions where the coder options are copied from the user-provided coder into the local jsonopts.Struct. Thus, the options struct that we plumb down the stack is gauranteed to be a superset of the options in the user-provided coder.
1 parent 400546f commit 23b16f8

6 files changed

+42
-45
lines changed

arshal.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,8 @@ func marshalEncode(out *jsontext.Encoder, in any, mo *jsonopts.Struct) (err erro
225225
marshal, _ = mo.Marshalers.(*Marshalers).lookup(marshal, t)
226226
}
227227
if err := marshal(out, va, mo); err != nil {
228-
xe := export.Encoder(out)
229-
if !xe.Flags.Get(jsonflags.AllowDuplicateNames) {
230-
xe.Tokens.InvalidateDisabledNamespaces()
228+
if !mo.Flags.Get(jsonflags.AllowDuplicateNames) {
229+
export.Encoder(out).Tokens.InvalidateDisabledNamespaces()
231230
}
232231
return err
233232
}
@@ -461,9 +460,8 @@ func unmarshalDecode(in *jsontext.Decoder, out any, uo *jsonopts.Struct) (err er
461460
unmarshal, _ = uo.Unmarshalers.(*Unmarshalers).lookup(unmarshal, t)
462461
}
463462
if err := unmarshal(in, va, uo); err != nil {
464-
xd := export.Decoder(in)
465-
if !xd.Flags.Get(jsonflags.AllowDuplicateNames) {
466-
xd.Tokens.InvalidateDisabledNamespaces()
463+
if !uo.Flags.Get(jsonflags.AllowDuplicateNames) {
464+
export.Decoder(in).Tokens.InvalidateDisabledNamespaces()
467465
}
468466
return err
469467
}

arshal_any.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.St
103103
return enc.WriteToken(jsontext.Null)
104104
}
105105
// Optimize for marshaling an empty map without any preceding whitespace.
106-
if !xe.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
106+
if !mo.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
107107
xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '{'), "{}"...)
108108
xe.Tokens.Last.Increment()
109109
if xe.NeedFlush() {
@@ -118,7 +118,7 @@ func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.St
118118
}
119119
// A Go map guarantees that each entry has a unique key
120120
// The only possibility of duplicates is due to invalid UTF-8.
121-
if !xe.Flags.Get(jsonflags.AllowInvalidUTF8) {
121+
if !mo.Flags.Get(jsonflags.AllowInvalidUTF8) {
122122
xe.Tokens.Last.DisableNamespace()
123123
}
124124
if !mo.Flags.Get(jsonflags.Deterministic) || len(obj) <= 1 {
@@ -168,7 +168,7 @@ func unmarshalObjectAny(dec *jsontext.Decoder, uo *jsonopts.Struct) (map[string]
168168
obj := make(map[string]any)
169169
// A Go map guarantees that each entry has a unique key
170170
// The only possibility of duplicates is due to invalid UTF-8.
171-
if !xd.Flags.Get(jsonflags.AllowInvalidUTF8) {
171+
if !uo.Flags.Get(jsonflags.AllowInvalidUTF8) {
172172
xd.Tokens.Last.DisableNamespace()
173173
}
174174
for dec.PeekKind() != '}' {
@@ -217,7 +217,7 @@ func marshalArrayAny(enc *jsontext.Encoder, arr []any, mo *jsonopts.Struct) erro
217217
return enc.WriteToken(jsontext.Null)
218218
}
219219
// Optimize for marshaling an empty slice without any preceding whitespace.
220-
if !xe.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
220+
if !mo.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
221221
xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '['), "[]"...)
222222
xe.Tokens.Last.Increment()
223223
if xe.NeedFlush() {

arshal_default.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func makeBoolArshaler(t reflect.Type) *arshaler {
127127
}
128128

129129
// Optimize for marshaling without preceding whitespace.
130-
if optimizeCommon && !xe.Flags.Get(jsonflags.AnyWhitespace) && !mo.Flags.Get(jsonflags.StringifyBoolsAndStrings) && !xe.Tokens.Last.NeedObjectName() {
130+
if optimizeCommon && !mo.Flags.Get(jsonflags.AnyWhitespace|jsonflags.StringifyBoolsAndStrings) && !xe.Tokens.Last.NeedObjectName() {
131131
xe.Buf = strconv.AppendBool(xe.Tokens.MayAppendDelim(xe.Buf, 't'), va.Bool())
132132
xe.Tokens.Last.Increment()
133133
if xe.NeedFlush() {
@@ -200,7 +200,7 @@ func makeStringArshaler(t reflect.Type) *arshaler {
200200

201201
// Optimize for marshaling without preceding whitespace or string escaping.
202202
s := va.String()
203-
if optimizeCommon && !xe.Flags.Get(jsonflags.AnyWhitespace) && !mo.Flags.Get(jsonflags.StringifyBoolsAndStrings) && !xe.Tokens.Last.NeedObjectName() && !jsonwire.NeedEscape(s) {
203+
if optimizeCommon && !mo.Flags.Get(jsonflags.AnyWhitespace|jsonflags.StringifyBoolsAndStrings) && !xe.Tokens.Last.NeedObjectName() && !jsonwire.NeedEscape(s) {
204204
b := xe.Buf
205205
b = xe.Tokens.MayAppendDelim(b, '"')
206206
b = append(b, '"')
@@ -446,7 +446,7 @@ func makeIntArshaler(t reflect.Type) *arshaler {
446446
}
447447

448448
// Optimize for marshaling without preceding whitespace or string escaping.
449-
if optimizeCommon && !xe.Flags.Get(jsonflags.AnyWhitespace) && !mo.Flags.Get(jsonflags.StringifyNumbers) && !xe.Tokens.Last.NeedObjectName() {
449+
if optimizeCommon && !mo.Flags.Get(jsonflags.AnyWhitespace|jsonflags.StringifyNumbers) && !xe.Tokens.Last.NeedObjectName() {
450450
xe.Buf = strconv.AppendInt(xe.Tokens.MayAppendDelim(xe.Buf, '0'), va.Int(), 10)
451451
xe.Tokens.Last.Increment()
452452
if xe.NeedFlush() {
@@ -533,7 +533,7 @@ func makeUintArshaler(t reflect.Type) *arshaler {
533533
}
534534

535535
// Optimize for marshaling without preceding whitespace or string escaping.
536-
if optimizeCommon && !xe.Flags.Get(jsonflags.AnyWhitespace) && !mo.Flags.Get(jsonflags.StringifyNumbers) && !xe.Tokens.Last.NeedObjectName() {
536+
if optimizeCommon && !mo.Flags.Get(jsonflags.AnyWhitespace|jsonflags.StringifyNumbers) && !xe.Tokens.Last.NeedObjectName() {
537537
xe.Buf = strconv.AppendUint(xe.Tokens.MayAppendDelim(xe.Buf, '0'), va.Uint(), 10)
538538
xe.Tokens.Last.Increment()
539539
if xe.NeedFlush() {
@@ -625,7 +625,7 @@ func makeFloatArshaler(t reflect.Type) *arshaler {
625625
}
626626

627627
// Optimize for marshaling without preceding whitespace or string escaping.
628-
if optimizeCommon && !xe.Flags.Get(jsonflags.AnyWhitespace) && !mo.Flags.Get(jsonflags.StringifyNumbers) && !xe.Tokens.Last.NeedObjectName() {
628+
if optimizeCommon && !mo.Flags.Get(jsonflags.AnyWhitespace|jsonflags.StringifyNumbers) && !xe.Tokens.Last.NeedObjectName() {
629629
xe.Buf = jsonwire.AppendFloat(xe.Tokens.MayAppendDelim(xe.Buf, '0'), fv, bits)
630630
xe.Tokens.Last.Increment()
631631
if xe.NeedFlush() {
@@ -755,7 +755,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
755755
return enc.WriteToken(jsontext.Null)
756756
}
757757
// Optimize for marshaling an empty map without any preceding whitespace.
758-
if optimizeCommon && !xe.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
758+
if optimizeCommon && !mo.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
759759
xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '{'), "{}"...)
760760
xe.Tokens.Last.Increment()
761761
if xe.NeedFlush() {
@@ -785,7 +785,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
785785
// A Go map guarantees that each entry has a unique key.
786786
// As such, disable the expensive duplicate name check if we know
787787
// that every Go key will serialize as a unique JSON string.
788-
if !nonDefaultKey && mapKeyWithUniqueRepresentation(k.Kind(), xe.Flags.Get(jsonflags.AllowInvalidUTF8)) {
788+
if !nonDefaultKey && mapKeyWithUniqueRepresentation(k.Kind(), mo.Flags.Get(jsonflags.AllowInvalidUTF8)) {
789789
xe.Tokens.Last.DisableNamespace()
790790
}
791791

@@ -913,7 +913,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
913913
// will be rejected as duplicates since they semantically refer
914914
// to the same Go value. This is an unusual interaction
915915
// between syntax and semantics, but is more correct.
916-
if !nonDefaultKey && mapKeyWithUniqueRepresentation(k.Kind(), xd.Flags.Get(jsonflags.AllowInvalidUTF8)) {
916+
if !nonDefaultKey && mapKeyWithUniqueRepresentation(k.Kind(), uo.Flags.Get(jsonflags.AllowInvalidUTF8)) {
917917
xd.Tokens.Last.DisableNamespace()
918918
}
919919

@@ -922,7 +922,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
922922
// since existing presence alone is insufficient to indicate
923923
// whether the input had a duplicate name.
924924
var seen reflect.Value
925-
if !xd.Flags.Get(jsonflags.AllowDuplicateNames) && va.Len() > 0 {
925+
if !uo.Flags.Get(jsonflags.AllowDuplicateNames) && va.Len() > 0 {
926926
seen = reflect.MakeMap(reflect.MapOf(k.Type(), emptyStructType))
927927
}
928928

@@ -941,7 +941,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
941941
}
942942

943943
if v2 := va.MapIndex(k.Value); v2.IsValid() {
944-
if !xd.Flags.Get(jsonflags.AllowDuplicateNames) && (!seen.IsValid() || seen.MapIndex(k.Value).IsValid()) {
944+
if !uo.Flags.Get(jsonflags.AllowDuplicateNames) && (!seen.IsValid() || seen.MapIndex(k.Value).IsValid()) {
945945
// TODO: Unread the object name.
946946
name := xd.PreviousTokenOrValue()
947947
return newDuplicateNameError(dec.StackPointer(), nil, dec.InputOffset()-len64(name))
@@ -1078,20 +1078,20 @@ func makeStructArshaler(t reflect.Type) *arshaler {
10781078
b := xe.Buf
10791079
if xe.Tokens.Last.Length() > 0 {
10801080
b = append(b, ',')
1081-
if xe.Flags.Get(jsonflags.SpaceAfterComma) {
1081+
if mo.Flags.Get(jsonflags.SpaceAfterComma) {
10821082
b = append(b, ' ')
10831083
}
10841084
}
1085-
if xe.Flags.Get(jsonflags.Multiline) {
1085+
if mo.Flags.Get(jsonflags.Multiline) {
10861086
b = xe.AppendIndent(b, xe.Tokens.NeedIndent('"'))
10871087
}
10881088

10891089
// Append the token to the output and to the state machine.
10901090
n0 := len(b) // offset before calling AppendQuote
1091-
if !xe.Flags.Get(jsonflags.EscapeForHTML | jsonflags.EscapeForJS | jsonflags.EscapeInvalidUTF8) {
1091+
if !mo.Flags.Get(jsonflags.EscapeForHTML | jsonflags.EscapeForJS | jsonflags.EscapeInvalidUTF8) {
10921092
b = append(b, f.quotedName...)
10931093
} else {
1094-
b, _ = jsonwire.AppendQuote(b, f.name, &xe.Flags)
1094+
b, _ = jsonwire.AppendQuote(b, f.name, &mo.Flags)
10951095
}
10961096
xe.Buf = b
10971097
xe.Names.ReplaceLastQuotedOffset(n0)
@@ -1136,14 +1136,14 @@ func makeStructArshaler(t reflect.Type) *arshaler {
11361136
// Remember the previous written object member.
11371137
// The set of seen fields only needs to be updated to detect
11381138
// duplicate names with those from the inlined fallback.
1139-
if !xe.Flags.Get(jsonflags.AllowDuplicateNames) && fields.inlinedFallback != nil {
1139+
if !mo.Flags.Get(jsonflags.AllowDuplicateNames) && fields.inlinedFallback != nil {
11401140
seenIdxs.insert(uint(f.id))
11411141
}
11421142
prevIdx = f.id
11431143
}
11441144
if fields.inlinedFallback != nil && !(mo.Flags.Get(jsonflags.DiscardUnknownMembers) && fields.inlinedFallback.unknown) {
11451145
var insertUnquotedName func([]byte) bool
1146-
if !xe.Flags.Get(jsonflags.AllowDuplicateNames) {
1146+
if !mo.Flags.Get(jsonflags.AllowDuplicateNames) {
11471147
insertUnquotedName = func(name []byte) bool {
11481148
// Check that the name from inlined fallback does not match
11491149
// one of the previously marshaled names from known fields.
@@ -1215,7 +1215,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
12151215
if uo.Flags.Get(jsonflags.RejectUnknownMembers) && (fields.inlinedFallback == nil || fields.inlinedFallback.unknown) {
12161216
return newUnmarshalErrorAfter(dec, t, ErrUnknownName)
12171217
}
1218-
if !xd.Flags.Get(jsonflags.AllowDuplicateNames) && !xd.Namespaces.Last().InsertUnquoted(name) {
1218+
if !uo.Flags.Get(jsonflags.AllowDuplicateNames) && !xd.Namespaces.Last().InsertUnquoted(name) {
12191219
// TODO: Unread the object name.
12201220
return newDuplicateNameError(dec.StackPointer(), nil, dec.InputOffset()-len64(val))
12211221
}
@@ -1234,7 +1234,7 @@ func makeStructArshaler(t reflect.Type) *arshaler {
12341234
continue
12351235
}
12361236
}
1237-
if !xd.Flags.Get(jsonflags.AllowDuplicateNames) && !seenIdxs.insert(uint(f.id)) {
1237+
if !uo.Flags.Get(jsonflags.AllowDuplicateNames) && !seenIdxs.insert(uint(f.id)) {
12381238
// TODO: Unread the object name.
12391239
return newDuplicateNameError(dec.StackPointer(), nil, dec.InputOffset()-len64(val))
12401240
}
@@ -1383,7 +1383,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
13831383
return enc.WriteToken(jsontext.Null)
13841384
}
13851385
// Optimize for marshaling an empty slice without any preceding whitespace.
1386-
if optimizeCommon && !xe.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
1386+
if optimizeCommon && !mo.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
13871387
xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '['), "[]"...)
13881388
xe.Tokens.Last.Increment()
13891389
if xe.NeedFlush() {
@@ -1624,7 +1624,7 @@ func makePointerArshaler(t reflect.Type) *arshaler {
16241624
return err
16251625
}
16261626
if uo.Flags.Get(jsonflags.StringifyWithLegacySemantics) &&
1627-
(uo.Flags.Get(jsonflags.StringifyNumbers) || uo.Flags.Get(jsonflags.StringifyBoolsAndStrings)) {
1627+
uo.Flags.Get(jsonflags.StringifyNumbers|jsonflags.StringifyBoolsAndStrings) {
16281628
// A JSON null quoted within a JSON string should take effect
16291629
// within the pointer value, rather than the indirect value.
16301630
//
@@ -1664,7 +1664,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
16641664
}
16651665
// Optimize for the any type if there are no special options.
16661666
if optimizeCommon &&
1667-
t == anyType && !mo.Flags.Get(jsonflags.StringifyNumbers) && !mo.Flags.Get(jsonflags.StringifyBoolsAndStrings) && mo.Format == "" &&
1667+
t == anyType && !mo.Flags.Get(jsonflags.StringifyNumbers|jsonflags.StringifyBoolsAndStrings) && mo.Format == "" &&
16681668
(mo.Marshalers == nil || !mo.Marshalers.(*Marshalers).fromAny) {
16691669
return marshalValueAny(enc, va.Elem().Interface(), mo)
16701670
}
@@ -1709,7 +1709,7 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
17091709
// Duplicate name check must be enforced since unmarshalValueAny
17101710
// does not implement merge semantics.
17111711
if optimizeCommon &&
1712-
t == anyType && !xd.Flags.Get(jsonflags.AllowDuplicateNames) && uo.Format == "" &&
1712+
t == anyType && !uo.Flags.Get(jsonflags.AllowDuplicateNames) && uo.Format == "" &&
17131713
(uo.Unmarshalers == nil || !uo.Unmarshalers.(*Unmarshalers).fromAny) {
17141714
v, err := unmarshalValueAny(dec, uo)
17151715
// We must check for nil interface values up front.

arshal_funcs.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func MarshalFuncV1[T any](fn func(T) ([]byte, error)) *Marshalers {
178178
val, err := fn(va.castTo(t).Interface().(T))
179179
if err != nil {
180180
err = wrapSkipFunc(err, "marshal function of type func(T) ([]byte, error)")
181-
if export.Encoder(enc).Flags.Get(jsonflags.ReportLegacyErrorValues) {
181+
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
182182
return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalFuncV1") // unlike unmarshal, always wrapped
183183
}
184184
err = newMarshalErrorBefore(enc, t, err)
@@ -230,7 +230,7 @@ func MarshalFuncV2[T any](fn func(*jsontext.Encoder, T, Options) error) *Marshal
230230
}
231231
err = errSkipMutation
232232
}
233-
if xe.Flags.Get(jsonflags.ReportLegacyErrorValues) {
233+
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
234234
return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalFuncV2") // unlike unmarshal, always wrapped
235235
}
236236
if !export.IsIOError(err) {
@@ -267,7 +267,7 @@ func UnmarshalFuncV1[T any](fn func([]byte, T) error) *Unmarshalers {
267267
err = fn(val, va.castTo(t).Interface().(T))
268268
if err != nil {
269269
err = wrapSkipFunc(err, "unmarshal function of type func([]byte, T) error")
270-
if export.Decoder(dec).Flags.Get(jsonflags.ReportLegacyErrorValues) {
270+
if uo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
271271
return err // unlike marshal, never wrapped
272272
}
273273
err = newUnmarshalErrorAfter(dec, t, err)
@@ -312,7 +312,7 @@ func UnmarshalFuncV2[T any](fn func(*jsontext.Decoder, T, Options) error) *Unmar
312312
}
313313
err = errSkipMutation
314314
}
315-
if export.Decoder(dec).Flags.Get(jsonflags.ReportLegacyErrorValues) {
315+
if uo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
316316
return err // unlike marshal, never wrapped
317317
}
318318
if !isSyntacticError(err) && !export.IsIOError(err) {

arshal_inlined.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ func marshalInlinedFallbackAll(enc *jsontext.Encoder, va addressableValue, mo *j
111111
mk := newAddressableValue(m.Type().Key())
112112
mv := newAddressableValue(m.Type().Elem())
113113
marshalKey := func(mk addressableValue) error {
114-
xe := export.Encoder(enc)
115-
b, err := jsonwire.AppendQuote(enc.UnusedBuffer(), mk.String(), &xe.Flags)
114+
b, err := jsonwire.AppendQuote(enc.UnusedBuffer(), mk.String(), &mo.Flags)
116115
if err != nil {
117116
return newMarshalErrorBefore(enc, m.Type().Key(), err)
118117
}

arshal_methods.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
122122
return append(b, b2...), err
123123
}); err != nil {
124124
err = wrapSkipFunc(err, "marshal method")
125-
if export.Encoder(enc).Flags.Get(jsonflags.ReportLegacyErrorValues) {
125+
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
126126
return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalText") // unlike unmarshal, always wrapped
127127
}
128128
if !isSemanticError(err) && !export.IsIOError(err) {
@@ -155,7 +155,7 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
155155
appender := va.Addr().Interface().(encodingTextAppender)
156156
if err := export.Encoder(enc).AppendRaw('"', false, appender.AppendText); err != nil {
157157
err = wrapSkipFunc(err, "append method")
158-
if export.Encoder(enc).Flags.Get(jsonflags.ReportLegacyErrorValues) {
158+
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
159159
return internal.NewMarshalerError(va.Addr().Interface(), err, "AppendText") // unlike unmarshal, always wrapped
160160
}
161161
if !isSemanticError(err) && !export.IsIOError(err) {
@@ -174,7 +174,7 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
174174
val, err := marshaler.MarshalJSON()
175175
if err != nil {
176176
err = wrapSkipFunc(err, "marshal method")
177-
if export.Encoder(enc).Flags.Get(jsonflags.ReportLegacyErrorValues) {
177+
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
178178
return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSON") // unlike unmarshal, always wrapped
179179
}
180180
err = newMarshalErrorBefore(enc, t, err)
@@ -204,7 +204,7 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
204204
}
205205
if err != nil {
206206
err = wrapSkipFunc(err, "marshal method")
207-
if xe.Flags.Get(jsonflags.ReportLegacyErrorValues) {
207+
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
208208
return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSONV2") // unlike unmarshal, always wrapped
209209
}
210210
if !export.IsIOError(err) {
@@ -238,7 +238,7 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
238238
unmarshaler := va.Addr().Interface().(encoding.TextUnmarshaler)
239239
if err := unmarshaler.UnmarshalText(s); err != nil {
240240
err = wrapSkipFunc(err, "unmarshal method")
241-
if export.Decoder(dec).Flags.Get(jsonflags.ReportLegacyErrorValues) {
241+
if uo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
242242
return err // unlike marshal, never wrapped
243243
}
244244
if !isSemanticError(err) && !isSyntacticError(err) && !export.IsIOError(err) {
@@ -260,7 +260,7 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
260260
unmarshaler := va.Addr().Interface().(UnmarshalerV1)
261261
if err := unmarshaler.UnmarshalJSON(val); err != nil {
262262
err = wrapSkipFunc(err, "unmarshal method")
263-
if export.Decoder(dec).Flags.Get(jsonflags.ReportLegacyErrorValues) {
263+
if uo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
264264
return err // unlike marshal, never wrapped
265265
}
266266
err = newUnmarshalErrorAfter(dec, t, err)
@@ -284,7 +284,7 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
284284
}
285285
if err != nil {
286286
err = wrapSkipFunc(err, "unmarshal method")
287-
if xd.Flags.Get(jsonflags.ReportLegacyErrorValues) {
287+
if uo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
288288
return err // unlike marshal, never wrapped
289289
}
290290
if !isSyntacticError(err) && !export.IsIOError(err) {

0 commit comments

Comments
 (0)