JeffreySu/WeiXinMPSDK · error · WeixinException

JSON 反序列化结果为空,目标类型:

Error message

JSON 反序列化结果为空,目标类型:{typeof(T).FullName}

What it means

WeixinJsonSerializer.Deserialize<T> uses System.Text.Json's JsonSerializer.Deserialize with JsonTypeInfo, and throws WeixinException when the JSON input deserializes to null even though a T instance was expected. This guards callers from silently receiving a null object for a non-nullable T. It typically means the response body was 'null' (literally) or an empty/invalid payload.

Solutions

  1. Check the raw JSON string is non-empty and not 'null' before deserializing; inspect the HTTP response body and status.
  2. Log the raw payload and target type to identify why deserialization yields null.
  3. Verify the correct JsonTypeInfo/context is used for T so the payload maps to a real object.
  4. Catch WeixinException and treat it as an upstream/API error with the raw body preserved for debugging.

Example fix

// before
var result = WeixinJsonSerializer.Deserialize<WxApiResult>(body, jsonTypeInfo);
// after
if (string.IsNullOrWhiteSpace(body) || body.Trim() == "null")
    throw new WeixinException($"Empty/null response body: {body}");
var result = WeixinJsonSerializer.Deserialize<WxApiResult>(body, jsonTypeInfo);
Defensive patterns

Strategy: try-catch

Validate before calling

if (string.IsNullOrWhiteSpace(json) || json.Trim() == "null")
    throw new WeixinException("Response body is empty or null");

Type guard

static bool IsDeserializable(string json) =>
    !string.IsNullOrWhiteSpace(json) && json.Trim() != "null";

Try / catch

try { result = WeixinJsonSerializer.Deserialize<T>(json, jsonTypeInfo); }
catch (WeixinException ex) { logger.LogError(ex, "Null deserialization for {T}: {Body}", typeof(T).Name, json); }

Prevention

When it happens

Trigger: Calling Deserialize<T> (or DeserializeWxJsonResult) on a JSON string that is 'null', whitespace, or a payload System.Text.Json maps to null for the given JsonTypeInfo.

Common situations: WeChat API returned an empty body or literal 'null' due to an upstream error; passing an empty response body from HttpClient; mismatched T/JsonTypeInfo where the JSON shape does not produce a value.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/2ee95f6c86dcd25e. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin/Senparc.Weixin/Helpers/Serializers/WeixinJsonSerializer.cs:92

        /// <summary>
        /// 使用调用方提供的源生成元数据反序列化对象。
        /// </summary>
        public static T Deserialize<T>(string json, JsonTypeInfo<T> jsonTypeInfo)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            if (jsonTypeInfo == null)
            {
                throw new ArgumentNullException(nameof(jsonTypeInfo));
            }

            var result = JsonSerializer.Deserialize(json, jsonTypeInfo);
            if (result == null)
            {
                throw new WeixinException($"JSON 反序列化结果为空,目标类型:{typeof(T).FullName}");
            }

            return result;
        }

        /// <summary>
        /// 使用 SDK 内置源生成元数据反序列化微信错误结果。
        /// </summary>
        public static WxJsonResult DeserializeWxJsonResult(string json)
        {
            return Deserialize(json, WeixinJsonSerializerContext.Default.WxJsonResult);
        }
    }
}

View on GitHub (pinned to be573f6f94)