From 0778c0d4d43771101945cf8ea297d45792f590fb Mon Sep 17 00:00:00 2001 From: maurok Date: Thu, 1 Jun 2017 16:31:18 -0300 Subject: [PATCH] # Handling LUIS JObjects --- .../LuisActionResolver.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs b/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs index 2b9e8a625a..9c5df45c1b 100644 --- a/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs +++ b/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs @@ -350,6 +350,9 @@ private static bool AssignValue(ILuisAction action, PropertyInfo property, objec { object value; + // handle LUIS JObjects + paramValue = SanitizeInputValue(type, paramValue); + if (type.IsArray) { value = BuildArrayOfValues(action, property, type.GetElementType(), paramValue); @@ -405,6 +408,33 @@ private static Array BuildArrayOfValues(ILuisAction action, PropertyInfo propert } } + private static object SanitizeInputValue(Type targetType, object value) + { + object result = value; + + // handle case where input is JArray returned from LUIS + if (value is Newtonsoft.Json.Linq.JArray) + { + var arrayOfValues = value as Newtonsoft.Json.Linq.JArray; + + if (targetType.IsArray) + { + result = arrayOfValues.AsEnumerable(); + } + else + { + if (arrayOfValues.Count > 1) + { + throw new FormatException("Cannot assign multiple values to single field"); + } + + result = arrayOfValues[0]; + } + } + + return result; + } + private static bool AssignEntitiesToMembers( ILuisAction action, IEnumerable properties,