Self referencing loop when converting WordDocument object to json.

Environment: 

  • Newtonsoft.Json 13.0.3,
  • Syncfusion.DocIO.AspNet.Mvc5 28.2.6
  • dotnet framework 4.2.6.

Issue: I'm trying to convert a WordDocument object to JSON, following the example provided here. However, I encountered the following error:

Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'Document' with type 'Syncfusion.DocIO.DLS.WordDocument'. Path 'Data.Footnotes.Separator.Paragraphs[0].ChildEntities[0].CharacterFormat'.'

Attempts to Resolve:

  1. ReferenceLoopHandling.Ignore: This setting caused a memory leak, eventually leading to the program being killed by the OS.
  2. ReferenceLoopHandling.Serialize: This setting resulted in a stack overflow.
  3. PreserveReferencesHandling.Objects: This setting did not resolve the self-referencing exception.

Could you please help me solve this problem? Any guidance or suggestions would be greatly appreciated. Thank you in advance!


1 Reply

DS Dhanush Sekar Syncfusion Team February 19, 2025 02:35 PM UTC

Hi Benjamin,


The problem arises from serializing the DocIO instance. To resolve this issue, please follow these steps:

  • Add below Nuget to your project

    <PackageReference Include="Syncfusion.EJ2.WordEditor.AspNet.Mvc5" Version="28.2.6" />

  • Add the following statement to differentiate the DocIO and EJ2Document instances
    using EJ2WordDocument = Syncfusion.EJ2.DocumentEditor.WordDocument; using DocIOWordDocument= Syncfusion.DocIO.DLS.WordDocument;

  • Pass the stream DocumenEditor Load API and serialize the EJ2Document editor document instance. 
    EJ2WordDocument document = EJ2WordDocument.Load(stream, FormatType.Docx); string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); document.Dispose();

Regards,

Dhanush Sekar


Loader.
Up arrow icon