-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
JsonSerializer.Helpers.cs
132 lines (111 loc) · 5.34 KB
/
JsonSerializer.Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json
{
public static partial class JsonSerializer
{
internal const string SerializationUnreferencedCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.";
internal const string SerializationRequiresDynamicCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.";
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
private static JsonTypeInfo GetTypeInfo(JsonSerializerOptions? options, Type inputType, bool fallBackToNearestAncestorType = false)
{
Debug.Assert(inputType != null);
options ??= JsonSerializerOptions.Default;
if (!options.IsInitializedForReflectionSerializer)
{
options.InitializeForReflectionSerializer();
}
// In order to improve performance of polymorphic root-level object serialization,
// we bypass GetTypeInfoForRootType and cache JsonTypeInfo<object> in a dedicated property.
// This lets any derived types take advantage of the cache in GetTypeInfoForRootType themselves.
return inputType == JsonTypeInfo.ObjectType
? options.ObjectTypeInfo
: options.GetTypeInfoForRootType(inputType, fallBackToNearestAncestorType);
}
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
private static JsonTypeInfo<T> GetTypeInfo<T>(JsonSerializerOptions? options)
=> (JsonTypeInfo<T>)GetTypeInfo(options, typeof(T));
private static JsonTypeInfo GetTypeInfo(JsonSerializerContext context, Type inputType)
{
Debug.Assert(context != null);
Debug.Assert(inputType != null);
JsonTypeInfo? info = context.GetTypeInfo(inputType);
if (info is null)
{
ThrowHelper.ThrowInvalidOperationException_NoMetadataForType(inputType, context);
}
info.EnsureConfigured();
return info;
}
private static void ValidateInputType(object? value, Type inputType)
{
if (inputType is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(inputType));
}
if (value is not null)
{
Type runtimeType = value.GetType();
if (!inputType.IsAssignableFrom(runtimeType))
{
ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value);
}
}
}
internal static bool IsValidNumberHandlingValue(JsonNumberHandling handling) =>
JsonHelpers.IsInRangeInclusive((int)handling, 0,
(int)(
JsonNumberHandling.Strict |
JsonNumberHandling.AllowReadingFromString |
JsonNumberHandling.WriteAsString |
JsonNumberHandling.AllowNamedFloatingPointLiterals));
internal static bool IsValidUnmappedMemberHandlingValue(JsonUnmappedMemberHandling handling) =>
handling is JsonUnmappedMemberHandling.Skip or JsonUnmappedMemberHandling.Disallow;
[return: NotNullIfNotNull(nameof(value))]
internal static T? UnboxOnRead<T>(object? value)
{
if (value is null)
{
if (default(T) is not null)
{
// Casting null values to a non-nullable struct throws NullReferenceException.
ThrowUnableToCastValue(value);
}
return default;
}
if (value is T typedValue)
{
return typedValue;
}
ThrowUnableToCastValue(value);
return default!;
static void ThrowUnableToCastValue(object? value)
{
if (value is null)
{
ThrowHelper.ThrowInvalidOperationException_DeserializeUnableToAssignNull(declaredType: typeof(T));
}
else
{
ThrowHelper.ThrowInvalidCastException_DeserializeUnableToAssignValue(typeOfValue: value.GetType(), declaredType: typeof(T));
}
}
}
[return: NotNullIfNotNull(nameof(value))]
internal static T? UnboxOnWrite<T>(object? value)
{
if (default(T) is not null && value is null)
{
// Casting null values to a non-nullable struct throws NullReferenceException.
ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(typeof(T));
}
return (T?)value;
}
}
}