Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize object to json with type info using Newtonsoft.Json?

I want to have a property with type name in JSON when I serialize objects of certain types. I wrote a converter:

public class TypeInfoConverter : JsonConverter {     private readonly IEnumerable<Type> _types;      public TypeInfoConverter(IEnumerable<Type> types) {         Contract.Requires(types != null);          _types = types;     }      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {         var jObject = JObject.FromObject(value, serializer);         jObject.AddFirst(new JProperty("Type", value.GetType().Name));         jObject.WriteTo(writer);     }      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {         return serializer.Deserialize(reader, objectType);     }      public override bool CanConvert(Type objectType) {         return _types.Any(t => t.IsAssignableFrom(objectType));     } } 

But when I'm trying to serialize object I have an infinity recursion here: var jObject = JObject.FromObject(value, serializer); It is obvious because I use the same instance of JsonSerializer which was configured with that converter.

How to prevent using this converter, but I want to use other converters which configured for this serializer?

Types which I want to serialize:

public interface ITaskResult { }  public class UserHasRejectedOffer : ITaskResult {     public string Message { get; set; } }  public class UserHasFilledForm : ITaskResult {     public string FormValue1 { get; set; }      public string Formvalue2 { get; set; } }  ... 
like image 300
Exta Avatar asked Oct 14 '13 12:10

Exta


People also ask

What is the use of JsonConvert SerializeObject?

SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings.

What is Jsonproperty annotation C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

What is serializing and deserializing 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).


2 Answers

var jsonSerializerSettings = new JsonSerializerSettings() {      TypeNameHandling = TypeNameHandling.All }; var json = JsonConvert.SerializeObject(instance, jsonSerializerSettings); 

http://james.newtonking.com/json/help/index.html?topic=html/SerializationSettings.htm

like image 109
CalvinDale Avatar answered Sep 28 '22 23:09

CalvinDale


public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)      var converters = serializer.Converters.Where(x => !(x is TypeInfoConverter)).ToArray();      var jObject = JObject.FromObject(value);     jObject.AddFirst(new JProperty("Type", value.GetType().Name));     jObject.WriteTo(writer, converters); } 
like image 43
Exta Avatar answered Sep 28 '22 23:09

Exta