Commits

Anonymous committed 52db266

Corrected bug deserializing enumerations.

  • Participants
  • Parent commits 7491815

Comments (0)

Files changed (6)

File Json.Tests/Json/Serialization/JsonSerializerTest.cs

 			           		{"StringProp", "stringValue"},
 			           		{"IntProp", 42},
 			           		{"DoubleProp", 6},
-			           		{"BoolProp", true}
+			           		{"BoolProp", true},
+							{"EnumProp", 2}
 			           	};
 			var expected = new ObjectWithBasicProps
-							{
-								StringProp = "stringValue",
-								IntProp = 42,
-								DoubleProp = 6.0,
-								BoolProp = true
+			               	{
+			               		StringProp = "stringValue",
+			               		IntProp = 42,
+			               		DoubleProp = 6.0,
+			               		BoolProp = true,
+			               		EnumProp = JsonValueType.Boolean
 							};
 			var actual = _serializer.Deserialize<ObjectWithBasicProps>(json);
 			Assert.AreEqual(expected, actual);
 							StringProp = "stringValue",
 							IntProp = 42,
 							DoubleProp = 6.0,
-							BoolProp = true
+							BoolProp = true,
+							EnumProp = JsonValueType.Boolean
 						};
 			JsonValue expected = new JsonObject
 									{
 										{"StringProp", "stringValue"},
 										{"IntProp", 42},
 										{"DoubleProp", 6},
-										{"BoolProp", true}
+										{"BoolProp", true},
+										{"EnumProp", 2}
 									};
 			var actual = _serializer.Serialize(obj);
 			Assert.AreEqual(expected, actual);

File Json.Tests/Test References/ObjectWithBasicProps.cs

 
 ***************************************************************************************/
 using Manatee.Json.Attributes;
+using Manatee.Json.Enumerations;
 
 namespace Manatee.Tests.Test_References
 {
 		public int IntProp { get; set; }
 		public double DoubleProp { get; set; }
 		public bool BoolProp { get; set; }
+		public JsonValueType EnumProp { get; set; }
 		#endregion
 
 		#region Nonserializable Instance Properties
 			       other.BoolProp.Equals(BoolProp) &&
 			       Equals(other.ReadOnlyProp, ReadOnlyProp) &&
 			       Equals(other.WriteOnlyProp, WriteOnlyProp) &&
-			       Equals(other.IgnoreProp, IgnoreProp);
+			       Equals(other.IgnoreProp, IgnoreProp) &&
+				   Equals(other.EnumProp, EnumProp);
 		}
 		public override int GetHashCode()
 		{
 				result = (result * 397) ^ (ReadOnlyProp != null ? ReadOnlyProp.GetHashCode() : 0);
 				result = (result * 397) ^ (WriteOnlyProp != null ? WriteOnlyProp.GetHashCode() : 0);
 				result = (result * 397) ^ (IgnoreProp != null ? IgnoreProp.GetHashCode() : 0);
+				result = (result * 397) ^ EnumProp.GetHashCode();
 				return result;
 			}
 		}

File Json.sln.docstates.suo

Binary file modified.

File Json.suo

Binary file modified.

File Json/Helpers/ObjectCaster.cs

 ***************************************************************************************/
 using System;
 using System.Reflection;
+using System.Reflection.Emit;
 
 namespace Manatee.Json.Helpers
 {
 			catch (InvalidCastException)
 			{
 				result = default(T);
-				var parseMethod = typeof(T).GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public,
+				var parseMethod = typeof(T).GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy,
 													  null, new[] { typeof(string), typeof(T).MakeByRefType() }, null);
 				if (parseMethod == null) return false;
 				var paramsList = new object[] { obj.ToString(), result };
 				return default(T);
 			}
 		}
-
 	}
 }

File Json/Helpers/PrimitiveMapper.cs

 		{
 			if (!IsPrimitive(typeof(T)))
 			    throw new NotPrimitiveTypeException(typeof(T));
+			if (typeof(Enum).IsAssignableFrom(typeof(T)))
+				return (T) Enum.ToObject(typeof (T), (int) json.Number);
 			var value = json.GetValue();
 			T result;
 			ObjectCaster.TryCast(value, out result);
 
 		public static bool IsPrimitive(Type type)
 		{
-			return (type == typeof (string)) || type.IsPrimitive;
+			return type.IsPrimitive || (type == typeof (string)) || (typeof(Enum).IsAssignableFrom(type));
 		}
 
 		private static object GetValue(this JsonValue json)