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

Examples/ExampleAssistantV2.unity

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ LightmapSettings:
5050
m_BounceScale: 1
5151
m_IndirectOutputScale: 1
5252
m_AlbedoBoost: 1
53-
m_TemporalCoherenceThreshold: 1
5453
m_EnvironmentLightingMode: 0
5554
m_EnableBakedLightmaps: 1
5655
m_EnableRealtimeLightmaps: 1
@@ -117,7 +116,8 @@ NavMeshSettings:
117116
GameObject:
118117
m_ObjectHideFlags: 0
119118
m_CorrespondingSourceObject: {fileID: 0}
120-
m_PrefabInternal: {fileID: 0}
119+
m_PrefabInstance: {fileID: 0}
120+
m_PrefabAsset: {fileID: 0}
121121
serializedVersion: 6
122122
m_Component:
123123
- component: {fileID: 474543417}
@@ -133,7 +133,8 @@ GameObject:
133133
MonoBehaviour:
134134
m_ObjectHideFlags: 0
135135
m_CorrespondingSourceObject: {fileID: 0}
136-
m_PrefabInternal: {fileID: 0}
136+
m_PrefabInstance: {fileID: 0}
137+
m_PrefabAsset: {fileID: 0}
137138
m_GameObject: {fileID: 474543415}
138139
m_Enabled: 1
139140
m_EditorHideFlags: 0
@@ -161,7 +162,8 @@ Transform:
161162
GameObject:
162163
m_ObjectHideFlags: 0
163164
m_CorrespondingSourceObject: {fileID: 0}
164-
m_PrefabInternal: {fileID: 0}
165+
m_PrefabInstance: {fileID: 0}
166+
m_PrefabAsset: {fileID: 0}
165167
serializedVersion: 6
166168
m_Component:
167169
- component: {fileID: 486024580}
@@ -178,14 +180,16 @@ GameObject:
178180
AudioListener:
179181
m_ObjectHideFlags: 0
180182
m_CorrespondingSourceObject: {fileID: 0}
181-
m_PrefabInternal: {fileID: 0}
183+
m_PrefabInstance: {fileID: 0}
184+
m_PrefabAsset: {fileID: 0}
182185
m_GameObject: {fileID: 486024577}
183186
m_Enabled: 1
184187
--- !u!20 &486024579
185188
Camera:
186189
m_ObjectHideFlags: 0
187190
m_CorrespondingSourceObject: {fileID: 0}
188-
m_PrefabInternal: {fileID: 0}
191+
m_PrefabInstance: {fileID: 0}
192+
m_PrefabAsset: {fileID: 0}
189193
m_GameObject: {fileID: 486024577}
190194
m_Enabled: 1
191195
serializedVersion: 2
@@ -194,6 +198,7 @@ Camera:
194198
m_projectionMatrixMode: 1
195199
m_SensorSize: {x: 36, y: 24}
196200
m_LensShift: {x: 0, y: 0}
201+
m_GateFitMode: 2
197202
m_FocalLength: 50
198203
m_NormalizedViewPortRect:
199204
serializedVersion: 2
@@ -225,7 +230,8 @@ Camera:
225230
Transform:
226231
m_ObjectHideFlags: 0
227232
m_CorrespondingSourceObject: {fileID: 0}
228-
m_PrefabInternal: {fileID: 0}
233+
m_PrefabInstance: {fileID: 0}
234+
m_PrefabAsset: {fileID: 0}
229235
m_GameObject: {fileID: 486024577}
230236
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
231237
m_LocalPosition: {x: 0, y: 1, z: -10}
@@ -238,7 +244,8 @@ Transform:
238244
GameObject:
239245
m_ObjectHideFlags: 0
240246
m_CorrespondingSourceObject: {fileID: 0}
241-
m_PrefabInternal: {fileID: 0}
247+
m_PrefabInstance: {fileID: 0}
248+
m_PrefabAsset: {fileID: 0}
242249
serializedVersion: 6
243250
m_Component:
244251
- component: {fileID: 561626363}
@@ -254,7 +261,8 @@ GameObject:
254261
Light:
255262
m_ObjectHideFlags: 0
256263
m_CorrespondingSourceObject: {fileID: 0}
257-
m_PrefabInternal: {fileID: 0}
264+
m_PrefabInstance: {fileID: 0}
265+
m_PrefabAsset: {fileID: 0}
258266
m_GameObject: {fileID: 561626361}
259267
m_Enabled: 1
260268
serializedVersion: 8
@@ -291,7 +299,8 @@ Light:
291299
Transform:
292300
m_ObjectHideFlags: 0
293301
m_CorrespondingSourceObject: {fileID: 0}
294-
m_PrefabInternal: {fileID: 0}
302+
m_PrefabInstance: {fileID: 0}
303+
m_PrefabAsset: {fileID: 0}
295304
m_GameObject: {fileID: 561626361}
296305
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
297306
m_LocalPosition: {x: 0, y: 3, z: 0}

Examples/GenericSerialization.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Watson.Assistant.V2;
19+
using IBM.Watson.Logging;
20+
using MiniJSON;
21+
using System.Collections.Generic;
22+
using UnityEngine;
23+
24+
namespace IBM.Watson.Examples
25+
{
26+
public class GenericSerialization : MonoBehaviour
27+
{
28+
private string responseJson = "{\"output\":{\"generic\":[{\"response_type\":\"text\",\"text\":\"Let me confirm: You want an appointment for Friday at 12 PM. Is this correct?\"}],\"intents\":[{\"intent\":\"Customer_Care_Appointments\",\"confidence\":0.642203044891357}],\"entities\":[{\"entity\":\"sys-date\",\"location\":[0,9],\"value\":\"2019-01-11\",\"confidence\":1,\"metadata\":{\"calendar_type\":\"GREGORIAN\",\"timezone\":\"GMT\"}},{\"entity\":\"reply\",\"location\":[10,16],\"value\":\"yes\",\"confidence\":1}]},\"context\":{\"global\":{\"system\":{\"turn_count\":5,\"skill_reference_id\":\"dc762f48-e10c-4418-92c1-c7be6feadbc0\"}},\"skills\":{\"main skill\":{\"user_defined\":{\"no_reservation\":true,\"time\":\"12:00:00\",\"name\":\"Watson\",\"date\":\"2019-01-11\"}}}}}";
29+
30+
void Start()
31+
{
32+
LogSystem.InstallDefaultReactors();
33+
34+
Dictionary<string, object> e = Json.Deserialize(responseJson) as Dictionary<string, object>;
35+
Dictionary<string, object> context = e["context"] as Dictionary<string, object>;
36+
Dictionary<string, object> skills = context["skills"] as Dictionary<string, object>;
37+
Dictionary<string, object> main_skill = skills["main skill"] as Dictionary<string, object>;
38+
Dictionary<string, object> user_defined = main_skill["user_defined"] as Dictionary<string, object>;
39+
40+
string name = user_defined["name"] as string;
41+
Log.Debug("GenericSerialization", "test: {0}", name);
42+
}
43+
44+
public T GetValueFromGeneric<T>(Dictionary<string, object> obj)
45+
{
46+
T response = default;
47+
return response;
48+
}
49+
}
50+
}

Examples/GenericSerialization.cs.meta

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

0 commit comments

Comments
 (0)