Skip to content

Commit add8b46

Browse files
committed
feat(Assistant V2): Deserializing Dictionaries
1 parent d31cd77 commit add8b46

15 files changed

+473
-33
lines changed

Assistant.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assistant/v2.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assistant/v2/Assistant.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ private void OnMessageResponse(RESTConnector.Request req, RESTConnector.Response
411411
response.Result = new MessageResponse();
412412
fsData data = null;
413413
Dictionary<string, object> customData = ((MessageRequestObj)req).CustomData;
414+
foreach (KeyValuePair<string, string> kvp in resp.Headers)
415+
{
416+
response.Headers.Add(kvp.Key, kvp.Value);
417+
}
418+
response.StatusCode = resp.HttpResponseCode;
414419

415420
try
416421
{

Assistant/v2/Models/MessageContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class MessageContext
3434
/// Contains information specific to particular skills within the Assistant.
3535
/// </summary>
3636
[fsProperty("skills")]
37-
public object Skills { get; set; }
37+
public MessageContextSkills Skills { get; set; }
3838
}
3939

4040
}

Assistant/v2/Models/MessageContextSkills.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,61 @@
1616
*/
1717

1818
using FullSerializer;
19+
using FullSerializer.Internal;
20+
using System;
21+
using System.Collections.Generic;
1922

2023
namespace IBM.Watson.Assistant.V2
2124
{
2225
/// <summary>
2326
/// Contains information specific to particular skills within the Assistant.
2427
/// </summary>
25-
[fsObject]
26-
public class MessageContextSkills
28+
[fsObject(Converter = typeof(MessageContextSkillsConverter))]
29+
public class MessageContextSkills : Dictionary<string, object>
2730
{
2831
}
2932

33+
public class MessageContextSkillsConverter : fsConverter
34+
{
35+
private fsSerializer serializer = new fsSerializer();
36+
37+
public override bool CanProcess(Type type)
38+
{
39+
return type == typeof(MessageContextSkills);
40+
}
41+
42+
public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
43+
{
44+
if (data.IsString == false)
45+
{
46+
return fsResult.Fail("Type converter requires a string");
47+
}
48+
49+
instance = fsTypeCache.GetType(data.AsString);
50+
if (instance == null)
51+
{
52+
return fsResult.Fail("Unable to find type " + data.AsString);
53+
}
54+
return fsResult.Success;
55+
}
56+
57+
public override object CreateInstance(fsData data, Type storageType)
58+
{
59+
return new MessageContextSkills();
60+
}
61+
62+
public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
63+
{
64+
MessageContextSkills messageContextSkills = (MessageContextSkills)instance;
65+
serialized = null;
66+
67+
Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();
68+
fsData tempData = null;
69+
70+
71+
72+
serialized = new fsData(serialization);
73+
return fsResult.Success;
74+
}
75+
}
3076
}

Core/Editor.meta

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/Editor/Help.meta

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/Editor/Help/Working.meta

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/WatsonResponse.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class WatsonResponse<T>
2424
/// <summary>
2525
/// The status code returned from the server.
2626
/// </summary>
27-
public int StatusCode { get; set; }
27+
public long StatusCode { get; set; }
2828
/// <summary>
2929
/// Dictionary of headers returned by the request.
3030
/// </summary>
@@ -33,5 +33,13 @@ public class WatsonResponse<T>
3333
/// The deserialized result.
3434
/// </summary>
3535
public T Result { get; set; }
36+
37+
public WatsonResponse()
38+
{
39+
if(Headers == null)
40+
{
41+
Headers = new Dictionary<string, object>();
42+
}
43+
}
3644
}
3745
}

Examples/ExampleAssistantV2.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ private IEnumerator Examples()
169169
Dictionary<string, string> userDefinedDictionary = new Dictionary<string, string>();
170170
userDefinedDictionary.Add("name", "Watson");
171171

172-
Dictionary<string, Dictionary<string, string>> skillDictionary = new Dictionary<string, Dictionary<string, string>>();
172+
Dictionary<string, object> skillDictionary = new Dictionary<string, object>();
173173
skillDictionary.Add("user_defined", userDefinedDictionary);
174174

175-
Dictionary<string, Dictionary<string, Dictionary<string, string>>> skills = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
175+
MessageContextSkills skills = new MessageContextSkills();
176176
skills.Add("main skill", skillDictionary);
177177

178178
//SerializableDictionary<string, string> userDefinedDictionary = new SerializableDictionary<string, string>();
@@ -249,8 +249,17 @@ private void OnMessage3(WatsonResponse<MessageResponse> response, WatsonError er
249249
}
250250
private void OnMessage4(WatsonResponse<MessageResponse> response, WatsonError error, System.Collections.Generic.Dictionary<string, object> customData)
251251
{
252-
Log.Debug("ExampleAssistantV2.OnMessage4()", "response: {0}", response.Result.Output.Generic[0].Text);
253-
//Log.Debug("ExampleAssistantV2.OnMessage4()", "response: {0}", response.Result.Context.Skills["main skill"]["user_defined"]["name"]);
252+
//Log.Debug("ExampleAssistantV2.OnMessage4()", "response: {0}", response.Result.Output.Generic[0].Text);
253+
254+
object e = response.Result as object;
255+
Dictionary<string, object> e2 = e as Dictionary<string, object>;
256+
Dictionary<string, object> context = e2["context"] as Dictionary<string, object>;
257+
Dictionary<string, object> skills = context["skills"] as Dictionary<string, object>;
258+
Dictionary<string, object> main_skill = skills["main skill"] as Dictionary<string, object>;
259+
Dictionary<string, object> user_defined = main_skill["user_defined"] as Dictionary<string, object>;
260+
261+
string name = user_defined["name"] as string;
262+
Log.Debug("GenericSerialization", "test: {0}", name);
254263
_messageTested4 = true;
255264
}
256265

0 commit comments

Comments
 (0)