JsonConvert.SerializeObject()在使用DateTime字段序列化类时失败

时间:2013-02-10 11:33:28

标签: c# .net c#-4.0 json.net

我正在上课只有私人领域和他们的公共吸气者。我需要将类对象转换为JSON String,因此我使用的是JSON.Net。

以下是将类对象转换为JSON字符串的简单代码段。

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

但方法SerializeObject会在StackOverflowExceptionMyClass的字段中抛出DateTime。这里发生了什么?

更新

以下是MyClass的样子(因为它,我不介意分享实际的课程)

    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;
            }
        }
    }

另外,我将有另一个类,它将具有MyClass类型字段(以及它自己的类似字段集),它也将被转换为JSON字符串,而且不应该是因为据说JSON.Net也支持这种情况。

注意:我是C#的新手,但我以前曾在Java中使用JSON,在那里我可以使用JSONObjectJSONArray进行游戏,他们是很直接。

1 个答案:

答案 0 :(得分:3)

看起来你的TestEndTime属性的getter引用了它自己。因此,当Json.NET尝试序列化它时,它会递归访问自身并导致StackOverflowException。

希望有所帮助!