JeffreySu/WeiXinMPSDK · error · NotSupportedException

未生成类型 的 JSON 元数据。

Error message

未生成类型 {typeof(T).FullName} 的 JSON 元数据。

What it means

TenPayJsonSerializer.Serialize uses source-generated System.Text.Json metadata (TenPayJsonSerializerContext). If no JsonTypeInfo was generated for the requested type T — i.e. the type is not registered with [JsonSerializable] on the serializer context — a NotSupportedException is thrown so serialization fails fast instead of silently falling back to reflection.

Solutions

  1. Use only types registered in TenPayJsonSerializerContext (the library's built-in request entities).
  2. If it's your own type, add [JsonSerializable(typeof(YourType))] to a JsonSerializerContext and serialize via that context instead.
  3. Update the Senparc.Weixin.TenPay package to a version whose serializer context includes the type you need.

Example fix

// before
string json = TenPayJsonSerializer.Serialize(myCustomType);
// after
[JsonSerializable(typeof(MyCustomType))]
internal partial class MyJsonContext : JsonSerializerContext { }
string json = JsonSerializer.Serialize(myCustomType, MyJsonContext.Default.MyCustomType);
Defensive patterns

Strategy: try-catch

Validate before calling

// C#
if (TenPayJsonSerializerContext.Default.GetTypeInfo(typeof(MyType)) is null)
    throw new InvalidOperationException("Type not registered in TenPayJsonSerializerContext");

Try / catch

try { json = TenPayJsonSerializer.Serialize(value); }
catch (NotSupportedException ex) when (ex.Message.Contains("JSON 元数据")) { /* fall back to JsonSerializerContext registered for the type */ }

Prevention

When it happens

Trigger: Internally serializing a request type T that is not included in TenPayJsonSerializerContext; typically hit when using a custom entity or a new type with a serialization path, especially under Native AOT / trimmed publishing where reflection serialization is disabled.

Common situations: Upgrading the library or SDK version and passing a newly introduced type not yet in the JsonSerializable list, or building with PublishTrimmed/AOT where the type isn't pre-generated, or passing a custom IH5_Info-derived type to Serialize.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPay/V3/Universal/Entities/Request/TenPayJsonSerializer.cs:63

    [JsonSerializable(typeof(TenpayV3ProfitShareingRequestData_ReceiverInfo[]))]
    [JsonSerializable(typeof(TenpayV3ProfitShareingAddReceiverRequestData_ReceiverInfo))]
    [JsonSerializable(typeof(TenpayV3ProfitShareing_ReceiverInfo))]
    [JsonSerializable(typeof(StoreInfoJson))]
    [JsonSerializable(typeof(H5InfoIosJson))]
    [JsonSerializable(typeof(H5InfoAndroidJson))]
    [JsonSerializable(typeof(H5InfoWapJson))]
    internal partial class TenPayJsonSerializerContext : JsonSerializerContext
    {
    }

    internal static class TenPayJsonSerializer
    {
        internal static string Serialize<T>(T value)
        {
            var jsonTypeInfo = TenPayJsonSerializerContext.Default.GetTypeInfo(typeof(T));
            if (jsonTypeInfo == null)
            {
                throw new NotSupportedException($"未生成类型 {typeof(T).FullName} 的 JSON 元数据。");
            }

            return JsonSerializer.Serialize(value, jsonTypeInfo);
        }

        internal static string SerializeCustomH5Info(IH5_Info value)
        {
            if (!JsonSerializer.IsReflectionEnabledByDefault)
            {
                throw new NotSupportedException(
                    $"Native AOT 不支持未注册的 H5 场景类型:{value.GetType().FullName}。" +
                    "请使用 H5_Info_IOS、H5_Info_Android 或 H5_Info_WAP。");
            }

#pragma warning disable IL2026
#pragma warning disable IL3050
            return JsonSerializer.Serialize(
                new Dictionary<string, object> { ["h5_info"] = value },

View on GitHub (pinned to be573f6f94)