From 7c966a3f2a685219ed9b96142f4a4db64f06a0f6 Mon Sep 17 00:00:00 2001 From: witskeeper Date: Mon, 9 Sep 2024 19:51:48 +0800 Subject: [PATCH] fix NewtonsoftEntityIdJsonConverter --- .../Json/NewtonsoftEntityIdJsonConverter.cs | 53 ++++++++++--------- .../NewtonsoftEntityIdJsonConverterTests.cs | 10 +++- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/AspNetCore/Json/NewtonsoftEntityIdJsonConverter.cs b/src/AspNetCore/Json/NewtonsoftEntityIdJsonConverter.cs index c7d039f4..b2a66fa1 100644 --- a/src/AspNetCore/Json/NewtonsoftEntityIdJsonConverter.cs +++ b/src/AspNetCore/Json/NewtonsoftEntityIdJsonConverter.cs @@ -12,6 +12,8 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer { if (value is null) writer.WriteNull(); + else if (value is IInt32StronglyTypedId int32Id) + writer.WriteValue(int32Id.Id); else writer.WriteValue(value.ToString()); } @@ -20,39 +22,38 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer JsonSerializer serializer) { object? v = default; - if (reader.TokenType == JsonToken.String) - { - var value = reader.Value?.ToString(); - if (value != null) + var value = reader.Value?.ToString(); + + if (value != null) + { + if (!_ConstructorInfo.TryGetValue(objectType, out var _constructorInfo)) { - if (!_ConstructorInfo.TryGetValue(objectType, out var _constructorInfo)) - { - _constructorInfo = objectType.GetConstructors()[0]; - _ConstructorInfo.Add(objectType, _constructorInfo); - } + _constructorInfo = objectType.GetConstructors()[0]; + _ConstructorInfo.Add(objectType, _constructorInfo); + } - Type parameterType = _constructorInfo.GetParameters()[0].ParameterType; + Type parameterType = _constructorInfo.GetParameters()[0].ParameterType; - if (parameterType == typeof(long)) - { - v = _constructorInfo.Invoke(new object[] { long.Parse(value) }); - } - else if (parameterType == typeof(int)) - { - v = _constructorInfo.Invoke(new object[] { int.Parse(value) }); - } - else if (parameterType == typeof(string)) - { - v = _constructorInfo.Invoke(new object[] { value }); - } - else if (parameterType == typeof(Guid)) - { - v = _constructorInfo.Invoke(new object[] { Guid.Parse(value) }); - } + if (parameterType == typeof(long)) + { + v = _constructorInfo.Invoke(new object[] { long.Parse(value) }); + } + else if (parameterType == typeof(int)) + { + v = _constructorInfo.Invoke(new object[] { int.Parse(value) }); + } + else if (parameterType == typeof(string)) + { + v = _constructorInfo.Invoke(new object[] { value }); + } + else if (parameterType == typeof(Guid)) + { + v = _constructorInfo.Invoke(new object[] { Guid.Parse(value) }); } } + return v; } diff --git a/test/NetCorePal.Extensions.AspNetCore.UnitTests/NewtonsoftEntityIdJsonConverterTests.cs b/test/NetCorePal.Extensions.AspNetCore.UnitTests/NewtonsoftEntityIdJsonConverterTests.cs index f83fad0e..675b71fd 100644 --- a/test/NetCorePal.Extensions.AspNetCore.UnitTests/NewtonsoftEntityIdJsonConverterTests.cs +++ b/test/NetCorePal.Extensions.AspNetCore.UnitTests/NewtonsoftEntityIdJsonConverterTests.cs @@ -38,7 +38,10 @@ public void Int64EntityTest() Assert.Equal(id, id2); var id3 = JsonConvert.DeserializeObject("null", settings); Assert.Null(id3); - + var id4 = JsonConvert.DeserializeObject("10", settings); + Assert.NotNull(id4); + Assert.Equal(10, id4.Id); + Assert.Throws(() => JsonConvert.DeserializeObject("\"\"", settings)); } @@ -49,11 +52,14 @@ public void Int32EntityTest() settings.Converters.Add(new NewtonsoftEntityIdJsonConverter()); var id = new MyIntId(1); var json = JsonConvert.SerializeObject(id, settings); - Assert.Equal("\"1\"", json); + Assert.Equal("1", json); var id2 = JsonConvert.DeserializeObject(json, settings); Assert.Equal(id, id2); var id3 = JsonConvert.DeserializeObject("null", settings); Assert.Null(id3); + var id4 = JsonConvert.DeserializeObject("10", settings); + Assert.NotNull(id4); + Assert.Equal(10, id4.Id); Assert.Throws(() => JsonConvert.DeserializeObject("\"\"", settings)); }