【问题标题】:Failed to desirialize the SecurityIdentifier class无法反序列化 SecurityIdentifier 类
【发布时间】:2020-04-26 22:31:45
【问题描述】:

我必须反序列化一个包含 AD 用户的 json 字符串:

[
  {
    "DistinguishedName": "CN=Guest,CN=Users,...",
    "DisplayName": null,
    "FirstName": null,
    "LastName": null,
    "SamAccountName": "Guest",
    "ObjectGuid": "some guild...",
    "Mail": null,
    "IsEnabled": false,
    "SecurityIdentifier": {
      "BinaryLength": 28,
      "AccountDomainSid": {
        "BinaryLength": 24,
        "Value": "some value"
      },
      "Value": "some value"
    },
    ...
    }
  },
  ...
  ]

我的班级看起来像:

  public class MyClass
    {
        public string DistinguishedName { get; set; }
        public string DisplayName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public SecurityIdentifier SecurityIdentifier { get; set; }
        ...
    }

反序列化

var result = JsonSerializer.Deserialize<IEnumerable<MyClass>>(json);

在我尝试反序列化 SecurityIdentifier 字段之前工作正常。

没有无参数构造函数的引用类型的反序列化 不支持。输入“System.Security.Principal.SecurityIdentifier”

有办法解决这个问题吗?非常感谢!

【问题讨论】:

  • 我认为问题出在没有无参数构造函数的SecurityIdentifier类中,不支持没有无参数构造函数的引用类型的反序列化。
  • 是的,似乎是。但是 SecurityIdentifier 类是密封的,我不能从它继承来使用默认构造函数创建自己的类。
  • 您可以尝试为SecurityIdentifier 创建一个自定义JsonConverter

标签: c# .net json


【解决方案1】:

按照this post 中的建议,您可以尝试切换到 NewtonSoft 来反序列化您的 json。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    相关资源
    最近更新 更多