/// <summary> /// 将 [JSON 序列化程式实现种类] 转换为 [序列化程式实现种类] /// </summary> /// <param name="value"></param> /// <returns></returns> public static ImplType Convert(this JsonImplType value) { return(value switch { JsonImplType.SystemTextJson => ImplType.SystemTextJson, _ => ImplType.NewtonsoftJson, });
/// <summary> /// (Serialize)JSON 序列化 /// </summary> /// <param name="implType"></param> /// <param name="value"></param> /// <param name="inputType"></param> /// <param name="writeIndented"></param> /// <param name="ignoreNullValues"></param> /// <returns></returns> public static string SJSON(JsonImplType implType, object?value, Type?inputType = null, bool writeIndented = false, bool ignoreNullValues = false) { switch (implType) { case JsonImplType.SystemTextJson: var options = new SJsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, WriteIndented = writeIndented, IgnoreNullValues = ignoreNullValues }; return(SJsonSerializer.Serialize(value, inputType ?? value?.GetType() ?? typeof(object), options)); default: #if NOT_NJSON throw new NotSupportedException(); #else var formatting = writeIndented ? Formatting.Indented : Formatting.None; var settings = ignoreNullValues ? new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore } : null; return(SerializeObject(value, inputType, formatting, settings)); #endif } }
public static T DJSON <T>(JsonImplType implType, string value) { return(implType switch { JsonImplType.SystemTextJson => SJsonSerializer.Deserialize <T>(value), _ => DeserializeObject <T>(value), });
public static T DJSON <T>(JsonImplType implType, string value) { return(implType switch { JsonImplType.SystemTextJson => SJsonSerializer.Deserialize <T>(value), _ => #if NOT_NJSON throw new NotSupportedException(), #else DeserializeObject <T>(value), #endif });
/// <summary> /// 将 [序列化程式实现种类] 转换为 [JSON 序列化程式实现种类] /// </summary> /// <param name="enum"></param> /// <param name="value"></param> /// <returns></returns> public static bool TryConvert(this ImplType @enum, out JsonImplType value) { switch (@enum) { case ImplType.NewtonsoftJson: value = JsonImplType.NewtonsoftJson; return(true); case ImplType.SystemTextJson: value = JsonImplType.SystemTextJson; return(true); default: value = default; return(false); } }