From 40795503a606332efb14a254f258cac5593870fc Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sun, 25 Dec 2016 17:31:05 +0100 Subject: [PATCH] Fix for deserialization of Guid and Nullable types --- VAR.Json/JsonParser.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/VAR.Json/JsonParser.cs b/VAR.Json/JsonParser.cs index 1c30c65..eae390a 100644 --- a/VAR.Json/JsonParser.cs +++ b/VAR.Json/JsonParser.cs @@ -78,7 +78,23 @@ namespace VAR.Json { if (obj.ContainsKey(prop.Name)) { - prop.SetValue(newObj, Convert.ChangeType(obj[prop.Name], prop.PropertyType), null); + Type underliningType = Nullable.GetUnderlyingType(prop.PropertyType); + Type effectiveType = underliningType ?? prop.PropertyType; + object valueOrig = obj[prop.Name]; + object valueDest; + if (underliningType != null && valueOrig == null) + { + valueDest = null; + } + else if (effectiveType == typeof(Guid) && valueOrig is string) + { + valueDest = new Guid((string)valueOrig); + } + else + { + valueDest = Convert.ChangeType(obj[prop.Name], effectiveType); + } + prop.SetValue(newObj, valueDest, null); } } return newObj;