[Fork] [Fork] [Fork] JsonConvert.DeserializeObject w/ DynamicObject and TypeCreationConverter

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Dynamic;
using System.Runtime.CompilerServices;
using Microsoft.CSharp.RuntimeBinder;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

// https://stackoverflow.com/questions/49916136/jsonconvert-deserializeobject-w-dynamicobject-and-typecreationconverter
					
	public class Program
	{
		public static void Main()
		{
			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}

	public class TestClass
	{
		public static void Test()
		{
			const string JSON = @"{'Name': 'my first story', 'ToldByUserId': 255, 'EntityName' : 'My Entity Name', 'NestedObject' : {'Foo' : 'Bar' }, 'ExplicitProperty' : 'Explicit Property' }";
			
			var settings = new JsonSerializerSettings
			{
				Converters = { new EntityBaseConverter() },
			};
			
			Test<EntityBase>(JSON, settings);
			Test<Story>(JSON, settings);
		}
		
		static void Test<T>(string JSON, JsonSerializerSettings settings) where T : EntityBase
		{
			dynamic stroy = JsonConvert.DeserializeObject<T>(JSON, settings);
			
			Console.WriteLine("\nDeserialized {0}:", stroy);
			
			var json2 = JsonConvert.SerializeObject(stroy, settings);
			Console.WriteLine(json2);
			
			Console.WriteLine("stroy.EntityName: \"{0}\"", stroy.EntityName);
			Console.WriteLine("stroy dynamic member names: " + string.Join(",", stroy.GetDynamicMemberNames()));

			Console.WriteLine("\nRe-serialized {0}:", stroy);
			Console.WriteLine(json2);			
			
			Assert.IsTrue(stroy.EntityName == ((string)JObject.Parse(JSON)["EntityName"] ?? ""));
			Assert.IsTrue(stroy.ExplicitProperty == ((string)JObject.Parse(JSON)["ExplicitProperty"]));
			Assert.IsTrue(!stroy.GetDynamicMemberNames().Contains("EntityName"));						
			Assert.IsTrue(!stroy.GetDynamicMemberNames().Contains("ExplicitProperty"));		
			
			Console.WriteLine("\nAll tests passed.");
		}
	}

    public class EntityBaseConverter : ParameterizedDynamicObjectConverterBase<EntityBase>
    {
        public override EntityBase CreateObject(JObject jObj, Type objectType, JsonSerializer serializer, ICollection<string> usedParameters)
        {
            var entityName = jObj.GetValue("EntityName", StringComparison.OrdinalIgnoreCase);
            if (entityName != null)
            {
                usedParameters.Add(((JProperty)entityName.Parent).Name);
            }
			var entityNameString = entityName == null ? "" : entityName.ToString();
			if (objectType == typeof(EntityBase))
			{
	            return new EntityBase(entityName == null ? "" : entityName.ToString());				
			}
			else
			{
				return (EntityBase)Activator.CreateInstance(objectType, new object [] { entityNameString });
			}			
        }
    }

    public abstract class ParameterizedDynamicObjectConverterBase<T> : JsonConverter where T : DynamicObject
    {
		public override bool CanConvert(Type objectType) { return typeof(T).IsAssignableFrom(objectType); } // Or possibly return objectType == typeof(T);

        public abstract T CreateObject(JObject jObj, Type objectType, JsonSerializer serializer, ICollection<string> usedParameters);

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
			// Logic adapted from JsonSerializerInternalReader.CreateDynamic()
			// https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs#L1751
			// By James Newton-King https://github.com/JamesNK
			
            var contract = (JsonDynamicContract)serializer.ContractResolver.ResolveContract(objectType);

            if (reader.TokenType == JsonToken.Null)
                return null;

            var jObj = JObject.Load(reader);

            var used = new HashSet<string>();
            var obj = CreateObject(jObj, objectType, serializer, used);

            foreach (var jProperty in jObj.Properties())
            {
                var memberName = jProperty.Name;
                if (used.Contains(memberName))
                    continue;
                // first attempt to find a settable property, otherwise fall back to a dynamic set without type
                JsonProperty property = contract.Properties.GetClosestMatchProperty(memberName);

                if (property != null && property.Writable && !property.Ignored)
                {
                    var propertyValue = jProperty.Value.ToObject(property.PropertyType, serializer);
                    property.ValueProvider.SetValue(obj, propertyValue);
                }
                else
                {
                    object propertyValue;
                    if (jProperty.Value.Type == JTokenType.Null)
                        propertyValue = null;
                    else if (jProperty.Value is JValue)
                        // Primitive
                        propertyValue = ((JValue)jProperty.Value).Value;
                    else
                        propertyValue = jProperty.Value.ToObject<IDynamicMetaObjectProvider>(serializer);
					// Unfortunately the following is not public!
                    // contract.TrySetMember(obj, memberName, propertyValue);
					// So we have to duplicate the logic of what Json.NET has already done.
					CallSiteCache.SetValue(memberName, obj, propertyValue);
                }				
            }
            return obj;
        }

        public override bool CanWrite { get { return false; } }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

	internal static class CallSiteCache
	{
		// Adapted from the answer to 
		// https://stackoverflow.com/questions/12057516/c-sharp-dynamicobject-dynamic-properties
		// by jbtule, https://stackoverflow.com/users/637783/jbtule
		// And also
		// https://github.com/mgravell/fast-member/blob/master/FastMember/CallSiteCache.cs
		// by Marc Gravell, https://github.com/mgravell
						
		private static readonly Dictionary<string, CallSite<Func<CallSite, object, object, object>>> setters 
			= new Dictionary<string, CallSite<Func<CallSite, object, object, object>>>();
		
		public static void SetValue(string propertyName, object target, object value)
		{
			CallSite<Func<CallSite, object, object, object>> site;
			
			lock (setters)
			{
				if (!setters.TryGetValue(propertyName, out site))
				{
					var binder = Binder.SetMember(CSharpBinderFlags.None,
						   propertyName, typeof(CallSiteCache),
						   new List<CSharpArgumentInfo>{
								   CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
								   CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)});
					setters[propertyName] = site = CallSite<Func<CallSite, object, object, object>>.Create(binder);
				}
			}
			
			site.Target(site, target, value);
		}
	}
	
	// this is not the actual type but a mock to test the behavior with
	public class EntityBase : DynamicObject
	{
		[JsonProperty]
		public string ExplicitProperty { get; set; }
		
		public string EntityName { get; private set; }
		
		private readonly Dictionary<string, object> values = new Dictionary<string, object>();
		
		public EntityBase(string entityName)
		{
			this.EntityName = entityName;
		}
		
		public virtual object this[string fieldname]
		{
			get
			{
				if (this.values.ContainsKey(fieldname))
					return this.values[fieldname];
				
				return null;
			}
			
			set
			{
				if (this.values.ContainsKey(fieldname))
					this.values[fieldname] = value;
				else
					this.values.Add(fieldname, value);
				
			}
		}
		
		public override IEnumerable<string> GetDynamicMemberNames()
		{
			return this.values.Keys.ToList();
		}
		
		// If you try to get a value of a property
		// not defined in the class, this method is called.
		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			result = this[binder.Name];
			return true;
		}
		
		// If you try to set a value of a property that is
		// not defined in the class, this method is called.
		public override bool TrySetMember(SetMemberBinder binder, object value)
		{
			this[binder.Name] = value;
			return true;
		}
		
	}

	public class Story : EntityBase
	{
		public Story(string entityName) : base(entityName) { }
	}
	
    public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
;