简体   繁体   English

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

[英]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. 我需要将类对象转换为JSON String,因此我使用的是JSON.Net。

Following is a simple snippet to convert class object into a JSON string. 以下是将类对象转换为JSON字符串的简单代码段。

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 . 但是SerializeObject方法在类型为DateTime MyClass的字段处抛出StackOverflowException What's happening here? 这里发生了什么事?

Update 更新

Following is how MyClass looks like (as it is, I don't mind sharing the actual class) 以下是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;
            }
        }
    }

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. 另外,我将有另一个类,它将具有MyClass类型字段(以及它自己的类似字段集),它也将被转换为JSON字符串,这应该不是问题,因为JSON.Net被说也支持这种情况。

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. 注意:我是C#的新手,但我以前在Java中使用JSON,在那里我可以使用JSONObjectJSONArray ,而且它们非常简单。

It looks like your TestEndTime property's getter references itself. 看起来你的TestEndTime属性的getter引用了它自己。 Therefore when Json.NET tries to serialize it, it recursively accesses itself and causes the StackOverflowException. 因此,当Json.NET尝试序列化它时,它会递归访问自身并导致StackOverflowException。

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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