简体   繁体   中英

JsonConvert.SerializeObject() fails in serializing class with DateTime fields

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM