Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     public class JsonFlatAttribute : Attribute
  10.     {
  11.     }
  12.  
  13.     public class FlatJsonConverter : JsonConverter
  14.     {
  15.         public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  16.         {
  17.             writer.WriteStartObject();
  18.             WriteObject(writer, value, serializer);
  19.             writer.WriteEndObject();
  20.         }
  21.  
  22.         private void WriteObject(JsonWriter writer, object value, JsonSerializer serializer)
  23.         {
  24.             var t = JToken.FromObject(value);
  25.             var flatNames = new List<string>();
  26.             if (t.Type == JTokenType.Object)
  27.             {
  28.                 var type = value.GetType();
  29.                 foreach (var propertyInfo in type.GetProperties())
  30.                 {
  31.                     var flatAttr = propertyInfo.GetCustomAttribute(typeof(JsonFlatAttribute));
  32.                     if (flatAttr != null) flatNames.Add(propertyInfo.Name.ToLower());
  33.                 }
  34.  
  35.                 foreach (var token in t.Children())
  36.                 {
  37.                     if (token.Type == JTokenType.Property && flatNames.Contains(((JProperty) token).Name.ToLower()))
  38.                     {
  39.                         foreach (var child in token.Children())
  40.                         {
  41.                             WriteObject(writer, child.Value<object>(), serializer);
  42.                         }
  43.                     }
  44.                     else
  45.                     {
  46.                         WriteObject(writer, token.Value<object>(), serializer);
  47.                     }
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 t.WriteTo(writer);
  53.             }
  54.         }
  55.  
  56.         public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  57.         {
  58.             var jObject = JObject.Load(reader);
  59.             var value = objectType.GetConstructor(new Type[] { }).Invoke(new object[] { });
  60.  
  61.             return ReadObject(objectType, jObject, value);
  62.         }
  63.  
  64.         private object ReadObject(Type objectType, JObject jObject, object value)
  65.         {
  66.             var flatNames = new List<string>();
  67.             foreach (var propertyInfo in objectType.GetProperties())
  68.             {
  69.                 var flatAttr = propertyInfo.GetCustomAttribute(typeof(JsonFlatAttribute));
  70.                 if (flatAttr != null)
  71.                 {
  72.                     var obj = propertyInfo.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
  73.                     foreach (var property in obj.GetType().GetProperties())
  74.                     {
  75.                         var method = typeof(JToken).GetMethod("Value").MakeGenericMethod(property.PropertyType);
  76.                         var val = method.Invoke(jObject, new object[] { property.Name.ToLower() });
  77.                         property.SetValue(obj, val);
  78.                     }
  79.  
  80.                     propertyInfo.SetValue(value, obj);
  81.                 }
  82.                 else
  83.                 {
  84.                     foreach (var property in value.GetType().GetProperties())
  85.                     {
  86.                         var method = typeof(JToken).GetMethod("Value").MakeGenericMethod(property.PropertyType);
  87.                         var val = method.Invoke(jObject, new object[] { property.Name.ToLower() });
  88.                         property.SetValue(value, val);
  89.                     }
  90.                 }
  91.             }
  92.  
  93.             return value;
  94.         }
  95.  
  96.         public override bool CanConvert(Type objectType)
  97.         {
  98.             // TODO.
  99.             return true;
  100.         }
  101.  
  102.         // TODO.
  103.         public override bool CanRead => true;
  104.         public override bool CanWrite => true;
  105.     }
  106.  
  107.     public class Foo
  108.     {
  109.         public int Y { get; set; }
  110.         public int Z { get; set; }
  111.     }
  112.    
  113.     public class Bar
  114.     {
  115.         public string X { get; set; }
  116.         public Foo Foo { get; set; }
  117.     }
  118.  
  119.     class Program
  120.     {
  121.         static void Main(string[] args)
  122.         {
  123.             var jsonSettings = new JsonSerializerSettings
  124.             {
  125.                 Converters = new List<JsonConverter> { new FlatJsonConverter() }
  126.             };
  127.  
  128.             var obj = JsonConvert.DeserializeObject<Bar>("{\"x\": 1, \"foo\": {\"y\": 2, \"z\": 3}}",
  129.                 new JsonSerializerSettings { Converters = new List<JsonConverter> { new FlatJsonConverter() } });
  130.             Console.WriteLine(JsonConvert.SerializeObject(obj));
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement