Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
546
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Serialization;
  6.  
  7. namespace TestProject
  8. {
  9.  
  10.     class TestConverter : JsonConverter
  11.     {
  12.         public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  13.         {
  14.             var e = value as BaseClass;
  15.             writer.WriteValue(e.Id);
  16.         }
  17.  
  18.         public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  19.         {
  20.             // this method never gets called when using custom contract resolver instead of json converter attributes
  21.             // omitted for brevity
  22.             return null;
  23.         }
  24.  
  25.         public override bool CanConvert(Type objectType)
  26.         {
  27.             return typeof(BaseClass).IsAssignableFrom(objectType);
  28.         }
  29.  
  30.     }
  31.  
  32.     class TestContractResolver : DefaultContractResolver
  33.     {
  34.         protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
  35.         {
  36.             var property = base.CreateProperty(member, memberSerialization);
  37.             if (typeof(BaseClass).IsAssignableFrom(property.PropertyType))
  38.             {
  39.                 property.Converter = new TestConverter();
  40.             }
  41.             return property;
  42.         }
  43.  
  44.     }
  45.  
  46.     abstract class BaseClass
  47.     {
  48.         public int Id { get { return 0; } }
  49.  
  50.     }
  51.  
  52.     class Song : BaseClass
  53.     {
  54.         public string Name { get; set; }
  55.  
  56.         // when this converter attribute is commented out -> exception
  57.         [JsonConverter(typeof(TestConverter))]
  58.         public Artist Artist { get; set; }
  59.  
  60.     }
  61.  
  62.     class Artist : BaseClass
  63.     {
  64.         public string Name { get; set; }
  65.     }
  66.     class Program
  67.     {
  68.         static void Main(string[] args)
  69.         {
  70.             var s = new Song();
  71.             var a = new Artist();
  72.             s.Name = "test 123";
  73.             a.Name = "some singer";
  74.             s.Artist = a;
  75.  
  76.             var serializer = new JsonSerializer();
  77.             serializer.ContractResolver = new TestContractResolver();
  78.  
  79.             // serialize value with a reference, works correctly
  80.             var writer = new StringWriter();
  81.             serializer.Serialize(writer, s);
  82.             var json = writer.ToString();
  83.             Console.WriteLine("serialized json: \n" + json);
  84.  
  85.             // deserialize the value. Custom converter is not used.
  86.             var reader = new JsonTextReader(new StringReader(json));
  87.             var loaded = serializer.Deserialize<Song>(reader);
  88.  
  89.             // dump the restored object
  90.             Console.WriteLine("\nrestored object:\n" + JsonConvert.SerializeObject(loaded, Formatting.Indented));
  91.            
  92.             Console.ReadLine();
  93.  
  94.         }
  95.     }
  96. }
Advertisement
RAW Paste Data Copied
Advertisement