Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content '\/Date('')\/' does not start with '\/Date(' and end with ')\/' as required for JSON

I want to send a post request to a WCF rest service as you can see:

Guid id;
id = Guid.NewGuid();

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:47026/NewsRepository.svc/AddNews");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = new JavaScriptSerializer().Serialize(new
    {       
        id = id,
        Subject="wfwf",
        ViewerCounter="1",
        Content="fsdsd",
        SubmitDatatime="2012/12/12",
        ModifiedDateTime="2012/12/12",
        PublisherName="sdaadasd",
        PictureAddress="adfafsd",
        TypeOfNews="adsadaad"                       
    });

    streamWriter.Write(json);
}
try 
{ 
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
}
catch(Exception er)
{
    MessageBox.Show(er.ToString());
}

But I get an error - 400 bad request. So I traced my WCF log file and I found this error:

There was an error deserializing the object of type CMSManagement.Domain.Entity.News. DateTime content '2012/12/12' does not start with '/Date(' and end with ')/' as required for JSON.

After my dear friend @svek code. The result is like this:

Guid id;
id = Guid.NewGuid();

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:47026/NewsRepository.svc/AddNews");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "PUT";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
    {
        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
    };

    string json = JsonConvert.SerializeObject(new
    {
        id = id,
        Subject = "wfwf",
        ViewerCounter = "1",
        Content = "fsdsd",
        SubmitDatatime = "2012/12/12",
        ModifiedDateTime = "2012/12/12",
        PublisherName = "sdaadasd",
        PictureAddress = "adfafsd",
        TypeOfNews = "adsadaad"
    }, microsoftDateFormatSettings);
    streamWriter.Write(json);
}
try 
{ 
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
}
catch(Exception er)
{
    MessageBox.Show(er.ToString());
}

But I get the same error. Why?

like image 443
Ehsan Akbar Avatar asked May 28 '17 13:05

Ehsan Akbar


1 Answers

Use JsonConvert

Instead of using the

string json = new JavaScriptSerializer().Serialize( new {...} );

use

//using Newtonsoft.Json;
string json = JsonConvert.SerializeObject(new {...} );

Setting up the DateTime format

Prior to Json.NET 4.5 dates were written using the Microsoft format: "/Date(1198908717056)/". If you want to use this format, or you want to maintain compatibility with Microsoft JSON serializers or older versions of Json.NET, then change the DateFormatHandling setting to MicrosoftDateFormat.

Source: http://www.newtonsoft.com/json/help/html/DatesInJSON.htm

Default Json.NET 4.5 Format

// default as of Json.NET 4.5
string isoJson = JsonConvert.SerializeObject(data);
// { "MyDateProperty":"2009-02-15T00:00:00Z" }

Microsoft Date Format

JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};

string microsoftJson = JsonConvert.SerializeObject(data, microsoftDateFormatSettings);
// { "MyDateProperty":"\/Date(1234656000000)\/" }

JavaScript JSON Format

string javascriptJson = JsonConvert.SerializeObject(data, 
    new JavaScriptDateTimeConverter());
// { "MyDateProperty":new Date(1234656000000)}

Solution Code

Here is the full working solution for your question:

JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};

string json = JsonConvert.SerializeObject(new
{
    id = id,
    Subject = "wfwf",
    ViewerCounter = "1",
    Content = "fsdsd",
    SubmitDatatime = "2012/12/12",
    ModifiedDateTime = "2012/12/12",
    PublisherName = "sdaadasd",
    PictureAddress = "adfafsd",
    TypeOfNews = "adsadaad"

}, microsoftDateFormatSettings); // ⇦ Added the format argument here

using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(streamWriter, json);
}
like image 164
Svek Avatar answered Oct 19 '22 12:10

Svek