Jean
Jean

Reputation: 1490

How to resolve SerializationException when complex objects contain non-nullable DateTime members?

I am converting some objects to JSON using WCF. Sometimes, there is an error:

SerializationException: DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON.

According to this post, I can resolve it by setting objects of type DateTime this way dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc). This resolves well enough when my class isn't compound, example:

public class MyObj : BaseObj
{
    [XmlElement(Order = 0, ElementName = "name")]
    public string Name { get; set; }
    [XmlElement(Order = 1, ElementName = "xDate")]
    public DateTime? XDate { get; set; }
    [XmlElement(Order = 2, ElementName = "yDate")]
    public DateTime YDate { get; set; }

}

[Serializable]
public class BaseObj
{
    [XmlElement(Order = 0,ElementName = "pDate")]
    public DateTime? PDate { get; set; }
    [XmlElement(Order = 1, ElementName = "qDate")]
    public DateTime QDate { get; set; }
}

Edit:

I don't actually create the instance of MyObj. I'm passed the object and create a wrapper around it

/*..some factory creates the instance `mo` and returns it...*/
MyObjWrapper wrapped = new MyObjWrapper() {MyObject = mo};

public class MyObjWrapper{
    [XmlIgnore]
    private MyObj _myObject;

    [XmlElement(Order = 0, ElementName = "Arg1")]
    public MyObj MyObject {
        get{ return _myobject; }
        set{
            value.GetType().GetProperties().ToList().ForEach(
                m => { if (m.PropertyType.Equals(typeof(DateTime))) m.SetValue(value, DateTime.SpecifyKind((DateTime)m.GetValue(value), DateTimeKind.Utc)); });
            _myobject = value;
        }
    }
}

How would I do so when the class is more complex (ie it has members which are user defined classes containing non-nullable date times as well)? I cannot set the members to be of the nullable type DateTime?. Example:

public class MyObj : BaseObj
{
    [XmlElement(Order = 0, ElementName = "name")]
    public string Name { get; set; }
    [XmlElement(Order = 1, ElementName = "xDate")]
    public DateTime? XDate { get; set; }
    [XmlElement(Order = 2, ElementName = "yDate")]
    public DateTime YDate { get; set; }
    [XmlElement(Order = 3, ElementName = "kaBoom")]
    public AnotherObj KaBoom {get; set;} /*This contains a non-nullable DateTime member which causes the exception */
}

[Serializable]
public class AnotherObj
{
    [XmlElement(Order = 0,ElementName = "objID")]
    public int ObjID {get; set;}
    [XmlElement(Order = 1,ElementName = "anotherDate")]
    public DateTime AnotherDate {get; set;} 
}

Upvotes: 1

Views: 499

Answers (1)

Mohammad
Mohammad

Reputation: 2764

you can use this workaround, In your json object definition

[IgnoreDataMember]
public DateTime dateObject;

public string dateCustomSerialize
{
 get {
//Custom get
}
set {
//Custom set
}
}

Upvotes: 0

Related Questions