@@ -409,7 +409,7 @@ func Unmarshal(in []byte, out any, opts ...Options) (err error) {
409
409
dec := export .GetBufferedDecoder (in , opts ... )
410
410
defer export .PutBufferedDecoder (dec )
411
411
xd := export .Decoder (dec )
412
- err = unmarshalFull (dec , out , & xd .Struct )
412
+ err = unmarshalDecode (dec , out , & xd .Struct , true )
413
413
if err != nil && xd .Flags .Get (jsonflags .ReportErrorsWithLegacySemantics ) {
414
414
return internal .TransformUnmarshalError (out , err )
415
415
}
@@ -426,25 +426,13 @@ func UnmarshalRead(in io.Reader, out any, opts ...Options) (err error) {
426
426
dec := export .GetStreamingDecoder (in , opts ... )
427
427
defer export .PutStreamingDecoder (dec )
428
428
xd := export .Decoder (dec )
429
- err = unmarshalFull (dec , out , & xd .Struct )
429
+ err = unmarshalDecode (dec , out , & xd .Struct , true )
430
430
if err != nil && xd .Flags .Get (jsonflags .ReportErrorsWithLegacySemantics ) {
431
431
return internal .TransformUnmarshalError (out , err )
432
432
}
433
433
return err
434
434
}
435
435
436
- func unmarshalFull (in * jsontext.Decoder , out any , uo * jsonopts.Struct ) error {
437
- switch err := unmarshalDecode (in , out , uo ); err {
438
- case nil :
439
- return export .Decoder (in ).CheckEOF ()
440
- case io .EOF :
441
- offset := in .InputOffset () + int64 (len (in .UnreadBuffer ()))
442
- return & jsontext.SyntacticError {ByteOffset : offset , Err : io .ErrUnexpectedEOF }
443
- default :
444
- return err
445
- }
446
- }
447
-
448
436
// UnmarshalDecode deserializes a Go value from a [jsontext.Decoder] according to
449
437
// the provided unmarshal options (while ignoring marshal, encode, or decode options).
450
438
// Any unmarshal options already specified on the [jsontext.Decoder]
@@ -463,14 +451,14 @@ func UnmarshalDecode(in *jsontext.Decoder, out any, opts ...Options) (err error)
463
451
defer func () { xd .Struct = optsOriginal }()
464
452
xd .Struct .JoinWithoutCoderOptions (opts ... )
465
453
}
466
- err = unmarshalDecode (in , out , & xd .Struct )
454
+ err = unmarshalDecode (in , out , & xd .Struct , false )
467
455
if err != nil && xd .Flags .Get (jsonflags .ReportErrorsWithLegacySemantics ) {
468
456
return internal .TransformUnmarshalError (out , err )
469
457
}
470
458
return err
471
459
}
472
460
473
- func unmarshalDecode (in * jsontext.Decoder , out any , uo * jsonopts.Struct ) (err error ) {
461
+ func unmarshalDecode (in * jsontext.Decoder , out any , uo * jsonopts.Struct , last bool ) (err error ) {
474
462
v := reflect .ValueOf (out )
475
463
if v .Kind () != reflect .Pointer || v .IsNil () {
476
464
return & SemanticError {action : "unmarshal" , GoType : reflect .TypeOf (out ), Err : internal .ErrNonNilReference }
@@ -481,7 +469,11 @@ func unmarshalDecode(in *jsontext.Decoder, out any, uo *jsonopts.Struct) (err er
481
469
// In legacy semantics, the entirety of the next JSON value
482
470
// was validated before attempting to unmarshal it.
483
471
if uo .Flags .Get (jsonflags .ReportErrorsWithLegacySemantics ) {
484
- if err := export .Decoder (in ).CheckNextValue (); err != nil {
472
+ if err := export .Decoder (in ).CheckNextValue (last ); err != nil {
473
+ if err == io .EOF {
474
+ offset := in .InputOffset () + int64 (len (in .UnreadBuffer ()))
475
+ return & jsontext.SyntacticError {ByteOffset : offset , Err : io .ErrUnexpectedEOF }
476
+ }
485
477
return err
486
478
}
487
479
}
@@ -495,8 +487,15 @@ func unmarshalDecode(in *jsontext.Decoder, out any, uo *jsonopts.Struct) (err er
495
487
if ! uo .Flags .Get (jsonflags .AllowDuplicateNames ) {
496
488
export .Decoder (in ).Tokens .InvalidateDisabledNamespaces ()
497
489
}
490
+ if err == io .EOF {
491
+ offset := in .InputOffset () + int64 (len (in .UnreadBuffer ()))
492
+ return & jsontext.SyntacticError {ByteOffset : offset , Err : io .ErrUnexpectedEOF }
493
+ }
498
494
return err
499
495
}
496
+ if last {
497
+ return export .Decoder (in ).CheckEOF ()
498
+ }
500
499
return nil
501
500
}
502
501
0 commit comments