System.Text.Json JsonSerializer.Deserialize throws "Each parameter in the deserialization constructor…" exception

huangapple go评论58阅读模式
英文:

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(&quot;audioPreviewSuccess&quot;)]
    public bool AudioPreviewSuccess { get; set; }

    [JsonPropertyName(&quot;result&quot;)]
    public SpeechSynthesisResult? Result { get; set; }

    [JsonPropertyName(&quot;errorMessage&quot;)]
    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&lt;TextToSpeechResponse&gt;(authorizedResponseBody, options) ??
               new TextToSpeechResponse {
                   Message = &quot;Something went wrong with deserializing the authorizedResponseBody.&quot;,
               };

The problem is that I get the following exception:

Each parameter in the deserialization constructor on type &#39;CpatCognitiveServicesApp.Responses.TextToSpeechResponse&#39; 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 &#39;JsonSerializerOptions.IncludeFields&#39; is enabled. The match can be case-insensitive.

This is what the JSON string looks like:

{
	&quot;audioPreviewSuccess&quot;: true,
	&quot;result&quot;: {
		&quot;resultId&quot;: &quot;ee80d3cf5f154fbba69401a4254b5b26&quot;,
		&quot;reason&quot;: 9,
		&quot;audioData&quot;: &quot;UklGRnb1AABXQVZFZm10IBIAAAABAAEAgD4AAAB9AAACABAAAABkYXRhUPUAAAA&quot;,
		&quot;properties&quot;: {}
	},
	&quot;errorMessage&quot;: 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(&quot;audioData&quot;)]
    public byte[] AudioData { get; set; }

    [JsonPropertyName(&quot;reason&quot;)]
    public ResultReason Reason { get; set; }

    [JsonPropertyName(&quot;resultId&quot;)]
    public string ResultId { get; set; }

    [JsonPropertyName(&quot;properties&quot;)]
    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&lt;byte&gt;();
        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)

huangapple
  • 本文由 发表于 2023年2月24日 09:42:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定