Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NewtonsoftEntityIdJsonConverter #76

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/AspNetCore/Json/NewtonsoftEntityIdJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public void Int64EntityTest()
Assert.Equal(id, id2);
var id3 = JsonConvert.DeserializeObject<MyLongId>("null", settings);
Assert.Null(id3);

var id4 = JsonConvert.DeserializeObject<MyLongId>("10", settings);
Assert.NotNull(id4);
Assert.Equal(10, id4.Id);

Assert.Throws<FormatException>(() => JsonConvert.DeserializeObject<MyLongId>("\"\"", settings));
}

Expand All @@ -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<MyIntId>(json, settings);
Assert.Equal(id, id2);
var id3 = JsonConvert.DeserializeObject<MyIntId>("null", settings);
Assert.Null(id3);
var id4 = JsonConvert.DeserializeObject<MyIntId>("10", settings);
Assert.NotNull(id4);
Assert.Equal(10, id4.Id);

Assert.Throws<FormatException>(() => JsonConvert.DeserializeObject<MyIntId>("\"\"", settings));
}
Expand Down
Loading