LeoFraietta
LeoFraietta

Reputation: 300

How to get a nullable DateTime serialized on WCF service

I have a WCF service with a nullable datetime member in DataContract as showed below. Because of business rules this datamember can't have the EmitDefaultValue setted to true and the type must be a "DateTime?" .

[DataContract(Name = "DADOS")]
public class Dados 
{
    [DataMember(EmitDefaultValue = false, Name = "NASCIMENTO")]
    public DateTime? DtNascimento = null;
}

My service contract is specified like below, see that I have to have two versions of Webinvoke method to keep different systems interoperability (Json and XML responses):

[ServiceContract]
public interface IRestService
{
    [OperationContract(Name = "ConsultaDadosXml")]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "ConsultaDados/xml?token={token}")]
    Dados ConsultaDadosXml(string token);

    [OperationContract(Name = "ConsultaDadosJson")]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "ConsultaDados/json?token={token}")]
    Dados ConsultaDadosJson(string token);
}

The problem is, when the DtNascimento value comes with a good value from the database, everything works fine. When this value is really null on the database, the XML/JSON response comes without the NASCIMENTO tag, is this happening because we have EmitDefaultValue = false. I can set my database to send me a empty value when this occurs, but my serialized object comes with a MinDate value on the responses.

Xml version:

<DADOS>
    <NASCIMENTO>1900-01-01T00:00:00</NASCIMENTO>
</DADOS>

Json version:

{
    "NASCIMENTO": "/Date(-2208981600000-0200)/",
}

What I really need is an empty variable shown on the answers when this value is null because there are other systems plugged on the web service trying to interpret those values, so the best solution would be keep empty variables:

Xml version:

<DADOS>
    <NASCIMENTO></NASCIMENTO>
</DADOS>

Json version:

{
    "NASCIMENTO": "",
}

Any suggestion will be appreciated.

Tks

Léo

Upvotes: 1

Views: 3988

Answers (1)

dbc
dbc

Reputation: 116795

You could create a proxy string-valued private property that returns an empty string for a null DateTime? value, and serialize that. It produces an empty element in the following format:

<NASCIMENTO></NASCIMENTO>

which is defined by the XML standard to have the same meaning as <NASCIMENTO />.

[DataContract(Name = "DADOS")]
public class Dados
{
    [IgnoreDataMember]
    public DateTime? DtNascimento { get; set; }

    [DataMember(EmitDefaultValue = false, Name = "NASCIMENTO")]
    string DtNascimentoString
    {
        get
        {
            if (DtNascimento == null)
                return string.Empty;
            return XmlConvert.ToString(DtNascimento.Value, XmlDateTimeSerializationMode.RoundtripKind);
        }
        set
        {
            if (string.IsNullOrEmpty(value))
                DtNascimento = null;
            else
                DtNascimento = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.RoundtripKind);
        }
    }
}

Upvotes: 1

Related Questions