Skip to content

Commit 2ef8c7e

Browse files
committed
merge in develop
2 parents 31431d1 + 2659efe commit 2ef8c7e

File tree

9 files changed

+202
-37
lines changed

9 files changed

+202
-37
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22
==========
3+
## Version 2.2.1
4+
_2018-04-12_
5+
* Fixed: Serialization/Deserialization of generic object in the `assistant` service([361](https://github.com/watson-developer-cloud/unity-sdk/issues/361), [363](https://github.com/watson-developer-cloud/unity-sdk/pull/363)).
6+
37
## Version 2.2.0
48
_2018-04-09_
59
* New: Updated Visual Recognition with Core ML support ([4182](https://zenhub.innovate.ibm.com/app/workspace/o/watson/developer-experience/issues/4182), [357](https://github.com/watson-developer-cloud/unity-sdk/pull/357)).

Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_NAME = "Watson Developer Cloud Unity SDK"
3838
# could be handy for archiving the generated documentation or if some version
3939
# control system is used.
4040

41-
PROJECT_NUMBER = 2.2.0
41+
PROJECT_NUMBER = 2.2.1
4242

4343
# Using the PROJECT_BRIEF tag one can provide an optional one line description
4444
# for a project that appears at the top of each page and should give viewer a

Examples/ServiceExamples/Scripts/ExampleAssistant.cs

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ public class ExampleAssistant : MonoBehaviour
4343
private string _createdWorkspaceId;
4444

4545
private Assistant _service;
46-
47-
private string _inputString = "Turn on the winshield wipers";
46+
47+
private fsSerializer _serializer = new fsSerializer();
48+
49+
private string _inputString = "Hello";
50+
private string _conversationString0 = "unlock the door";
51+
private string _conversationString1 = "turn on the ac";
52+
private string _conversationString2 = "turn down the radio";
4853

4954
private static string _createdWorkspaceName = "unity-sdk-example-workspace-delete";
5055
private static string _createdWorkspaceDescription = "A Workspace created by the Unity SDK Assistant example script. Please delete this.";
@@ -59,6 +64,7 @@ public class ExampleAssistant : MonoBehaviour
5964
private static string _createdExample = "untiyExample";
6065
private static string _dialogNodeName = "untiyDialognode";
6166
private static string _dialogNodeDesc = "Unity SDK Integration test dialog node";
67+
private Dictionary<string, object> _context = null;
6268

6369
private bool _listWorkspacesTested = false;
6470
private bool _createWorkspaceTested = false;
@@ -154,10 +160,42 @@ private IEnumerator Examples()
154160
input.Add("text", _inputString);
155161
MessageRequest messageRequest = new MessageRequest()
156162
{
157-
Input = input,
158-
AlternateIntents = true
163+
Input = input
159164
};
160165
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest);
166+
while (!_messageTested)
167+
yield return null;
168+
_messageTested = false;
169+
170+
input["text"] = _conversationString0;
171+
MessageRequest messageRequest0 = new MessageRequest()
172+
{
173+
Input = input,
174+
Context = _context
175+
};
176+
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest0);
177+
while (!_messageTested)
178+
yield return null;
179+
_messageTested = false;
180+
181+
input["text"] = _conversationString1;
182+
MessageRequest messageRequest1 = new MessageRequest()
183+
{
184+
Input = input,
185+
Context = _context
186+
};
187+
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest1);
188+
while (!_messageTested)
189+
yield return null;
190+
_messageTested = false;
191+
192+
input["text"] = _conversationString2;
193+
MessageRequest messageRequest2 = new MessageRequest()
194+
{
195+
Input = input,
196+
Context = _context
197+
};
198+
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest2);
161199
while (!_messageTested)
162200
yield return null;
163201

@@ -628,9 +666,40 @@ private void OnListIntents(IntentCollection response, Dictionary<string, object>
628666
_listIntentsTested = true;
629667
}
630668

631-
private void OnMessage(MessageResponse response, Dictionary<string, object> customData)
669+
private void OnMessage(object response, Dictionary<string, object> customData)
632670
{
633671
Log.Debug("ExampleAssistant.OnMessage()", "Response: {0}", customData["json"].ToString());
672+
673+
// Convert resp to fsdata
674+
fsData fsdata = null;
675+
fsResult r = _serializer.TrySerialize(response.GetType(), response, out fsdata);
676+
if (!r.Succeeded)
677+
throw new WatsonException(r.FormattedMessages);
678+
679+
// Convert fsdata to MessageResponse
680+
MessageResponse messageResponse = new MessageResponse();
681+
object obj = messageResponse;
682+
r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
683+
if (!r.Succeeded)
684+
throw new WatsonException(r.FormattedMessages);
685+
686+
// Set context for next round of messaging
687+
object _tempContext = null;
688+
(response as Dictionary<string, object>).TryGetValue("context", out _tempContext);
689+
690+
if (_tempContext != null)
691+
_context = _tempContext as Dictionary<string, object>;
692+
else
693+
Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
694+
695+
// Get intent
696+
object tempIntentsObj = null;
697+
(response as Dictionary<string, object>).TryGetValue("intents", out tempIntentsObj);
698+
object tempIntentObj = (tempIntentsObj as List<object>)[0];
699+
object tempIntent = null;
700+
(tempIntentObj as Dictionary<string, object>).TryGetValue("intent", out tempIntent);
701+
string intent = tempIntent.ToString();
702+
634703
_messageTested = true;
635704
}
636705

Scripts/Services/Assistant/v1/Assistant.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using IBM.Watson.DeveloperCloud.Logging;
2323
using IBM.Watson.DeveloperCloud.Utilities;
2424
using System;
25+
using MiniJSON;
2526

2627
namespace IBM.Watson.DeveloperCloud.Services.Assistant.v1
2728
{
@@ -108,15 +109,40 @@ public Assistant(Credentials credentials)
108109
/// <param name="workspaceId">Unique identifier of the workspace.</param>
109110
/// <param name="request">The message to be sent. This includes the user's input, along with optional intents, entities, and context from the last response. (optional)</param>
110111
/// <param name="nodesVisitedDetails">Whether to include additional diagnostic information about the dialog nodes that were visited during processing of the message. (optional, default to false)</param>
111-
/// <returns><see cref="MessageResponse" />MessageResponse</returns>
112112
/// <param name="customData">A Dictionary<string, object> of data that will be passed to the callback. The raw json output from the REST call will be passed in this object as the value of the 'json' key.</string></param>
113-
public bool Message(SuccessCallback<MessageResponse> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null)
113+
public bool Message(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null)
114114
{
115115
if (successCallback == null)
116116
throw new ArgumentNullException("successCallback");
117117
if (failCallback == null)
118118
throw new ArgumentNullException("failCallback");
119119

120+
IDictionary<string, string> requestDict = new Dictionary<string, string>();
121+
if (request.Context != null)
122+
requestDict.Add("context", Json.Serialize(request.Context));
123+
if (request.Input != null)
124+
requestDict.Add("input", Json.Serialize(request.Input));
125+
if(request.AlternateIntents != null)
126+
requestDict.Add("alternate_intents", Json.Serialize(request.AlternateIntents));
127+
if (request.Entities != null)
128+
requestDict.Add("entities", Json.Serialize(request.Entities));
129+
if (request.Intents != null)
130+
requestDict.Add("intents", Json.Serialize(request.Intents));
131+
if (request.Output != null)
132+
requestDict.Add("output", Json.Serialize(request.Output));
133+
134+
int iterator = 0;
135+
StringBuilder stringBuilder = new StringBuilder("{");
136+
foreach (KeyValuePair<string, string> property in requestDict)
137+
{
138+
string delimeter = iterator < requestDict.Count - 1 ? "," : "";
139+
stringBuilder.Append(string.Format("\"{0}\": {1}{2}", property.Key, property.Value, delimeter));
140+
iterator++;
141+
}
142+
stringBuilder.Append("}");
143+
144+
string stringToSend = stringBuilder.ToString();
145+
120146
MessageRequestObj req = new MessageRequestObj();
121147
req.SuccessCallback = successCallback;
122148
req.FailCallback = failCallback;
@@ -130,9 +156,7 @@ public bool Message(SuccessCallback<MessageResponse> successCallback, FailCallba
130156
}
131157
req.Parameters["version"] = VersionDate;
132158
req.Headers["Content-Type"] = "application/json";
133-
fsData data = null;
134-
_serializer.TrySerialize(request.Input, out data);
135-
req.Send = Encoding.UTF8.GetBytes(data.ToString());
159+
req.Send = Encoding.UTF8.GetBytes(stringToSend);
136160
req.OnResponse = OnMessageResponse;
137161

138162
RESTConnector connector = RESTConnector.GetConnector(Credentials, string.Format("/v1/workspaces/{0}/message", workspaceId));
@@ -147,7 +171,7 @@ private class MessageRequestObj : RESTConnector.Request
147171
/// <summary>
148172
/// The success callback.
149173
/// </summary>
150-
public SuccessCallback<MessageResponse> SuccessCallback { get; set; }
174+
public SuccessCallback<object> SuccessCallback { get; set; }
151175
/// <summary>
152176
/// The fail callback.
153177
/// </summary>
@@ -160,24 +184,18 @@ private class MessageRequestObj : RESTConnector.Request
160184

161185
private void OnMessageResponse(RESTConnector.Request req, RESTConnector.Response resp)
162186
{
163-
MessageResponse result = new MessageResponse();
164-
fsData data = null;
187+
object result = null;
188+
string data = "";
165189
Dictionary<string, object> customData = ((MessageRequestObj)req).CustomData;
166190
customData.Add(Constants.String.RESPONSE_HEADERS, resp.Headers);
167191

168192
if (resp.Success)
169193
{
170194
try
171195
{
172-
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
173-
if (!r.Succeeded)
174-
throw new WatsonException(r.FormattedMessages);
175-
176-
object obj = result;
177-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
178-
if (!r.Succeeded)
179-
throw new WatsonException(r.FormattedMessages);
180-
196+
// For deserializing into a generic object
197+
data = Encoding.UTF8.GetString(resp.Data);
198+
result = Json.Deserialize(data);
181199
customData.Add("json", data);
182200
}
183201
catch (Exception e)

Scripts/Services/Assistant/v1/IAssistant.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace IBM.Watson.DeveloperCloud.Services.Assistant.v1
3737

3838
public interface IAssistant
3939
{
40-
bool Message(SuccessCallback<MessageResponse> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null);
40+
bool Message(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null);
4141
bool CreateWorkspace(SuccessCallback<Workspace> successCallback, FailCallback failCallback, CreateWorkspace properties = null, Dictionary<string, object> customData = null);
4242

4343
bool DeleteWorkspace(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceId, Dictionary<string, object> customData = null);

Scripts/Services/Assistant/v1/Model/MessageRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MessageRequest
3131
/// </summary>
3232
/// <value>An input object that includes the input text.</value>
3333
[fsProperty("input")]
34-
public Dictionary<string, object> Input { get; set; }
34+
public object Input { get; set; }
3535
/// <summary>
3636
/// Whether to return more than one intent. Set to `true` to return all matching intents.
3737
/// </summary>
@@ -43,7 +43,7 @@ public class MessageRequest
4343
/// </summary>
4444
/// <value>State information for the conversation. Continue a conversation by including the context object from the previous response.</value>
4545
[fsProperty("context")]
46-
public Dictionary<string, object> Context { get; set; }
46+
public object Context { get; set; }
4747
/// <summary>
4848
/// Entities to use when evaluating the message. Include entities from the previous response to continue using those entities rather than detecting entities in the new input.
4949
/// </summary>

Scripts/Services/Assistant/v1/Model/MessageResponse.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,37 @@ public class MessageResponse
3131
/// </summary>
3232
/// <value>The user input from the request.</value>
3333
[fsProperty("input")]
34-
public Dictionary<string, object> Input { get; set; }
34+
public object Input { get; set; }
3535
/// <summary>
3636
/// An array of intents recognized in the user input, sorted in descending order of confidence.
3737
/// </summary>
3838
/// <value>An array of intents recognized in the user input, sorted in descending order of confidence.</value>
3939
[fsProperty("intents")]
40-
public Dictionary<string, object> Intents { get; set; }
40+
public object Intents { get; set; }
4141
/// <summary>
4242
/// An array of entities identified in the user input.
4343
/// </summary>
4444
/// <value>An array of entities identified in the user input.</value>
4545
[fsProperty("entities")]
46-
public Dictionary<string, object> Entities { get; set; }
46+
public object Entities { get; set; }
4747
/// <summary>
4848
/// Whether to return more than one intent. A value of `true` indicates that all matching intents are returned.
4949
/// </summary>
5050
/// <value>Whether to return more than one intent. A value of `true` indicates that all matching intents are returned.</value>
5151
[fsProperty("alternate_intents")]
52-
public Dictionary<string, object> AlternateIntents { get; set; }
52+
public object AlternateIntents { get; set; }
5353
/// <summary>
5454
/// State information for the conversation.
5555
/// </summary>
5656
/// <value>State information for the conversation.</value>
5757
[fsProperty("context")]
58-
public Dictionary<string, object> Context { get; set; }
58+
public object Context { get; set; }
5959
/// <summary>
6060
/// Output from the dialog, including the response to the user, the nodes that were triggered, and log messages.
6161
/// </summary>
6262
/// <value>Output from the dialog, including the response to the user, the nodes that were triggered, and log messages.</value>
6363
[fsProperty("output")]
64-
public Dictionary<string, object> Output { get; set; }
64+
public object Output { get; set; }
6565
}
6666

6767
}

0 commit comments

Comments
 (0)