// Rename Serializable.DebugView / DebugViewText
// Rename CrossLanguageSettings
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using static Newtonsoft.Json.JsonConvert;
// ReSharper disable once CheckNamespace
namespace System
{
///
/// 用于单元测试输出文本的JSON序列化与反序列化
///
public static class Serializable2
{
static readonly Lazy mDebugViewTextSettings = new Lazy(GetDebugViewTextSettings);
static JsonSerializerSettings GetDebugViewTextSettings() => new JsonSerializerSettings
{
ContractResolver = new IgnoreJsonPropertyContractResolver(),
Converters = new List
{
new StringEnumConverter(),
},
};
///
/// 将忽略 属性
///
sealed class IgnoreJsonPropertyContractResolver : DefaultContractResolver
{
protected override IList CreateProperties(Type type, MemberSerialization memberSerialization)
{
var result = base.CreateProperties(type, memberSerialization);
foreach (var item in result)
{
item.PropertyName = item.UnderlyingName;
}
return result;
}
}
///
/// 序列化 JSON 模型,使用原键名,仅调试使用
///
///
///
public static string S(object? value)
=> SerializeObject(value, Formatting.Indented, mDebugViewTextSettings.Value);
///
/// 反序列化 JSON 模型,使用原键名,仅调试使用
///
///
///
///
[return: MaybeNull]
public static T D(string value)
=> DeserializeObject(value, mDebugViewTextSettings.Value);
}
}