Skip to content

Commit

Permalink
# Handling LUIS JObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
maurok committed Jun 1, 2017
1 parent 6c43709 commit 0778c0d
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<object>();
}
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<PropertyInfo> properties,
Expand Down

0 comments on commit 0778c0d

Please sign in to comment.