Search code examples
c#jsonserialization

Serializing nested objects


Below is an example that illustrates the problem I am struggling with. I have no idea how to serialize the presented structure. please help.

File /*CConnector.cs*/

namespace serjalizacja
{
    public class CConnector
    {
        private Ellipse _circle;
        private string _name;
        private CElement _parent;
        public Ellipse Circle
        {
            get { return _circle; }
            set { _circle = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public CElement Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }
    
        public CConnector()
        {
            _circle = new Ellipse();
            _name = "1";
        }

        public CConnector(string name, CElement parent)
        {
            _circle = new Ellipse();
            _name = name;
            _parent = parent;
        }
    }
}

File /*CElement.cs*/

namespace serjalizacja 
{ 
    public class CElement 
    { 
        private CSheet _parent; 
        private string _name; 
        private List<CConnector> _connectors;    
        public CSheet Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    
        public List<CConnector> Connectors
        {
            get { return _connectors; }
            set { _connectors = value; }
        }
    
        public CElement()
        {
            _connectors = new List<CConnector>();
            _connectors.Add(new CConnector("1", this));
            _connectors.Add(new CConnector("2", this));
        }
    
        public CElement(string name, CSheet parent)
        {
            _connectors = new List<CConnector>();
            _name = name;
            _parent = parent;
            _connectors.Add(new CConnector("1", this));
            _connectors.Add(new CConnector("2", this));
        }
    }
}

File /*CSheet.cs*/

namespace serjalizacja 
{ 
    public class CSheet 
    { 
        private List<CElement> _childrens; 
        public List<CElement> Childrens 
        { 
            get {  return _childrens; } 
            set { _childrens = value; }
        }

        public CSheet()
        {
            _childrens = new List<CElement>();
        }
    
        public void Add(CElement child)
        {
            _childrens.Add(child);
        }
    }
}

File /*MainWindow.xaml.cs*/

namespace serjalizacja 
{ 
    public partial class MainWindow : Window 
    { 
        CSheet sheet; 
        public MainWindow() 
        { 
            InitializeComponent(); 
            sheet = new CSheet(); 
            sheet.Add(new CElement("Start", sheet)); 
            sheet.Add(new CElement("Stop", sheet)); 
        } 
        private void Serializacja_Click(object sender, RoutedEventArgs e)
        {
            //JsonSerializerOptions options = new JsonSerializerOptions()
            //{
            //    ReferenceHandler = ReferenceHandler.IgnoreCycles,
            //    NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
            //    WriteIndented = true
            //};
            string sheetstring = JsonSerializer.Serialize(sheet/*, options*/);
        }
    
        private void Deserialize_Click(object sender, RoutedEventArgs e)
        {
    
        }
    }
}

I tried to add the serialization option as shown in the commented part of the code, but unfortunately it doesn't work. If anyone can provide information that will lead me to a solution, I will be eternally grateful. :)


Solution

  • Try using [JsonIgnore] attribute on properties that will cause cycles, such as Parent.

    Take a look at this link: How to ignore properties with System.Text.Json.

    Also, you can make you code a lot simpler by using auto properties.