Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make JSON.NET and Serializable attribute work together

Tags:

I'm using JSON.NET and had some troubles in the past during WEBAPI objects deserialization. After doing some research I've found that the class was marked with [Serializable]. When I removed this the deserialization was just fine.

More detailed information about this can be found here:

Why won't Web API deserialize this but JSON.Net will?

Now it comes to the problem that I use binaryformatter to create a hash value calculated from this object class. But Binaryformatter requires that the class must be marked as [Serializable].

Could you recommend me any approach to make both things work at the same time?

like image 751
Javier Avatar asked Jan 03 '14 22:01

Javier


People also ask

Is JSON used for serialization?

Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.

Why do we need serialization and deserialization in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

Why does JSON need to be serialized?

The purpose of serializing it into JSON is so that the message will be a format that can be understood and from there, deserialize it into an object type that makes sense for the consumer.

What is a JSON serialization exception?

JsonSerializationException(String, Exception) Initializes a new instance of the JsonSerializationException class with a specified error message and a reference to the inner exception that is the cause of this exception.


2 Answers

Found the solution:

First, check that your Newtonsoft.JSON version is greater than 4.5 or just update with NuGET

According to the version notes, both can work together starting from this version using some extra annotations.

http://james.newtonking.com/archive/2012/04/11/json-net-4-5-release-2-serializable-support-and-bug-fixes

"Now if you are serializing types that have the attribute and don’t want the new behaviour, it can either be overridden on a type using the JsonObjectAttribute"

[JsonObject]
[Serializable]
public class Foobar {

Now it is possible to use JSON.NET and, in my case, the binaryformatter with the [Serializable] attribute.

like image 125
Javier Avatar answered Oct 26 '22 12:10

Javier


An alternative to specifying JsonObject on each class is to tell web.api to ignore Serialize attributes globally. This can be done by resetting the DefaultContractResolver on the web api JsonFormatter:

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();

(using NewtonSoft.Json.Serialization where config is the System.Web.Http.HttpConfiguration)

As of NewtonSoft v4.5 the IgnoreSerializableAttribute property on the DefaultContractResolver is set to true but the web api wrapper, around DefaultContractResolver, has this set to false by default.

like image 25
Mark Jerzykowski Avatar answered Oct 26 '22 10:10

Mark Jerzykowski