From 0871098ccca72dfd92d3316cd0c3f355fa703295 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sat, 20 Jun 2015 16:36:51 +0200 Subject: [PATCH] JSONParser: Fix static type conversion to incomplete types. --- Scrummer/Code/JSON/JSONParser.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Scrummer/Code/JSON/JSONParser.cs b/Scrummer/Code/JSON/JSONParser.cs index cc228ef..ee0b050 100644 --- a/Scrummer/Code/JSON/JSONParser.cs +++ b/Scrummer/Code/JSON/JSONParser.cs @@ -53,7 +53,7 @@ namespace Scrummer.Code.JSON return typeProperties; } - private bool CompareToType(Dictionary obj, Type type) + private float CompareToType(Dictionary obj, Type type) { PropertyInfo[] typeProperties = Type_GetProperties(type); int count = 0; @@ -64,7 +64,7 @@ namespace Scrummer.Code.JSON count++; } } - return (count == typeProperties.Length); + return ((float)count / (float)typeProperties.Length); } private object ConvertToType(Dictionary obj, Type type) @@ -83,13 +83,21 @@ namespace Scrummer.Code.JSON private object TryConvertToTypes(Dictionary obj) { + Type bestMatch = null; + float bestMatchFactor = 0.0f; foreach (Type type in _knownTypes) { - if (CompareToType(obj, type)) + float matchFactor = CompareToType(obj, type); + if (matchFactor > bestMatchFactor) { - return ConvertToType(obj, type); + bestMatch = type; + bestMatchFactor = matchFactor; } } + if (bestMatch != null) + { + return ConvertToType(obj, bestMatch); + } return obj; }