RestSharp to WCF as JSON date formatting / serializing

无人久伴 提交于 2020-01-14 14:32:32

问题


I have a WCF Method that takes in a list of "Timestamps"

public bool SyncTimestamps(IList<Timestamp> timestamps)

For the life of me I cannot get the client to pass values to the host in a format that it is happy with using RestSharp. The problem appears to be with dateformatting.

Attempt 1

var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(timestamps);

Output 1

"[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"2013-08-05T13:51:13.6201529Z\",\"TimestampActual\":\"2013-08-05T13:51:13.6201529Z\",\"Type\":1,\"Active\":false}]"

Error 1

DateTime content '2013-08-05T13:51:13.6201529Z' does not start with '/Date(' and end with ')/' as required for JSON.'

I read that this was a problem with the RestSharp serializer so I replaced with Json.Net which produces a slightly different date string.

Attempt 2

var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(JsonConvert.SerializeObject(timestamps));    

Output 2

{application/json="[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"2013-08-05T14:54:33.9261815+01:00\",\"TimestampActual\":\"2013-08-05T14:54:33.9251814+01:00\",\"Type\":1,\"Active\":false}]"}

Error 2

The exception message is 'Expecting state 'Element'.. 
Encountered 'Text' with name '', namespace ''. '. See server logs for more details. 
The exception stack trace is: 

at ReadArrayOfTimestampFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString , CollectionDataContract ) 
at System.Runtime.Serialization.Json.JsonCollectionDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) at 

Can anyone suggest a way to produce a dateformat that WCF service will be happy to accept and deserialize? The docs on MSDN state that the following format is required.

DateTime values appear as JSON strings in the form of "/Date(700000+0500)/", where the first number (700000 in the example provided) is the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970.

UPDATE :

I found a settings as part of JSON.Net that allows me to transfer the date into the format that WCF appears to be askign for.

var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
JsonConvert.SerializeObject(obj, settings);

This appears to have done the job in creating the serialized string.

{application/json="[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"\\/Date(1375713542908+0100)\\/\",\"TimestampActual\":\"\\/Date(1375713542908+0100)\\/\",\"Type\":1,\"Active\":false}]"}

My service however still rejects this with "The exception message is 'Expecting state 'Element'.."


回答1:


Aside from JSON.Net, there's another JSON serializing library that will do this for you: https://www.nuget.org/packages/ServiceStack.Text/

var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
request.RequestFormat = DataFormat.Json;

var json = JsonSerializer.SerializeToString(timestamps);
request.AddParameter("application/json", json, ParameterType.RequestBody);



回答2:


You were almost there. You just have to add the parameter to the request manually, bypassing the faulty RestSharp serializer.

var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
request.RequestFormat = DataFormat.Json;
var settings = new JsonSerializerSettings() { DateFormatHandling=DateFormatHandling.MicrosoftDateFormat };
var json = JsonConvert.SerializeObject(timestamps,settings);

request.AddParameter("application/json",json,null,ParameterType.RequestBody);   


来源:https://stackoverflow.com/questions/18060440/restsharp-to-wcf-as-json-date-formatting-serializing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!