@@ -580,14 +580,21 @@ func (f *Fory) Serialize(value any) ([]byte, error) {
580580 reflValue .Type (), reflValue .Type ())
581581 }
582582 }
583- f .writeCtx .WriteValue (reflValue , RefModeTracking , true )
583+ f .writeCtx .WriteValue (reflValue , f . rootRefMode () , true )
584584 if f .writeCtx .HasError () {
585585 return nil , f .writeCtx .TakeError ()
586586 }
587587
588588 return f .writeCtx .buffer .GetByteSlice (0 , f .writeCtx .buffer .writerIndex ), nil
589589}
590590
591+ func (f * Fory ) rootRefMode () RefMode {
592+ if f .config .TrackRef {
593+ return RefModeTracking
594+ }
595+ return RefModeNullOnly
596+ }
597+
591598// Deserialize deserializes data directly into the provided target value.
592599// The target must be a pointer to the value to deserialize into.
593600func (f * Fory ) Deserialize (data []byte , v any ) error {
@@ -602,7 +609,7 @@ func (f *Fory) Deserialize(data []byte, v any) error {
602609 return f .readCtx .TakeError ()
603610 }
604611
605- f .readCtx .ReadValue (target , RefModeTracking , true )
612+ f .readCtx .ReadValue (target , f . rootRefMode () , true )
606613 if f .readCtx .HasError () {
607614 return f .readCtx .TakeError ()
608615 }
@@ -668,7 +675,7 @@ func (f *Fory) SerializeTo(buf *ByteBuffer, value any) error {
668675 }
669676
670677 // Standard path - TypeMeta is written inline using streaming protocol
671- f .writeCtx .WriteValue (rv , RefModeTracking , true )
678+ f .writeCtx .WriteValue (rv , f . rootRefMode () , true )
672679 if f .writeCtx .HasError () {
673680 f .writeCtx .buffer = origBuffer
674681 return f .writeCtx .TakeError ()
@@ -700,7 +707,7 @@ func (f *Fory) DeserializeFrom(buf *ByteBuffer, v any) error {
700707 }
701708
702709 // Deserialize the value - TypeMeta is read inline using streaming protocol
703- f .readCtx .ReadValue (target , RefModeTracking , true )
710+ f .readCtx .ReadValue (target , f . rootRefMode () , true )
704711 if f .readCtx .HasError () {
705712 f .readCtx .buffer = origBuffer
706713 return f .readCtx .TakeError ()
@@ -768,7 +775,7 @@ func (f *Fory) SerializeWithCallback(buffer *ByteBuffer, v any, callback func(Bu
768775 writeHeader (f .writeCtx , f .config )
769776
770777 // Serialize the value - TypeMeta is written inline using streaming protocol
771- f .writeCtx .WriteValue (reflect .ValueOf (v ), RefModeTracking , true )
778+ f .writeCtx .WriteValue (reflect .ValueOf (v ), f . rootRefMode () , true )
772779 if f .writeCtx .HasError () {
773780 return f .writeCtx .TakeError ()
774781 }
@@ -819,7 +826,7 @@ func (f *Fory) DeserializeWithCallbackBuffers(buffer *ByteBuffer, v any, buffers
819826 }
820827
821828 // Deserialize the value - TypeMeta is read inline using streaming protocol
822- f .readCtx .ReadValue (target , RefModeTracking , true )
829+ f .readCtx .ReadValue (target , f . rootRefMode () , true )
823830 if f .readCtx .HasError () {
824831 return f .readCtx .TakeError ()
825832 }
@@ -839,7 +846,7 @@ func (f *Fory) serializeReflectValue(value reflect.Value) ([]byte, error) {
839846 }
840847
841848 // Serialize the value - TypeMeta is written inline using streaming protocol
842- f .writeCtx .WriteValue (value , RefModeTracking , true )
849+ f .writeCtx .WriteValue (value , f . rootRefMode () , true )
843850 if f .writeCtx .HasError () {
844851 return nil , f .writeCtx .TakeError ()
845852 }
0 commit comments