WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 8d8b91b

Browse files
committed
debug LongToStringConverter
1 parent e29d1b1 commit 8d8b91b

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/OSharp.Utils/Json/LongToStringConverter.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,26 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
3939
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
4040
JsonSerializer serializer)
4141
{
42+
if (reader.TokenType == JsonToken.Null)
43+
{
44+
// 如果是可空类型,返回null;否则抛出异常
45+
if (Nullable.GetUnderlyingType(objectType) != null)
46+
{
47+
return null;
48+
}
49+
throw new JsonSerializationException($"Cannot convert null value to {objectType}.");
50+
}
51+
4252
var jt = JToken.ReadFrom(reader);
53+
// 处理空字符串的情况
54+
if (jt.Type == JTokenType.String && string.IsNullOrEmpty(jt.Value<string>()))
55+
{
56+
if (Nullable.GetUnderlyingType(objectType) != null)
57+
{
58+
return null;
59+
}
60+
throw new JsonSerializationException($"Cannot convert empty string to {objectType}.");
61+
}
4362
var value = jt.Value<long>();
4463
return value;
4564
}
@@ -53,7 +72,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
5372
/// </returns>
5473
public override bool CanConvert(Type objectType)
5574
{
56-
return objectType == typeof(long) || objectType == typeof(ulong);
75+
return objectType == typeof(long?) || objectType == typeof(long) || objectType == typeof(ulong);
5776
}
5877
}
5978

0 commit comments

Comments
 (0)