Commits

Anonymous committed ad984d4

Added IResolver so that DI containers can be used to instantiate objects.

  • Participants
  • Parent commits 60a9c51

Comments (0)

Files changed (10)

 		{E74B7538-5E17-43C7-925B-33784945312E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{E74B7538-5E17-43C7-925B-33784945312E}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{E74B7538-5E17-43C7-925B-33784945312E}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{E74B7538-5E17-43C7-925B-33784945312E}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

Manatee.Json.v11.suo

Binary file modified.

Manatee.Json/Manatee.Json.csproj

     <Compile Include="Schema\SchemaValidationError.cs" />
     <Compile Include="Schema\SchemaValidationResults.cs" />
     <Compile Include="Serialization\IJsonSerializable.cs" />
+    <Compile Include="Serialization\Internal\ActivatorResolver.cs" />
     <Compile Include="Serialization\Internal\JsonSerializableSerializer.cs" />
+    <Compile Include="Serialization\IResolver.cs" />
     <Compile Include="Serialization\JsonMapToAttribute.cs" />
     <Compile Include="Serialization\EnumSerializationFormat.cs" />
     <Compile Include="Serialization\MapBaseAbstractionBehavior.cs" />

Manatee.Json/Serialization/IResolver.cs

+/***************************************************************************************
+
+	Copyright 2012 Greg Dennis
+
+	   Licensed under the Apache License, Version 2.0 (the "License");
+	   you may not use this file except in compliance with the License.
+	   You may obtain a copy of the License at
+
+		 http://www.apache.org/licenses/LICENSE-2.0
+
+	   Unless required by applicable law or agreed to in writing, software
+	   distributed under the License is distributed on an "AS IS" BASIS,
+	   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+	   See the License for the specific language governing permissions and
+	   limitations under the License.
+ 
+	File Name:		IResolver.cs
+	Namespace:		Manatee.Json.Serialization
+	Class Name:		IResolver
+	Purpose:		Defines methods required to resolved instances for
+					deserialization.
+
+***************************************************************************************/
+
+using System;
+
+namespace Manatee.Json.Serialization
+{
+	public interface IResolver
+	{
+		T Resolve<T>();
+		object Resolve(Type type);
+	}
+}

Manatee.Json/Serialization/Internal/ActivatorResolver.cs

+/***************************************************************************************
+
+	Copyright 2012 Greg Dennis
+
+	   Licensed under the Apache License, Version 2.0 (the "License");
+	   you may not use this file except in compliance with the License.
+	   You may obtain a copy of the License at
+
+		 http://www.apache.org/licenses/LICENSE-2.0
+
+	   Unless required by applicable law or agreed to in writing, software
+	   distributed under the License is distributed on an "AS IS" BASIS,
+	   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+	   See the License for the specific language governing permissions and
+	   limitations under the License.
+ 
+	File Name:		ActivatorResolver.cs
+	Namespace:		Manatee.Json.Serialization.Internal
+	Class Name:		ActivatorResolver
+	Purpose:		Implements IResolver using the Activator.
+
+***************************************************************************************/
+
+using System;
+
+namespace Manatee.Json.Serialization.Internal
+{
+	internal class ActivatorResolver : IResolver
+	{
+		public T Resolve<T>()
+		{
+			return Activator.CreateInstance<T>();
+		}
+		public object Resolve(Type type)
+		{
+			return Activator.CreateInstance(type);
+		}
+	}
+}

Manatee.Json/Serialization/Internal/AutoSerializer.cs

 		}
 		public T Deserialize<T>(JsonValue json, JsonSerializer serializer)
 		{
-			var obj = JsonSerializationAbstractionMap.CreateInstance<T>(json);
+			var obj = JsonSerializationAbstractionMap.CreateInstance<T>(json, serializer.Options.Resolver);
 			var type = obj.GetType();
 			var propertyInfoList = GetProperties(type);
 			var map = DeserializeValues(obj, json, serializer, propertyInfoList, !serializer.Options.CaseSensitiveDeserialization);

Manatee.Json/Serialization/Internal/JsonCompatibleSerializer.cs

 		}
 		public T Deserialize<T>(JsonValue json, JsonSerializer serializer)
 		{
-			var value = (IJsonCompatible) JsonSerializationAbstractionMap.CreateInstance<T>(json);
+			var value = (IJsonCompatible) JsonSerializationAbstractionMap.CreateInstance<T>(json, serializer.Options.Resolver);
 			value.FromJson(json);
 			return (T) value;
 		}

Manatee.Json/Serialization/Internal/JsonSerializableSerializer.cs

 
 namespace Manatee.Json.Serialization.Internal
 {
-	public class JsonSerializableSerializer : ISerializer
+	internal class JsonSerializableSerializer : ISerializer
 	{
 		public bool ShouldMaintainReferences { get { return true; } }
 
 		}
 		public T Deserialize<T>(JsonValue json, JsonSerializer serializer)
 		{
-			var value = (IJsonSerializable) JsonSerializationAbstractionMap.CreateInstance<T>(json);
+			var value = (IJsonSerializable) JsonSerializationAbstractionMap.CreateInstance<T>(json, serializer.Options.Resolver);
 			value.FromJson(json, serializer);
 			return (T) value;
 		}

Manatee.Json/Serialization/JsonSerializationAbstractionMap.cs

 	public static class JsonSerializationAbstractionMap
 	{
 		private static readonly Dictionary<Type, Type> _registry;
+		private static readonly IResolver _defaultResolver;
 
 		static JsonSerializationAbstractionMap()
 		{
 			_registry = new Dictionary<Type, Type>();
+			_defaultResolver = new ActivatorResolver();
 		}
 
 		/// <summary>
 			return _registry.ContainsKey(type) ? _registry[type] : type;
 		}
 
-		internal static T CreateInstance<T>(JsonValue json)
+		internal static T CreateInstance<T>(JsonValue json, IResolver resolver)
 		{
 			var type = typeof (T);
+			resolver = resolver ?? _defaultResolver;
 			if (type.IsAbstract || type.IsInterface)
 			{
 				if ((json.Type == JsonValueType.Object) && (json.Object.ContainsKey(Constants.TypeKey)))
 				{
 					var concrete = Type.GetType(json.Object[Constants.TypeKey].String);
-					return (T) Activator.CreateInstance(concrete);
+					return (T) resolver.Resolve(concrete);
 				}
 				if (_registry.ContainsKey(type))
 				{
 					var concrete = _registry[type];
-					return (T) Activator.CreateInstance(concrete);
+					return (T) resolver.Resolve(concrete);
 				}
 				if (type.IsInterface)
 					return TypeGenerator.Default.Generate<T>();
 			}
-			return Activator.CreateInstance<T>();
+			return resolver.Resolve<T>();
 		}
 
 		private static void MapBaseTypes(Type tAbstract, Type tConcrete, bool overwrite)

Manatee.Json/Serialization/JsonSerializerOptions.cs

 		/// </remarks>
 		public bool CaseSensitiveDeserialization { get; set; }
 		/// <summary>
-		/// Gets and sets whether the serializer always includes the type name when serializing.
+		/// Gets and sets whether the serializer always includes the type name while serializing.
 		/// </summary>
 		/// <remarks>
 		/// This only affect automatic serialization.
 		/// </remarks>
 		public bool AlwaysSerializeTypeName { get; set; }
+		/// <summary>
+		/// Gets and sets an <see cref="IResolver"/> implementation for instantiating objects while deserializing.
+		/// </summary>
+		public IResolver Resolver { get; set; }
 
 		static JsonSerializerOptions()
 		{