Skip to content

Commit 38a0eb6

Browse files
authored
Add RecognizeComplete customized json serialization (Azure#36374)
1 parent 83e752b commit 38a0eb6

File tree

8 files changed

+100
-175
lines changed

8 files changed

+100
-175
lines changed

sdk/communication/Azure.Communication.CallAutomation/api/Azure.Communication.CallAutomation.netstandard2.0.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static partial class CallAutomationModelFactory
166166
public static Azure.Communication.CallAutomation.PlayCompleted PlayCompleted(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, Azure.Communication.CallAutomation.ResultInformation resultInformation = null) { throw null; }
167167
public static Azure.Communication.CallAutomation.PlayFailed PlayFailed(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, Azure.Communication.CallAutomation.ResultInformation resultInformation = null) { throw null; }
168168
public static Azure.Communication.CallAutomation.RecognizeCanceled RecognizeCanceled(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null) { throw null; }
169-
public static Azure.Communication.CallAutomation.RecognizeCompleted RecognizeCompleted(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, Azure.Communication.CallAutomation.ResultInformation resultInformation = null, Azure.Communication.CallAutomation.CallMediaRecognitionType recognitionType = default(Azure.Communication.CallAutomation.CallMediaRecognitionType), Azure.Communication.CallAutomation.DtmfResult dtmfResult = null, Azure.Communication.CallAutomation.ChoiceResult choiceResult = null, Azure.Communication.CallAutomation.SpeechResult speechResult = null) { throw null; }
169+
public static Azure.Communication.CallAutomation.RecognizeCompleted RecognizeCompleted(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, Azure.Communication.CallAutomation.ResultInformation resultInformation = null, Azure.Communication.CallAutomation.CallMediaRecognitionType recognitionType = default(Azure.Communication.CallAutomation.CallMediaRecognitionType), Azure.Communication.CallAutomation.RecognizeResult recognizeResult = null) { throw null; }
170170
public static Azure.Communication.CallAutomation.RecognizeFailed RecognizeFailed(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, Azure.Communication.CallAutomation.ResultInformation resultInformation = null) { throw null; }
171171
public static Azure.Communication.CallAutomation.RecordingStateChanged RecordingStateChanged(string callConnectionId = null, string serverCallId = null, string correlationId = null, string recordingId = null, Azure.Communication.CallAutomation.RecordingState state = default(Azure.Communication.CallAutomation.RecordingState), System.DateTimeOffset? startDateTime = default(System.DateTimeOffset?)) { throw null; }
172172
public static Azure.Communication.CallAutomation.RecordingStateResult RecordingStateResult(string recordingId = null, Azure.Communication.CallAutomation.RecordingState? recordingState = default(Azure.Communication.CallAutomation.RecordingState?)) { throw null; }
@@ -812,8 +812,10 @@ public RecognizeChoice(string label, System.Collections.Generic.IEnumerable<stri
812812
public partial class RecognizeCompleted : Azure.Communication.CallAutomation.CallAutomationEventBase
813813
{
814814
internal RecognizeCompleted() { }
815+
public Azure.Communication.CallAutomation.CallMediaRecognitionType RecognitionType { get { throw null; } set { } }
815816
public Azure.Communication.CallAutomation.RecognizeResult RecognizeResult { get { throw null; } }
816817
public static Azure.Communication.CallAutomation.RecognizeCompleted Deserialize(string content) { throw null; }
818+
public string Serialize() { throw null; }
817819
}
818820
public partial class RecognizeFailed : Azure.Communication.CallAutomation.CallAutomationEventBase
819821
{

sdk/communication/Azure.Communication.CallAutomation/src/Models/CallAutomationModelFactory.cs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -194,47 +194,11 @@ public static RemoveParticipantSucceeded RemoveParticipantSucceeded(string callC
194194
/// Determines the sub-type of the recognize operation.
195195
/// In case of cancel operation the this field is not set and is returned empty
196196
/// </param>
197-
/// <param name="dtmfResult"> Defines the result for RecognitionType = Dtmf. </param>
198-
/// <param name="choiceResult"> Defines the result for RecognitionType = Choices. </param>
199-
/// <param name="speechResult"> Defines the result for RecognitionType = Speech. </param>
197+
/// <param name="recognizeResult"> Defines the result for RecognitionType = Dtmf,Choice,Speech. </param>
200198
/// <returns> A new <see cref="CallAutomation.RecognizeCompleted"/> instance for mocking. </returns>
201-
public static RecognizeCompleted RecognizeCompleted(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, ResultInformation resultInformation = null, CallMediaRecognitionType recognitionType = default, DtmfResult dtmfResult = null, ChoiceResult choiceResult = null, SpeechResult speechResult = null)
199+
public static RecognizeCompleted RecognizeCompleted(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, ResultInformation resultInformation = null, CallMediaRecognitionType recognitionType = default, RecognizeResult recognizeResult = null)
202200
{
203-
return new RecognizeCompleted(callConnectionId, serverCallId, correlationId, operationContext, resultInformation, recognitionType, dtmfResult, choiceResult, speechResult);
204-
}
205-
206-
/// <summary> Initializes a new instance of RecognizeCompletedInternal. </summary>
207-
/// <param name="callConnectionId"> Call connection ID. </param>
208-
/// <param name="serverCallId"> Server call ID. </param>
209-
/// <param name="correlationId"> Correlation ID for event to call corre lation. Also called ChainId for skype chain ID. </param>
210-
/// <param name="operationContext"> Used by customers when calling mid-call actions to correlate the request to the response event. </param>
211-
/// <param name="resultInformation"> Contains the resulting SIP code/sub-code and message from NGC services. </param>
212-
/// <param name="recognitionType">
213-
/// Determines the sub-type of the recognize operation.
214-
/// In case of cancel operation the this field is not set and is returned empty
215-
/// </param>
216-
/// <param name="choiceResult"> Defines the result for RecognitionType = Choices. </param>
217-
/// <returns> A new <see cref="CallAutomation.RecognizeChoiceCompleted"/> instance for mocking. </returns>
218-
internal static RecognizeChoiceCompleted RecognizeChoiceCompleted(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, ResultInformation resultInformation = null, CallMediaRecognitionType recognitionType = default, ChoiceResult choiceResult = null)
219-
{
220-
return new RecognizeChoiceCompleted(callConnectionId, serverCallId, correlationId, operationContext, resultInformation, recognitionType, choiceResult);
221-
}
222-
223-
/// <summary> Initializes a new instance of RecognizeCompletedInternal. </summary>
224-
/// <param name="callConnectionId"> Call connection ID. </param>
225-
/// <param name="serverCallId"> Server call ID. </param>
226-
/// <param name="correlationId"> Correlation ID for event to call corre lation. Also called ChainId for skype chain ID. </param>
227-
/// <param name="operationContext"> Used by customers when calling mid-call actions to correlate the request to the response event. </param>
228-
/// <param name="resultInformation"> Contains the resulting SIP code/sub-code and message from NGC services. </param>
229-
/// <param name="recognitionType">
230-
/// Determines the sub-type of the recognize operation.
231-
/// In case of cancel operation the this field is not set and is returned empty
232-
/// </param>
233-
/// <param name="dtmfResult"> Defines the result for RecognitionType = Dtmf. </param>
234-
/// <returns> A new <see cref="CallAutomation.RecognizeDtmfCompleted"/> instance for mocking. </returns>
235-
internal static RecognizeDtmfCompleted RecognizeDtmfCompleted(string callConnectionId = null, string serverCallId = null, string correlationId = null, string operationContext = null, ResultInformation resultInformation = null, CallMediaRecognitionType recognitionType = default, DtmfResult dtmfResult = null)
236-
{
237-
return new RecognizeDtmfCompleted(callConnectionId, serverCallId, correlationId, operationContext, resultInformation, recognitionType, dtmfResult);
201+
return new RecognizeCompleted(callConnectionId, serverCallId, correlationId, operationContext, resultInformation, recognitionType, recognizeResult);
238202
}
239203
}
240204
}

sdk/communication/Azure.Communication.CallAutomation/src/Models/Events/RecognizeChoiceCompleted.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

sdk/communication/Azure.Communication.CallAutomation/src/Models/Events/RecognizeCompleted.cs

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using Azure.Core;
66
using System.Text.Json.Serialization;
77
using System;
8+
using System.Runtime.Serialization;
9+
using System.IO;
10+
using System.Text;
811

912
namespace Azure.Communication.CallAutomation
1013
{
@@ -23,7 +26,7 @@ public partial class RecognizeCompleted : CallAutomationEventBase
2326
/// </summary>
2427
[CodeGenMember("RecognitionType")]
2528
[JsonConverter(typeof(EquatableEnumJsonConverter<CallMediaRecognitionType>))]
26-
private CallMediaRecognitionType RecognitionType { get; set; }
29+
public CallMediaRecognitionType RecognitionType { get; set; }
2730

2831
/// <summary> Initializes a new instance of RecognizeCompleted. </summary>
2932
internal RecognizeCompleted()
@@ -40,30 +43,16 @@ internal RecognizeCompleted()
4043
/// Determines the sub-type of the recognize operation.
4144
/// In case of cancel operation the this field is not set and is returned empty
4245
/// </param>
43-
/// <param name="dtmfResult"> Defines the result for RecognitionType = Dtmf. </param>
44-
/// <param name="choiceResult"> Defines the result for RecognitionType = Choices. </param>
45-
/// <param name="speechResult"> Defines the result for RecognitionType = Speech. </param>
46-
internal RecognizeCompleted(string callConnectionId, string serverCallId, string correlationId, string operationContext, ResultInformation resultInformation, CallMediaRecognitionType recognitionType, DtmfResult dtmfResult, ChoiceResult choiceResult, SpeechResult speechResult)
46+
/// <param name="recognizeResult"> Defines the result for general recognizeResult. </param>
47+
internal RecognizeCompleted(string callConnectionId, string serverCallId, string correlationId, string operationContext, ResultInformation resultInformation, CallMediaRecognitionType recognitionType, RecognizeResult recognizeResult)
4748
{
4849
CallConnectionId = callConnectionId;
4950
ServerCallId = serverCallId;
5051
CorrelationId = correlationId;
5152
OperationContext = operationContext;
5253
ResultInformation = resultInformation;
5354
RecognitionType = recognitionType;
54-
55-
if (RecognitionType == CallMediaRecognitionType.Dtmf)
56-
{
57-
RecognizeResult = dtmfResult;
58-
}
59-
else if (RecognitionType == CallMediaRecognitionType.Choices)
60-
{
61-
RecognizeResult = choiceResult;
62-
}
63-
else if (RecognitionType == CallMediaRecognitionType.Speech || RecognitionType == CallMediaRecognitionType.SpeechOrDtmf)
64-
{
65-
RecognizeResult = speechResult;
66-
}
55+
RecognizeResult = recognizeResult;
6756
}
6857

6958
/// <summary> Initializes a new instance of RecognizeCompletedEvent. </summary>
@@ -118,5 +107,69 @@ public static RecognizeCompleted Deserialize(string content)
118107

119108
return new RecognizeCompleted(parsedRecognizeCompleted);
120109
}
110+
111+
/// <summary>
112+
/// Serialize <see cref="RecognizeCompleted"/> event.
113+
/// </summary>
114+
/// <returns>The serialized json string.</returns>
115+
public string Serialize()
116+
{
117+
string jsonValue = "";
118+
119+
var options = new JsonWriterOptions
120+
{
121+
Indented = true
122+
};
123+
124+
JsonConverter jsonConverter = new EquatableEnumJsonConverter<CallMediaRecognitionType>();
125+
var recognitionTypeOption = new JsonSerializerOptions()
126+
{
127+
Converters = { jsonConverter },
128+
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
129+
};
130+
131+
using var stream = new MemoryStream();
132+
using var writer = new Utf8JsonWriter(stream, options);
133+
JsonSerializerOptions jsonSeializerOptionForObject = new JsonSerializerOptions()
134+
{
135+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
136+
};
137+
jsonSeializerOptionForObject.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
138+
writer.WriteStartObject();
139+
writer.WriteString("callConnectionId", CallConnectionId);
140+
writer.WriteString("serverCallId", ServerCallId);
141+
writer.WriteString("correlationId", CorrelationId);
142+
writer.WriteString("operationContext", OperationContext);
143+
writer.WritePropertyName("resultInformation");
144+
JsonSerializer.Serialize(writer, ResultInformation, jsonSeializerOptionForObject);
145+
writer.WritePropertyName("recognitionType");
146+
JsonSerializer.Serialize(writer, RecognitionType, recognitionTypeOption);
147+
148+
if (RecognitionType == CallMediaRecognitionType.Dtmf)
149+
{
150+
DtmfResult dtmfResult = (DtmfResult)RecognizeResult;
151+
writer.WritePropertyName("dtmfResult");
152+
JsonSerializer.Serialize(writer, dtmfResult, jsonSeializerOptionForObject);
153+
}
154+
else if (RecognitionType == CallMediaRecognitionType.Choices)
155+
{
156+
ChoiceResult choiceResult = (ChoiceResult)RecognizeResult;
157+
writer.WritePropertyName("choiceResult");
158+
JsonSerializer.Serialize(writer, choiceResult, jsonSeializerOptionForObject);
159+
}
160+
else if (RecognitionType == CallMediaRecognitionType.Speech)
161+
{
162+
SpeechResult speechResult = (SpeechResult)RecognizeResult;
163+
writer.WritePropertyName("speechResult");
164+
JsonSerializer.Serialize(writer, speechResult, jsonSeializerOptionForObject);
165+
}
166+
167+
writer.WriteEndObject();
168+
writer.Flush();
169+
170+
jsonValue = Encoding.UTF8.GetString(stream.ToArray());
171+
172+
return jsonValue;
173+
}
121174
}
122175
}

sdk/communication/Azure.Communication.CallAutomation/src/Models/Events/RecognizeDtmfCompleted.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

sdk/communication/Azure.Communication.CallAutomation/src/Models/Events/RecognizeResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.Runtime.Serialization;
5+
46
namespace Azure.Communication.CallAutomation
57
{
68
/// <summary>

0 commit comments

Comments
 (0)