Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.NET serialization pre and post calls

I am using Json.NET to serialize an object graph. For each object that is serialized or deserialized, I want to call a method on that object before the serialization takes place. For e.g. all my objects implement an interface INotified with a method OnSerializing. I want OnSerializing to be called before the object is serialized.

Is there a way to achieve this without having to touch the Json.NET source code?

like image 420
Ali Kazmi Avatar asked Jun 26 '09 10:06

Ali Kazmi


People also ask

What is serialization 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).

What is serializing and Deserializing?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is Jsonconvert DeserializeObject?

DeserializeObject(String, Type,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, Type, JsonSerializerSettings) Deserializes the JSON to the specified .

How do you serialize an array in JSON?

To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for. Json.NET will serialize the collection and all of the values it contains.


1 Answers

The latest version of Json.NET supports serialization callbacks.

like image 58
James Newton-King Avatar answered Oct 03 '22 04:10

James Newton-King