Json.Net Issues: StackOverflowException is thrown when serialising circular dependent ISerializable object with ReferenceLoopHandling.Ignore

Posted by keyr on Stack Overflow See other posts from Stack Overflow or by keyr
Published on 2012-11-16T07:40:35Z Indexed on 2012/11/29 5:04 UTC
Read the original article Hit count: 1722

Filed under:
|
|
|

I have a legacy application that used binary serialisation to persist the data. Now we wanted to use Json.net 4.5 to serialise the data without much changes to the existing classes.

Things were working nice till we hit a circular dependent class. Is there any workaround to solve this problem?

Sample code as shown below

    [Serializable]
    class Department : ISerializable
    {
        public Employee Manager { get; set; }
        public string Name { get; set; }

        public Department() { }
        public Department( SerializationInfo info, StreamingContext context )
        {
            Manager = ( Employee )info.GetValue( "Manager", typeof( Employee ) );
            Name = ( string )info.GetValue( "Name", typeof( string ) );
        }
        public void GetObjectData( SerializationInfo info, StreamingContext context )
        {
            info.AddValue( "Manager", Manager );
            info.AddValue( "Name", Name );
        }
    }

    [Serializable]
    class Employee : ISerializable
    {
        [NonSerialized] //This does not work
        [XmlIgnore]//This does not work
        private Department mDepartment;
        public Department Department
        {
            get { return mDepartment; }
            set { mDepartment = value; }
        }

        public string Name { get; set; }

        public Employee() { }
        public Employee( SerializationInfo info, StreamingContext context )
        {
            Department = ( Department )info.GetValue( "Department", typeof( Department ) );
            Name = ( string )info.GetValue( "Name", typeof( string ) );
        }

        public void GetObjectData( SerializationInfo info, StreamingContext context )
        {
            info.AddValue( "Department", Department );
            info.AddValue( "Name", Name );
        }
    }

And the test code

        Department department = new Department();
        department.Name = "Dept1";

        Employee emp1 = new Employee { Name = "Emp1", Department = department };
        department.Manager = emp1;

        Employee emp2 = new Employee() { Name = "Emp2", Department = department };
        IList<Employee> employees = new List<Employee>();
        employees.Add( emp1 );
        employees.Add( emp2 );

        var memoryStream = new MemoryStream();
        var formatter = new BinaryFormatter();
        formatter.Serialize( memoryStream, employees );

        memoryStream.Seek( 0, SeekOrigin.Begin );
        IList<Employee> deserialisedEmployees = formatter.Deserialize( memoryStream ) as IList<Employee>; //Works nicely

        JsonSerializerSettings jsonSS= new JsonSerializerSettings();
        jsonSS.TypeNameHandling = TypeNameHandling.Objects;
        jsonSS.TypeNameAssemblyFormat = FormatterAssemblyStyle.Full;
        jsonSS.Formatting = Formatting.Indented;
        jsonSS.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //This is not working!!
        //jsonSS.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; //This is also not working!!
        jsonSS.PreserveReferencesHandling = PreserveReferencesHandling.All;
        string jsonAll = JsonConvert.SerializeObject( employees, jsonSS ); //Throws stackoverflow exception

Edit1: The issue has been reported to Json (http://json.codeplex.com/workitem/23668)

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET