-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathOutputSpeechConverter.cs
51 lines (41 loc) · 1.56 KB
/
OutputSpeechConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Alexa.NET.Response.Converters
{
public class OutputSpeechConverter : JsonConverter
{
public override bool CanRead => true;
public override bool CanWrite => false;
public static Dictionary<string, Func<IOutputSpeech>> TypeFactories = new()
{
{ "SSML", () => new SsmlOutputSpeech() },
{ "PlainText", () => new PlainTextOutputSpeech() },
};
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var typeKey = jsonObject["type"] ?? jsonObject["Type"];
var typeValue = typeKey.Value<string>();
var hasFactory = TypeFactories.ContainsKey(typeValue);
if (!hasFactory)
throw new(
$"unable to deserialize response. " +
$"unrecognized output speech type '{typeValue}'"
);
var speech = TypeFactories[typeValue]();
serializer.Populate(jsonObject.CreateReader(), speech);
return speech;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IOutputSpeech);
}
}
}