Change serializer for WebMethod from JsonDataContractSerializer to NewtonSoft Json Serializer - asp.net

If I return an object containing a DateTime property from a web method, which would be methods prefix with the data annotation
[WebMethod]
that date will be serialized to something like
"/Date(1365776562553)/"
instead of a string in iso-8601 format like
"2013-04-12T19:59:53.2422758+05:30"
If I could change the serializer for web methods from JsonDataContractSerializer to NewtonSoft Json Serializer, the dates would be fixed.
How?
I will not serialize manually or writing javascripts that consumes date strings in the first format.
The googling I have done this far only directs me to pages where any of those workarounds are suggested.

Related

Elasticsearch NEST client datetime deserialization

I'm trying to deserialize my NEST document to datetime, but everything ends up being a string because I'm using System.Object:
var response = this.client.Search<object>(search);
The reason for this is because I'm searching multiple indices and I have to use object type. Since my whole application is returning DateFormatHandling.MicrosoftDateFormat.
I'd like NEST to deserialize datetime to DateTime type, so that my serializer can correctly serialize to MicrosoftDateFormat on API.

Change default GUID serialization format

By default, the JSON and XML serializers in Web API 2 just call ToString() to serialize out a Guid, which means it includes dashes: fd190000-5532-8477-e053-9a16ce0af828. I'd really like to change the default serialization to the format returned by ToString("N"), which does not include dashes: fd19000055328477e0539a16ce0af828.
I found this article on creating a JsonConverter and overriding the WriteJson() method to use ToString("N"). That worked fine for JSON, but I haven't found anything similar for XML serialization.
Is there a way to implement this only once, regardless of how many MediaTypeFormatters are present? If not, how can I override XML serialization as well?

Saving a toISODate gets deserialized as a non-iso string

I follow the usual recommendations of serializing javascript Date objects to be sent to the server by using the toISODate js function.
In fact Breeze.js is doing this for me which is great. Everything works as expected when the property on my Entity is a DateTime - the date gets saved to the database as a UTC (0 offset date) datetime.
I hit a problem when the property on the Entity is of type string. A date that is sent over the wire as '2013-06-08T23:00:00Z' is being deserialized into the string property on the Entity as '06/08/2013 23:00:00' and this is the same value that is saved into the varchar backing column in the database.
So the date is being deserialized into a 'en-US' formatted date (MM/dd/yyyy HH:mm:ss). I'm stuck as to why this is happening or how to change things so that the string remains intact as it's deserialized into a string property.
A few technical notes:
I confirmed the deserialized value in the property by wiring up a BeforeSaveEntitiesDelegate to the EFContextProvider and inspected the Entity instance in the debugger just before it was saved
when inspecting the entity in the BeforeSaveEntitiesDelegate method on the server, I noted that the Thread.CurrentThread.CurrentCulture and CurrentUICulture were both 'en-GB'
for technical reasons I need to use a string property rather than a DateTime (or DateTimeOffset) - basically the property could receive any type of data so string is the universal format that will fit everything.
Help would be most welcome!
Thanks
Christian Crowhurst
For a .NET server, Breeze uses JSON.net to serialize/deserialize json. Breeze allows you to configure this by automatically detecting any 'custom' implementations of the 'BreezeConfig' class.
This means that you can customize Breeze's use of JSON.Net's serialization behavior by implementing a subclass of BreezeConfig. This might look something like this within your server project.
using Breeze.ContextProvider;
using Breeze.WebApi2;
using Newtonsoft.Json;
namespace Sample_WebApi2 {
public class CustomBreezeConfig : BreezeConfig {
/// <summary>
/// Override to use a specialized JsonSerializer implementation.
/// </summary>
protected override JsonSerializerSettings CreateJsonSerializerSettings() {
// get the breeze default settings.
var baseSettings = base.CreateJsonSerializerSettings();
// Not sure if this is the setting you want but...
baseSettings.DateParseHandling = DateParseHandling.None;
return baseSettings;
}
}

whats the difference between responseBody and Serialization

In Spring Framework,i am using responseBody annotation and serialization,i learned that responseBody is for HttpMessageConverts,it will return the output to view Resolver and serialization will convert the data in to byte stream and transfer it using version Id and header,here i have a question that, whats the difference between this two ?
Serialization is a computer science concept that describes how a data structure can be broken down and stored. Deserialization is the reverse, taking a stored format and converting it back into a data structure.
#ResponseBody is an annotation that Spring MVC uses on #RequestMapping methods. It tells the DispatcherServlet to take the return value of your handler method and, using an HttpMessageConverter, serialize it and write it directly to the HTTP response OutputStream.
See the javadoc of HttpMessageConverter for a list of implementation classes. You can write byte[], String, InputStream, Resource objects directly to the stream. There also exist HttpMessageConverter classes for converting any object returned by the handler method to JSON or XML.

ASP.NET json serializer only includes __type for top level class

I'm using [webmethod] attribute in an aspx page to expose a complex data structure(autogenerated from xml->xsd->xsd.exe) to the browser.
After I get the data on the client with jquery, only the top level object includes the "__type" attribute, so I cannot determine the class of any of the nested objects(attributes, array content etc).
Is there a way to make the asp.net serializer include the __type attribute recursively when serializing an object?
I encountered a similar situation with another JSON serializer, the one from ServiceStack.Text. They will put in the __type property if an object is declared as an interface or an abstract class, perhaps the vanilla .NET serializer will do so as well? If that's the case, is there any way for you to declare your object[] array as an array of interfaces? Even something as trivial as cerating an IEmpty with no methods will do the trick.

Resources