1

I'm having a class with only private fields and their public getter-setters. I need to convert class object into JSON String hence I'm using JSON.Net.

Following is a simple snippet to convert class object into a JSON string.

MyClass obj = new MyClass();
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);

But the method SerializeObject throws StackOverflowException at field in MyClass of type DateTime. What's happening here?

Update

Following is how MyClass looks like (as it is, I don't mind sharing the actual class)

    class MyClass
    {
        private int _Model;
        public int Model
        {
            get
            {
                return _Model;
            }
            set
            {
                _Model = value;
            }
        }

        private long _ProductionControlNumber;
        public long ProductionControlNumber
        {
            get
            {
                return _ProductionControlNumber;
            }
            set
            {
                _ProductionControlNumber = value;
            }
        }

        private DateTime _ProductionDate;
        public DateTime ProductionDate
        {
            get
            {
                return _ProductionDate;
            }
            set
            {
                _ProductionDate = value;
            }
        }

        private DateTime _TestDate;
        public DateTime TestDate
        {
            get
            {
                return _TestDate;
            }
            set
            {
                _TestDate = value;
            }
        }

        private DateTime _TestStartTime;
        public DateTime TestStartTime
        {
            get
            {
                return _TestStartTime;
            }
            set
            {
                _TestStartTime = value;
            }
        }

        private TimeSpan _TestDuration;
        public TimeSpan TestDuration
        {
            get
            {
                return _TestDuration;
            }
            set
            {
                _TestDuration = value;
            }
        }

        public DateTime TestEndTime
        {
            get
            {
                //TODO Perform start end time computing logic.
                return TestEndTime;
            }
        }

        private int _TestBed;
        public int TestBed
        {
            get
            {
                return _TestBed;
            }
            set
            {
                _TestBed = value;
            }
        }

        private long _EngineSerial;
        public long EngineSerial
        {
            get
            {
                return _EngineSerial;
            }
            set
            {
                _EngineSerial = value;
            }
        }

        private Single _FuelSpecificGravity;
        public Single FuelSpecificGravity
        {
            get
            {
                return _FuelSpecificGravity;
            }
            set
            {
                _FuelSpecificGravity = value;
            }
        }

        private long _FuelConsume100;
        public long FuelConsume100
        {
            get
            {
                return _FuelConsume100;
            }
            set
            {
                _FuelConsume100 = value;
            }
        }

        private long _FuelConsume110;
        public long FuelConsume110
        {
            get
            {
                return _FuelConsume100;
            }
            set
            {
                _FuelConsume100 = value;
            }
        }

        private int _TemporaryRPM;
        public int TemporaryRPM
        {
            get
            {
                return _TemporaryRPM;
            }
            set
            {
                _TemporaryRPM = value;
            }
        }

        private int _PermanentRPM;
        public int PermanentRPM
        {
            get
            {
                return _PermanentRPM;
            }
            set
            {
                _PermanentRPM = value;
            }
        }

        private Single _RatedPower;
        public Single RatedPower
        {
            get
            {
                return _RatedPower;
            }
            set
            {
                _RatedPower = value;
            }
        }

        private int _RatedSpeed;
        public int RatedSpeed
        {
            get
            {
                return _RatedSpeed;
            }
            set
            {
                _RatedSpeed = value;
            }
        }

        private double _PulleyDiameter;
        public double PulleyDiameter
        {
            get
            {
                return _PulleyDiameter;
            }
            set
            {
                _PulleyDiameter = value;
            }
        }

        private double _RopeDiameter;
        public double RopeDiameter
        {
            get
            {
                return _RopeDiameter;
            }
            set
            {
                _RopeDiameter = value;
            }
        }

        private Single _FullLoad;
        public Single FullLoad
        {
            get
            {
                return _FullLoad;
            }
            set
            {
                _FullLoad = value;
            }
        }
    }

Also, I'll have another class which will have MyClass type field (along with its own similar set of fields), which is going to be converted into JSON string too, and that shouldn't be a problem since JSON.Net is said to support that situation too.

Note: I'm new to C# but I've previously worked with JSON in Java, where I get to play with JSONObject and JSONArray, and they were pretty straight forward.

1
  • 1
    Could you also show us MyClass? Commented Feb 10, 2013 at 11:37

1 Answer 1

3

It looks like your TestEndTime property's getter references itself. Therefore when Json.NET tries to serialize it, it recursively accesses itself and causes the StackOverflowException.

Hope that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot!! that was exactly the problem, I've created private field for TestEndTime and similar getter-setter for it and works fine now.
I think it is better to use auto-properties in C# as of 3.0 if there is no specific reason for a private variable. This type of mistypings will not occur with that method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.