Recently I tried to get some custom REST Services for SharePoint working but had the Problem that my DateTime and Guid properties or parameters always gave me back empty values or even the error Bad Request.
Then I found out that the string representation of an empty Guid or even null value must be the Guid.Empty value '00000000-0000-0000-0000-000000000000' otherwise you might get a Bad Request or some strange behavior.
Even worse was the transfer of an empty date parameter or property.
I thought it will be good enough to have like this string representation for an empty date field "".
Or even most common schemas like "2010-5-5" and so would not work and always give back a Bad Request message.
Then after trying to post back a date value from my WCF REST Service to the client I found out that the expected format looks something like this: "\/Date(946645200000+1100)\/"
After some research I now know that this is due to how .NET converts the date objects from and to JSON.
Example:
DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(DateTime));
DateTime dateTime = new DateTime(2000, 1, 1);
dcjs.WriteObject(Response.OutputStream, dateTime);
So the DataContractJsonSerializer class is responsible for converting the DateTime values in the unexpected format shown above.
Now I got my lesson learned how to deal with this kind of data types with WCF services and JSON.
Then I found out that the string representation of an empty Guid or even null value must be the Guid.Empty value '00000000-0000-0000-0000-000000000000' otherwise you might get a Bad Request or some strange behavior.
Even worse was the transfer of an empty date parameter or property.
I thought it will be good enough to have like this string representation for an empty date field "".
Or even most common schemas like "2010-5-5" and so would not work and always give back a Bad Request message.
Then after trying to post back a date value from my WCF REST Service to the client I found out that the expected format looks something like this: "\/Date(946645200000+1100)\/"
After some research I now know that this is due to how .NET converts the date objects from and to JSON.
Example:
DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(DateTime));
DateTime dateTime = new DateTime(2000, 1, 1);
dcjs.WriteObject(Response.OutputStream, dateTime);
So the DataContractJsonSerializer class is responsible for converting the DateTime values in the unexpected format shown above.
Now I got my lesson learned how to deal with this kind of data types with WCF services and JSON.
Comments
Post a Comment