Skip to content

Commit 6d3745b

Browse files
committed
Fixed crash when using System.Text.Json v6.0
1 parent ce07172 commit 6d3745b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/JsonApiDotNetCore/Middleware/TraceLogWriter.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Reflection;
23
using System.Runtime.CompilerServices;
34
using System.Text;
@@ -50,17 +51,22 @@ public override bool CanConvert(Type typeToConvert)
5051

5152
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
5253
{
53-
return new ResourceFieldInTraceJsonConverter();
54+
Type objectType = typeof(ResourceFieldInTraceJsonConverter<>);
55+
Type converterType = objectType.MakeGenericType(typeToConvert);
56+
57+
return (JsonConverter)Activator.CreateInstance(converterType, BindingFlags.Instance | BindingFlags.Public, null, null,
58+
CultureInfo.InvariantCulture)!;
5459
}
5560

56-
private sealed class ResourceFieldInTraceJsonConverter : JsonConverter<ResourceFieldAttribute>
61+
private sealed class ResourceFieldInTraceJsonConverter<TField> : JsonConverter<TField>
62+
where TField : ResourceFieldAttribute
5763
{
58-
public override ResourceFieldAttribute Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
64+
public override TField Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
5965
{
6066
throw new NotSupportedException();
6167
}
6268

63-
public override void Write(Utf8JsonWriter writer, ResourceFieldAttribute value, JsonSerializerOptions options)
69+
public override void Write(Utf8JsonWriter writer, TField value, JsonSerializerOptions options)
6470
{
6571
writer.WriteStringValue(value.PublicName);
6672
}

src/JsonApiDotNetCore/Serialization/JsonConverters/SingleOrManyDataConverterFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Reflection;
23
using System.Text.Json;
34
using System.Text.Json.Serialization;
@@ -24,7 +25,7 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
2425
Type objectType = typeToConvert.GetGenericArguments()[0];
2526
Type converterType = typeof(SingleOrManyDataConverter<>).MakeGenericType(objectType);
2627

27-
return (JsonConverter)Activator.CreateInstance(converterType, BindingFlags.Instance | BindingFlags.Public, null, null, null)!;
28+
return (JsonConverter)Activator.CreateInstance(converterType, BindingFlags.Instance | BindingFlags.Public, null, null, CultureInfo.InvariantCulture)!;
2829
}
2930

3031
private sealed class SingleOrManyDataConverter<T> : JsonObjectConverter<SingleOrManyData<T>>

0 commit comments

Comments
 (0)