I have the following classes:
public class BaseResponse
{
public bool Success { get; set; }
public string Message { get; set; }
public BaseResponse(bool success, string message)
{
Success = success;
Message = message;
}
}
public class TextToSpeechResponse : BaseResponse
{
[JsonPropertyName("audioPreviewSuccess")]
public bool AudioPreviewSuccess { get; set; }
[JsonPropertyName("result")]
public SpeechSynthesisResult? Result { get; set; }
[JsonPropertyName("errorMessage")]
public string? ErrorMessage { get; set; }
[JsonConstructor]
public TextToSpeechResponse(bool audioPreviewSuccess, SpeechSynthesisResult speechResult, string? errorMessage, string message)
: base(true, message)
{
AudioPreviewSuccess = audioPreviewSuccess;
Result = speechResult;
ErrorMessage = errorMessage;
}
public TextToSpeechResponse()
: base(false, string.Empty)
{
AudioPreviewSuccess = false;
Result = null;
ErrorMessage = string.Empty;
}
}
I'm trying to deserialize a JSON using this code:
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
return JsonSerializer.Deserialize<TextToSpeechResponse>(authorizedResponseBody, options) ??
new TextToSpeechResponse {
Message = "Something went wrong with deserializing the authorizedResponseBody.",
};
The problem is that I get the following exception:
Each parameter in the deserialization constructor on type 'CpatCognitiveServicesApp.Responses.TextToSpeechResponse' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. Fields are only considered when 'JsonSerializerOptions.IncludeFields' is enabled. The match can be case-insensitive.
This is what the JSON string looks like:
{
"audioPreviewSuccess": true,
"result": {
"resultId": "ee80d3cf5f154fbba69401a4254b5b26",
"reason": 9,
"audioData": "UklGRnb1AABXQVZFZm10IBIAAAABAAEAgD4AAAB9AAACABAAAABkYXRhUPUAAAA",
"properties": {}
},
"errorMessage": null
}
As you can see, my class should have the properties mapped out correctly. I believe it's having an issue because properties
is coming back as {}
in the JSON.
I've tried adding UnknownTypeHandling = JsonUnknownTypeHandling.JsonNode
to the JsonSerializationOptions, but still got the same error. I've tried creating the following class to replace SpeechSynthesisResult
in the hopes of skipping or bypassing properties
, but the exception still occurs.
public class SpeechResult
{
[JsonPropertyName("audioData")]
public byte[] AudioData { get; set; }
[JsonPropertyName("reason")]
public ResultReason Reason { get; set; }
[JsonPropertyName("resultId")]
public string ResultId { get; set; }
[JsonPropertyName("properties")]
public JsonProperty Properties { get; set; }
[JsonConstructor]
public SpeechResult(byte[] audioData, ResultReason reason, string resultId, JsonProperty properties)
{
AudioData = audioData;
Reason = reason;
ResultId = resultId;
Properties = properties;
}
public SpeechResult()
{
AudioData = Array.Empty<byte>();
Reason = ResultReason.Canceled;
ResultId = string.Empty;
Properties = new JsonProperty();
}
}
I fear I'm missing something really silly, but I'm just not seeing it. My question is how can I successfully deserialize the string into the TextToSpeechResponse
class?