mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-04-05 17:37:54 +08:00
feat(core): 新增若干 bool 非标准化 JsonConvert
This commit is contained in:
parent
09652ed279
commit
94d14f83c5
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class NumberTypedBooleanConverter : JsonConverter<bool>
|
||||
{
|
||||
private readonly JsonConverter<bool?> _converter = new NumberTypedNullableBooleanConverter();
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool ReadJson(JsonReader reader, Type objectType, bool existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
return _converter.ReadJson(reader, objectType, existingValue, hasExistingValue, serializer) ?? default;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, bool value, JsonSerializer serializer)
|
||||
{
|
||||
_converter.WriteJson(writer, value, serializer);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class NumberTypedNullableBooleanConverter : JsonConverter<bool?>
|
||||
{
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool? ReadJson(JsonReader reader, Type objectType, bool? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return existingValue;
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.Boolean)
|
||||
{
|
||||
return serializer.Deserialize<bool>(reader);
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.Integer)
|
||||
{
|
||||
int value = serializer.Deserialize<int>(reader);
|
||||
return Convert.ToBoolean(value);
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
string? str = serializer.Deserialize<string>(reader);
|
||||
if (str == null)
|
||||
return existingValue;
|
||||
|
||||
if (int.TryParse(str, out int value))
|
||||
return Convert.ToBoolean(value);
|
||||
}
|
||||
|
||||
throw new JsonReaderException();
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, bool? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value.HasValue)
|
||||
writer.WriteValue(value.Value ? 1 : 0);
|
||||
else
|
||||
writer.WriteNull();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class NumberTypedBooleanConverter : JsonConverter<bool>
|
||||
{
|
||||
private readonly JsonConverter<bool?> _converter = new NumberTypedNullableBooleanConverter();
|
||||
|
||||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return _converter.Read(ref reader, typeToConvert, options) ?? default;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
|
||||
{
|
||||
_converter.Write(writer, value, options);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class NumberTypedNullableBooleanConverter : JsonConverter<bool?>
|
||||
{
|
||||
public override bool? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.True)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.False)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.Number)
|
||||
{
|
||||
if (reader.TryGetInt32(out int value))
|
||||
return Convert.ToBoolean(value);
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
string? str = reader.GetString();
|
||||
if (str == null)
|
||||
return null;
|
||||
|
||||
if (int.TryParse(str, out int value))
|
||||
return Convert.ToBoolean(value);
|
||||
}
|
||||
|
||||
throw new JsonException();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, bool? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value.HasValue)
|
||||
writer.WriteNumberValue(value.Value ? 1 : 0);
|
||||
else
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user