Skip to content

Commit b355ec2

Browse files
committed
Fixes issue #104 with nested deserialization not passing options
1 parent f07b9b6 commit b355ec2

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

Source/Schema.NET/JsonElementExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ namespace Schema.NET
77

88
internal static class JsonElementExtensions
99
{
10-
public static object ToObject(this JsonElement element, Type objectType)
10+
public static object ToObject(this JsonElement element, Type objectType, JsonSerializerOptions options)
1111
{
1212
// TODO: Investigate avoiding the string allocation
1313
var json = element.GetRawText();
14-
return JsonSerializer.Deserialize(json, objectType);
14+
return JsonSerializer.Deserialize(json, objectType, options);
1515
}
1616
}
1717
}

Source/Schema.NET/ValuesJsonConverter.cs

+17-17
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public override IValues Read(ref Utf8JsonReader reader, Type typeToConvert, Json
5656
if (tokenType == JsonTokenType.StartArray)
5757
{
5858
var unwrappedType = type.GetUnderlyingTypeFromNullable();
59-
argument = ReadJsonArray(token, unwrappedType);
59+
argument = ReadJsonArray(token, unwrappedType, null, options);
6060
}
6161
else
6262
{
63-
argument = ParseTokenArguments(token, tokenType, type);
63+
argument = ParseTokenArguments(token, tokenType, type, options);
6464
}
6565
}
6666
else
@@ -75,7 +75,7 @@ public override IValues Read(ref Utf8JsonReader reader, Type typeToConvert, Json
7575
var type = mainType.GenericTypeArguments[i];
7676
var unwrappedType = type.GetUnderlyingTypeFromNullable();
7777
// only read as many items as there are tokens left
78-
var args = ReadJsonArray(token, unwrappedType, count - total);
78+
var args = ReadJsonArray(token, unwrappedType, count - total, options);
7979

8080
if (args != null && args.Count > 0)
8181
{
@@ -103,7 +103,7 @@ public override IValues Read(ref Utf8JsonReader reader, Type typeToConvert, Json
103103

104104
try
105105
{
106-
var args = ParseTokenArguments(token, tokenType, type);
106+
var args = ParseTokenArguments(token, tokenType, type, options);
107107

108108
if (args != null)
109109
{
@@ -211,7 +211,7 @@ public virtual void WriteObject(Utf8JsonWriter writer, object value, JsonSeriali
211211
JsonSerializer.Serialize(writer, value, value?.GetType(), options);
212212
}
213213

214-
private static object ParseTokenArguments(JsonElement token, JsonTokenType tokenType, Type type)
214+
private static object ParseTokenArguments(JsonElement token, JsonTokenType tokenType, Type type, JsonSerializerOptions options)
215215
{
216216
const string SCHEMA_ORG = "http://schema.org/";
217217
const int SCHEMA_ORG_LENGTH = 18; // equivalent to "http://schema.org/".Length
@@ -230,42 +230,42 @@ private static object ParseTokenArguments(JsonElement token, JsonTokenType token
230230
{
231231
if (tokenType == JsonTokenType.StartObject)
232232
{
233-
args = ParseTokenObjectArguments(token, type, unwrappedType);
233+
args = ParseTokenObjectArguments(token, type, unwrappedType, options);
234234
}
235235
else
236236
{
237-
args = ParseTokenValueArguments(token, tokenType, type, unwrappedType);
237+
args = ParseTokenValueArguments(token, tokenType, type, unwrappedType, options);
238238
}
239239
}
240240

241241
return args;
242242
}
243243

244-
private static object ParseTokenObjectArguments(JsonElement token, Type type, Type unwrappedType)
244+
private static object ParseTokenObjectArguments(JsonElement token, Type type, Type unwrappedType, JsonSerializerOptions options)
245245
{
246246
object args = null;
247247
var typeName = GetTypeNameFromToken(token);
248248
if (string.IsNullOrEmpty(typeName))
249249
{
250-
args = token.ToObject(unwrappedType);
250+
args = token.ToObject(unwrappedType, options);
251251
}
252252
else if (typeName == type.Name)
253253
{
254-
args = token.ToObject(type);
254+
args = token.ToObject(type, options);
255255
}
256256
else
257257
{
258258
var builtType = Type.GetType($"{NamespacePrefix}{typeName}");
259259
if (builtType != null && type.GetTypeInfo().IsAssignableFrom(builtType.GetTypeInfo()))
260260
{
261-
args = token.ToObject(builtType);
261+
args = token.ToObject(builtType, options);
262262
}
263263
}
264264

265265
return args;
266266
}
267267

268-
private static object ParseTokenValueArguments(JsonElement token, JsonTokenType tokenType, Type type, Type unwrappedType)
268+
private static object ParseTokenValueArguments(JsonElement token, JsonTokenType tokenType, Type type, Type unwrappedType, JsonSerializerOptions options)
269269
{
270270
object args = null;
271271
if (unwrappedType.IsPrimitiveType())
@@ -336,7 +336,7 @@ private static object ParseTokenValueArguments(JsonElement token, JsonTokenType
336336
// }
337337
else
338338
{
339-
args = token.ToObject(typeof(object));
339+
args = token.ToObject(typeof(object), options);
340340
}
341341
}
342342
else if (unwrappedType == typeof(decimal))
@@ -426,7 +426,7 @@ private static object ParseTokenValueArguments(JsonElement token, JsonTokenType
426426
{
427427
if (!type.GetTypeInfo().IsInterface && !type.GetTypeInfo().IsClass)
428428
{
429-
args = token.ToObject(classType); // This is expected to throw on some case
429+
args = token.ToObject(classType, options); // This is expected to throw on some case
430430
}
431431
}
432432
}
@@ -451,7 +451,7 @@ private static Type ToClass(Type type)
451451
return type;
452452
}
453453

454-
private static IList ReadJsonArray(JsonElement token, Type type, int? count = null)
454+
private static IList ReadJsonArray(JsonElement token, Type type, int? count, JsonSerializerOptions options)
455455
{
456456
var classType = ToClass(type);
457457
var listType = typeof(List<>).MakeGenericType(type); // always read into list of interfaces
@@ -469,7 +469,7 @@ private static IList ReadJsonArray(JsonElement token, Type type, int? count = nu
469469
var typeName = GetTypeNameFromToken(childToken);
470470
if (string.IsNullOrEmpty(typeName))
471471
{
472-
var child = childToken.ToObject(classType);
472+
var child = childToken.ToObject(classType, options);
473473
var method = listType.GetRuntimeMethod(nameof(List<object>.Add), new[] { classType });
474474

475475
if (method != null)
@@ -484,7 +484,7 @@ private static IList ReadJsonArray(JsonElement token, Type type, int? count = nu
484484
var builtType = Type.GetType($"{NamespacePrefix}{typeName}");
485485
if (builtType != null && type.GetTypeInfo().IsAssignableFrom(builtType.GetTypeInfo()))
486486
{
487-
var child = (Thing)childToken.ToObject(builtType);
487+
var child = (Thing)childToken.ToObject(builtType, options);
488488
var method = listType.GetRuntimeMethod(nameof(List<object>.Add), new[] { classType });
489489

490490
if (method != null)

0 commit comments

Comments
 (0)