如何解决使用嵌套对象和中间步骤进行反序列化
我正在尝试使用 System.Text.Json JsonSerializer 序列化/反序列化一个对象。 我的容器对象是一个“LicenseFile”,它包含一个“License”对象和一个 byte[] 数字签名。
public class LicenseFile
{
public License License { get; set; }
public byte[] Signature { get; set; }
}
public class License
{
public string ProductName { get; set; }
public string ProductVersion { get; set; }
}
在序列化 LicenseFile 时,我还想先将 License 值转换为 JSON,然后再转换为 Base64。 为此,我创建了一个自定义 JSON 转换器,例如
public class LicenseFileConverter : JsonConverter<LicenseFile>
{
public override void Write(Utf8JsonWriter writer,LicenseFile licenseFile,JsonSerializerOptions options)
{
writer.WriteStartObject();
var json = JsonSerializer.Serialize(licenseFile.License);
byte[] jsonBytes = new UTF8Encoding().GetBytes(json);
writer.WriteBase64String("License",jsonBytes);
writer.WriteBase64String("Signature",licenseFile.Signature);
writer.WriteEndObject();
writer.Flush();
}
}
我想最终得到这样的 JSON 输出:
{
"License": "BASE64_OF_LICENSE_OBJECT_JSON'D","Signature": "BASE64_OF_SIGNATURE_BYTE[]"
}
我的问题:
- 这是一个好方法吗?我最好只使用辅助方法先序列化值,对它们进行 base64,然后将它们写出到文件中吗?
- 如何将 JSON 对象再次反序列化回对象(同时对它们进行 debase64 处理)
感谢您的任何建议!
解决方法
我推荐:
- 一个专门的Dto;或
-
#
上的[JsonIgnore]
和一个辅助字段;
例如:
License
或
public class LicenseFile
{
[JsonIgnore]
public License License { get; set; }
public string LicenseJson => JsonSerializer.Serialize(License); // Add base64 encoding if you wish
public byte[] Signature { get; set; }
}
测试
public class LicenseFileDto
{
public LicenseFileDto(LicenseFile license) {
License = JsonSerializer.Serialize(license.License);
Signature = license.Signature;
}
public string License { get; }
public byte[] Signature { get; }
}
var l = new LicenseFile
{
License = new License(name: "X Y",validUntil: new DateTime(2021,04,22)),Signature = new byte[] { 1,2,3 }
};
var json = JsonSerializer.Serialize(l,options: new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);
var dtoJson = JsonSerializer.Serialize(new LicenseFileDto(l),options: new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(dtoJson);
// Documentation at:
// https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-encoding#serialize-all-characters
// says:
// Use the unsafe encoder only when it's known that the client will be interpreting the resulting payload as UTF-8 encoded JSON.
var prettyJson = JsonSerializer.Serialize(new LicenseFileDto(l),options: new JsonSerializerOptions { WriteIndented = true,Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
Console.WriteLine(prettyJson);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。