Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.NET SDK] Add JSONConverter to support builtin.datetimeV2.* LUIS entities #2964

Merged
merged 3 commits into from
Jul 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.Bot.Builder.Luis.Models
using Newtonsoft.Json;
using Microsoft.Rest;
using Microsoft.Rest.Serialization;
using Newtonsoft.Json.Linq;

/// <summary>
/// Luis entity recommendation. Look at https://www.luis.ai/Help for more
Expand Down Expand Up @@ -82,7 +83,7 @@ public EntityRecommendation(string type, string role = default(string), string e
/// exact form of the resolution is defined by the entity type and is
/// documented here: https://www.luis.ai/Help#PreBuiltEntities.
/// </summary>
[JsonProperty(PropertyName = "resolution")]
[JsonProperty(PropertyName = "resolution", ItemConverterType = typeof(ResolutionConverter))]
public IDictionary<string, object> Resolution { get; set; }

/// <summary>
Expand All @@ -95,5 +96,108 @@ public virtual void Validate()
throw new ValidationException(ValidationRules.CannotBeNull, "Type");
}
}

internal class ResolutionConverter : JsonConverter
{
private const string UnexpectedEndError = "Unexpected end when reading IDictionary<string, object>";

public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IDictionary<string, object>));
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return ReadValue(reader);
}

private static object ReadValue(JsonReader reader)
{
while (reader.TokenType == JsonToken.Comment)
{
if (!reader.Read())
{
throw new JsonSerializationException("Unexpected token when converting IDictionary<string, object>");
}
}

switch (reader.TokenType)
{
case JsonToken.StartObject:
return ReadObject(reader);
case JsonToken.StartArray:
return ReadArray(reader);
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.String:
case JsonToken.Boolean:
case JsonToken.Undefined:
case JsonToken.Null:
case JsonToken.Date:
case JsonToken.Bytes:
return reader.Value;
default:
throw new JsonSerializationException
(string.Format("Unexpected token when converting IDictionary<string, object>: {0}", reader.TokenType));
}
}

private static object ReadArray(JsonReader reader)
{
IList<object> list = new List<object>();

while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.Comment:
break;
default:
var value = ReadValue(reader);

list.Add(value);
break;
case JsonToken.EndArray:
return list;
}
}

throw new JsonSerializationException(UnexpectedEndError);
}

private static object ReadObject(JsonReader reader)
{
var dictionary = new Dictionary<string, object>();

while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
var propertyName = reader.Value.ToString();

if (!reader.Read())
{
throw new JsonSerializationException(UnexpectedEndError);
}

var value = ReadValue(reader);

dictionary[propertyName] = value;
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
return dictionary;
}
}

throw new JsonSerializationException(UnexpectedEndError);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
}
}