英文:
System.Text.Json JsonSerializer.Deserialize throws "Each parameter in the deserialization constructor..." exception
问题
以下是您要翻译的代码部分:
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.",
};
这是您要翻译的代码部分,不包括问题描述部分。
英文:
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?
答案1
得分: 0
你的JsonConstructor中有一个错误,请修复SpeechResult属性,它应该与JSON字符串中的内容相同。
英文:
you have a bug in your JsonConstructor, fix a SpeechResult property, it should be the same as it is in a json string
public TextToSpeechResponse(bool audioPreviewSuccess, SpeechResult result, string? errorMessage, string message)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论