| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Reflection; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Metadata |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a strongly-typed property to prevent boxing and to create a direct delegate to the getter\setter. |
| | | 13 | | /// </summary> |
| | | 14 | | internal sealed class JsonPropertyInfo<T> : JsonPropertyInfo |
| | | 15 | | { |
| | | 16 | | private Func<object, T>? _typedGet; |
| | | 17 | | private Action<object, T>? _typedSet; |
| | | 18 | | |
| | | 19 | | internal JsonPropertyInfo(Type declaringType, JsonTypeInfo? declaringTypeInfo, JsonSerializerOptions options) |
| | 20048 | 20 | | : base(declaringType, propertyType: typeof(T), declaringTypeInfo, options) |
| | 20048 | 21 | | { |
| | 20048 | 22 | | } |
| | | 23 | | |
| | | 24 | | internal new Func<object, T>? Get |
| | | 25 | | { |
| | 0 | 26 | | get => _typedGet; |
| | 7794 | 27 | | set => SetGetter(value); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | internal new Action<object, T>? Set |
| | | 31 | | { |
| | 0 | 32 | | get => _typedSet; |
| | 7794 | 33 | | set => SetSetter(value); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | private protected override void SetGetter(Delegate? getter) |
| | 7794 | 37 | | { |
| | 7794 | 38 | | Debug.Assert(getter is null or Func<object, object?> or Func<object, T>); |
| | 7794 | 39 | | Debug.Assert(!IsConfigured); |
| | | 40 | | |
| | 7794 | 41 | | if (getter is null) |
| | 0 | 42 | | { |
| | 0 | 43 | | _typedGet = null; |
| | 0 | 44 | | _untypedGet = null; |
| | 0 | 45 | | } |
| | 7794 | 46 | | else if (getter is Func<object, T> typedGetter) |
| | 7794 | 47 | | { |
| | 7794 | 48 | | _typedGet = typedGetter; |
| | 7794 | 49 | | _untypedGet = getter is Func<object, object?> untypedGet ? untypedGet : obj => typedGetter(obj); |
| | 7794 | 50 | | } |
| | | 51 | | else |
| | 0 | 52 | | { |
| | 0 | 53 | | Func<object, object?> untypedGet = (Func<object, object?>)getter; |
| | 0 | 54 | | _typedGet = (obj => (T)untypedGet(obj)!); |
| | 0 | 55 | | _untypedGet = untypedGet; |
| | 0 | 56 | | } |
| | 7794 | 57 | | } |
| | | 58 | | |
| | | 59 | | private protected override void SetSetter(Delegate? setter) |
| | 7794 | 60 | | { |
| | 7794 | 61 | | Debug.Assert(setter is null or Action<object, object?> or Action<object, T>); |
| | 7794 | 62 | | Debug.Assert(!IsConfigured); |
| | | 63 | | |
| | 7794 | 64 | | if (setter is null) |
| | 0 | 65 | | { |
| | 0 | 66 | | _typedSet = null; |
| | 0 | 67 | | _untypedSet = null; |
| | 0 | 68 | | } |
| | 7794 | 69 | | else if (setter is Action<object, T> typedSetter) |
| | 7794 | 70 | | { |
| | 7794 | 71 | | _typedSet = typedSetter; |
| | 7794 | 72 | | _untypedSet = setter is Action<object, object?> untypedSet ? untypedSet : (obj, value) => typedSetter(ob |
| | 7794 | 73 | | } |
| | | 74 | | else |
| | 0 | 75 | | { |
| | 0 | 76 | | Action<object, object?> untypedSet = (Action<object, object?>)setter; |
| | 0 | 77 | | _typedSet = ((obj, value) => untypedSet(obj, value)); |
| | 0 | 78 | | _untypedSet = untypedSet; |
| | 0 | 79 | | } |
| | 7794 | 80 | | } |
| | | 81 | | |
| | | 82 | | internal new Func<object, T?, bool>? ShouldSerialize |
| | | 83 | | { |
| | 0 | 84 | | get => _shouldSerializeTyped; |
| | 0 | 85 | | set => SetShouldSerialize(value); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private Func<object, T?, bool>? _shouldSerializeTyped; |
| | | 89 | | |
| | | 90 | | private protected override void SetShouldSerialize(Delegate? predicate) |
| | 0 | 91 | | { |
| | 0 | 92 | | Debug.Assert(predicate is null or Func<object, object?, bool> or Func<object, T?, bool>); |
| | 0 | 93 | | Debug.Assert(!IsConfigured); |
| | | 94 | | |
| | 0 | 95 | | if (predicate is null) |
| | 0 | 96 | | { |
| | 0 | 97 | | _shouldSerializeTyped = null; |
| | 0 | 98 | | _shouldSerialize = null; |
| | 0 | 99 | | } |
| | 0 | 100 | | else if (predicate is Func<object, T?, bool> typedPredicate) |
| | 0 | 101 | | { |
| | 0 | 102 | | _shouldSerializeTyped = typedPredicate; |
| | 0 | 103 | | _shouldSerialize = typedPredicate is Func<object, object?, bool> untypedPredicate ? untypedPredicate : ( |
| | 0 | 104 | | } |
| | | 105 | | else |
| | 0 | 106 | | { |
| | 0 | 107 | | Func<object, object?, bool> untypedPredicate = (Func<object, object?, bool>)predicate; |
| | 0 | 108 | | _shouldSerializeTyped = (obj, value) => untypedPredicate(obj, value); |
| | 0 | 109 | | _shouldSerialize = untypedPredicate; |
| | 0 | 110 | | } |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | internal override object? DefaultValue => default(T); |
| | 33162 | 114 | | internal override bool PropertyTypeCanBeNull => default(T) is null; |
| | | 115 | | internal override void AddJsonParameterInfo(JsonParameterInfoValues parameterInfoValues) |
| | 4494 | 116 | | { |
| | 4494 | 117 | | Debug.Assert(!IsConfigured); |
| | 4494 | 118 | | Debug.Assert(AssociatedParameter is null); |
| | | 119 | | |
| | 4494 | 120 | | AssociatedParameter = new JsonParameterInfo<T>(parameterInfoValues, this); |
| | | 121 | | // Overwrite the nullability annotation of property setter with the parameter. |
| | 4494 | 122 | | _isSetNullable = parameterInfoValues.IsNullable; |
| | | 123 | | |
| | 4494 | 124 | | if (Options.RespectRequiredConstructorParameters) |
| | 0 | 125 | | { |
| | | 126 | | // If the property has been associated with a non-optional parameter, mark it as required. |
| | 0 | 127 | | _isRequired |= AssociatedParameter.IsRequiredParameter; |
| | 0 | 128 | | } |
| | 4494 | 129 | | } |
| | | 130 | | |
| | | 131 | | internal new JsonConverter<T> EffectiveConverter |
| | | 132 | | { |
| | | 133 | | get |
| | 0 | 134 | | { |
| | 0 | 135 | | Debug.Assert(_typedEffectiveConverter != null); |
| | 0 | 136 | | return _typedEffectiveConverter; |
| | 0 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private JsonConverter<T>? _typedEffectiveConverter; |
| | | 141 | | |
| | | 142 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 143 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 144 | | internal override void DetermineReflectionPropertyAccessors(MemberInfo memberInfo, bool useNonPublicAccessors) |
| | 7794 | 145 | | => DefaultJsonTypeInfoResolver.DeterminePropertyAccessors<T>(this, memberInfo, useNonPublicAccessors); |
| | | 146 | | |
| | | 147 | | private protected override void DetermineEffectiveConverter(JsonTypeInfo jsonTypeInfo) |
| | 20048 | 148 | | { |
| | 20048 | 149 | | Debug.Assert(jsonTypeInfo is JsonTypeInfo<T>); |
| | | 150 | | |
| | 20048 | 151 | | JsonConverter<T> converter = |
| | 20048 | 152 | | Options.ExpandConverterFactory(CustomConverter, PropertyType) // Expand any property-level custom conver |
| | 20048 | 153 | | ?.CreateCastingConverter<T>() // Cast to JsonConverter<T>, potentially w |
| | 20048 | 154 | | ?? ((JsonTypeInfo<T>)jsonTypeInfo).EffectiveConverter; // Fall back to the effective converter fo |
| | | 155 | | |
| | 20048 | 156 | | _effectiveConverter = converter; |
| | 20048 | 157 | | _typedEffectiveConverter = converter; |
| | 20048 | 158 | | } |
| | | 159 | | |
| | | 160 | | internal override object? GetValueAsObject(object obj) |
| | 0 | 161 | | { |
| | 0 | 162 | | if (IsForTypeInfo) |
| | 0 | 163 | | { |
| | 0 | 164 | | return obj; |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | Debug.Assert(HasGetter); |
| | 0 | 168 | | return Get!(obj); |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | internal override bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer) |
| | 0 | 172 | | { |
| | 0 | 173 | | T value = Get!(obj); |
| | | 174 | | |
| | 0 | 175 | | if ( |
| | 0 | 176 | | #if NET |
| | 0 | 177 | | !typeof(T).IsValueType && // treated as a constant by recent versions of the JIT. |
| | 0 | 178 | | #else |
| | 0 | 179 | | !EffectiveConverter.IsValueType && |
| | 0 | 180 | | #endif |
| | 0 | 181 | | Options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.IgnoreCycles && |
| | 0 | 182 | | value is not null && |
| | 0 | 183 | | !state.IsContinuation && |
| | 0 | 184 | | // .NET types that are serialized as JSON primitive values don't need to be tracked for cycle detection |
| | 0 | 185 | | EffectiveConverter.ConverterStrategy != ConverterStrategy.Value && |
| | 0 | 186 | | state.ReferenceResolver.ContainsReferenceForCycleDetection(value)) |
| | 0 | 187 | | { |
| | | 188 | | // If a reference cycle is detected, treat value as null. |
| | 0 | 189 | | value = default!; |
| | 0 | 190 | | Debug.Assert(value == null); |
| | 0 | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | if (IgnoreDefaultValuesOnWrite) |
| | 0 | 194 | | { |
| | | 195 | | // Fast path `ShouldSerialize` check when using JsonIgnoreCondition.WhenWritingNull/Default configuratio |
| | 0 | 196 | | if (IsDefaultValue(value)) |
| | 0 | 197 | | { |
| | 0 | 198 | | return true; |
| | | 199 | | } |
| | 0 | 200 | | } |
| | 0 | 201 | | else if (ShouldSerialize?.Invoke(obj, value) == false) |
| | 0 | 202 | | { |
| | | 203 | | // We return true here. |
| | | 204 | | // False means that there is not enough data. |
| | 0 | 205 | | return true; |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | if (value is null) |
| | 0 | 209 | | { |
| | 0 | 210 | | Debug.Assert(PropertyTypeCanBeNull); |
| | | 211 | | |
| | 0 | 212 | | if (!IsGetNullable && Options.RespectNullableAnnotations) |
| | 0 | 213 | | { |
| | 0 | 214 | | ThrowHelper.ThrowJsonException_PropertyGetterDisallowNull(Name, state.Current.JsonTypeInfo.Type); |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | if (EffectiveConverter.HandleNullOnWrite) |
| | 0 | 218 | | { |
| | 0 | 219 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 220 | | { |
| | 0 | 221 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | 0 | 222 | | writer.WritePropertyNameSection(EscapedNameSection); |
| | 0 | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | int originalDepth = writer.CurrentDepth; |
| | 0 | 226 | | EffectiveConverter.Write(writer, value, Options); |
| | 0 | 227 | | if (originalDepth != writer.CurrentDepth) |
| | 0 | 228 | | { |
| | 0 | 229 | | ThrowHelper.ThrowJsonException_SerializationConverterWrite(EffectiveConverter); |
| | | 230 | | } |
| | 0 | 231 | | } |
| | | 232 | | else |
| | 0 | 233 | | { |
| | 0 | 234 | | writer.WriteNullSection(EscapedNameSection); |
| | 0 | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | return true; |
| | | 238 | | } |
| | | 239 | | else |
| | 0 | 240 | | { |
| | 0 | 241 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 242 | | { |
| | 0 | 243 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | 0 | 244 | | writer.WritePropertyNameSection(EscapedNameSection); |
| | 0 | 245 | | } |
| | | 246 | | |
| | 0 | 247 | | return EffectiveConverter.TryWrite(writer, value, Options, ref state); |
| | | 248 | | } |
| | 0 | 249 | | } |
| | | 250 | | |
| | | 251 | | internal override bool GetMemberAndWriteJsonExtensionData(object obj, ref WriteStack state, Utf8JsonWriter write |
| | 0 | 252 | | { |
| | | 253 | | bool success; |
| | 0 | 254 | | T value = Get!(obj); |
| | | 255 | | |
| | 0 | 256 | | if (ShouldSerialize?.Invoke(obj, value) == false) |
| | 0 | 257 | | { |
| | | 258 | | // We return true here. |
| | | 259 | | // False means that there is not enough data. |
| | 0 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | if (value == null) |
| | 0 | 264 | | { |
| | 0 | 265 | | success = true; |
| | 0 | 266 | | } |
| | | 267 | | else |
| | 0 | 268 | | { |
| | 0 | 269 | | success = EffectiveConverter.TryWriteDataExtensionProperty(writer, value, Options, ref state); |
| | 0 | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | return success; |
| | 0 | 273 | | } |
| | | 274 | | |
| | | 275 | | internal override bool ReadJsonAndSetMember(object obj, scoped ref ReadStack state, ref Utf8JsonReader reader) |
| | 0 | 276 | | { |
| | | 277 | | bool success; |
| | | 278 | | |
| | 0 | 279 | | bool isNullToken = reader.TokenType == JsonTokenType.Null; |
| | | 280 | | |
| | 0 | 281 | | if (isNullToken && !EffectiveConverter.HandleNullOnRead && !state.IsContinuation) |
| | 0 | 282 | | { |
| | 0 | 283 | | if (default(T) is not null || !CanDeserialize) |
| | 0 | 284 | | { |
| | 0 | 285 | | if (default(T) is null) |
| | 0 | 286 | | { |
| | 0 | 287 | | Debug.Assert(CanDeserialize || EffectiveObjectCreationHandling == JsonObjectCreationHandling.Pop |
| | 0 | 288 | | ThrowHelper.ThrowInvalidOperationException_DeserializeUnableToAssignNull(EffectiveConverter.Type |
| | | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(EffectiveConverter.Type); |
| | | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | if (!IgnoreNullTokensOnRead) |
| | 0 | 295 | | { |
| | 0 | 296 | | if (!IsSetNullable && Options.RespectNullableAnnotations) |
| | 0 | 297 | | { |
| | 0 | 298 | | ThrowHelper.ThrowJsonException_PropertySetterDisallowNull(Name, state.Current.JsonTypeInfo.Type) |
| | | 299 | | } |
| | | 300 | | |
| | 0 | 301 | | T? value = default; |
| | 0 | 302 | | Set!(obj, value!); |
| | 0 | 303 | | } |
| | | 304 | | |
| | 0 | 305 | | success = true; |
| | 0 | 306 | | state.Current.MarkPropertyAsRead(this); |
| | 0 | 307 | | } |
| | 0 | 308 | | else if (EffectiveConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null) |
| | 0 | 309 | | { |
| | | 310 | | // CanUseDirectReadOrWrite == false when using streams |
| | 0 | 311 | | Debug.Assert(!state.IsContinuation); |
| | 0 | 312 | | Debug.Assert(EffectiveObjectCreationHandling != JsonObjectCreationHandling.Populate, "Populating should |
| | | 313 | | |
| | 0 | 314 | | if (!isNullToken || !IgnoreNullTokensOnRead || default(T) is not null) |
| | 0 | 315 | | { |
| | | 316 | | // Optimize for internal converters by avoiding the extra call to TryRead. |
| | 0 | 317 | | T? fastValue = EffectiveConverter.Read(ref reader, PropertyType, Options); |
| | | 318 | | |
| | 0 | 319 | | if (fastValue is null && !IsSetNullable && Options.RespectNullableAnnotations) |
| | 0 | 320 | | { |
| | 0 | 321 | | ThrowHelper.ThrowJsonException_PropertySetterDisallowNull(Name, state.Current.JsonTypeInfo.Type) |
| | | 322 | | } |
| | | 323 | | |
| | 0 | 324 | | Set!(obj, fastValue!); |
| | 0 | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | success = true; |
| | 0 | 328 | | state.Current.MarkPropertyAsRead(this); |
| | 0 | 329 | | } |
| | | 330 | | else |
| | 0 | 331 | | { |
| | 0 | 332 | | success = true; |
| | 0 | 333 | | if (!isNullToken || !IgnoreNullTokensOnRead || default(T) is not null || state.IsContinuation) |
| | 0 | 334 | | { |
| | 0 | 335 | | state.Current.ReturnValue = obj; |
| | | 336 | | |
| | 0 | 337 | | success = EffectiveConverter.TryRead(ref reader, PropertyType, Options, ref state, out T? value, out |
| | 0 | 338 | | if (success) |
| | 0 | 339 | | { |
| | 0 | 340 | | if (typeof(T).IsValueType || !populatedValue) |
| | 0 | 341 | | { |
| | | 342 | | // note: populatedValue value may be different than when CreationHandling is Populate |
| | | 343 | | // i.e. when initial value of property is null |
| | | 344 | | |
| | | 345 | | // We cannot do reader.Skip early because converter decides if populating will happen or not |
| | 0 | 346 | | if (CanDeserialize) |
| | 0 | 347 | | { |
| | 0 | 348 | | if (value is null && !IsSetNullable && Options.RespectNullableAnnotations) |
| | 0 | 349 | | { |
| | 0 | 350 | | ThrowHelper.ThrowJsonException_PropertySetterDisallowNull(Name, state.Current.JsonTy |
| | | 351 | | } |
| | | 352 | | |
| | 0 | 353 | | Set!(obj, value!); |
| | 0 | 354 | | } |
| | 0 | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | state.Current.MarkPropertyAsRead(this); |
| | 0 | 358 | | } |
| | 0 | 359 | | } |
| | 0 | 360 | | } |
| | | 361 | | |
| | 0 | 362 | | return success; |
| | 0 | 363 | | } |
| | | 364 | | |
| | | 365 | | internal override bool ReadJsonAsObject(scoped ref ReadStack state, ref Utf8JsonReader reader, out object? value |
| | 0 | 366 | | { |
| | | 367 | | bool success; |
| | 0 | 368 | | bool isNullToken = reader.TokenType == JsonTokenType.Null; |
| | 0 | 369 | | if (isNullToken && !EffectiveConverter.HandleNullOnRead && !state.IsContinuation) |
| | 0 | 370 | | { |
| | 0 | 371 | | if (default(T) is not null) |
| | 0 | 372 | | { |
| | 0 | 373 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(EffectiveConverter.Type); |
| | | 374 | | } |
| | | 375 | | |
| | 0 | 376 | | value = default(T); |
| | 0 | 377 | | success = true; |
| | 0 | 378 | | } |
| | | 379 | | else |
| | 0 | 380 | | { |
| | | 381 | | // Optimize for internal converters by avoiding the extra call to TryRead. |
| | 0 | 382 | | if (EffectiveConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null) |
| | 0 | 383 | | { |
| | | 384 | | // CanUseDirectReadOrWrite == false when using streams |
| | 0 | 385 | | Debug.Assert(!state.IsContinuation); |
| | | 386 | | |
| | 0 | 387 | | value = EffectiveConverter.Read(ref reader, PropertyType, Options); |
| | 0 | 388 | | success = true; |
| | 0 | 389 | | } |
| | | 390 | | else |
| | 0 | 391 | | { |
| | 0 | 392 | | success = EffectiveConverter.TryRead(ref reader, PropertyType, Options, ref state, out T? typedValue |
| | 0 | 393 | | value = typedValue; |
| | 0 | 394 | | } |
| | 0 | 395 | | } |
| | | 396 | | |
| | 0 | 397 | | return success; |
| | 0 | 398 | | } |
| | | 399 | | |
| | | 400 | | private protected override void ConfigureIgnoreCondition(JsonIgnoreCondition? ignoreCondition) |
| | 7794 | 401 | | { |
| | 7794 | 402 | | switch (ignoreCondition) |
| | | 403 | | { |
| | | 404 | | case null: |
| | 7794 | 405 | | break; |
| | | 406 | | |
| | | 407 | | case JsonIgnoreCondition.Never: |
| | 0 | 408 | | ShouldSerialize = ShouldSerializeIgnoreConditionNever; |
| | 0 | 409 | | break; |
| | | 410 | | |
| | | 411 | | case JsonIgnoreCondition.Always: |
| | 0 | 412 | | ShouldSerialize = ShouldSerializeIgnoreConditionAlways; |
| | 0 | 413 | | break; |
| | | 414 | | |
| | | 415 | | case JsonIgnoreCondition.WhenWritingNull: |
| | 0 | 416 | | if (PropertyTypeCanBeNull) |
| | 0 | 417 | | { |
| | 0 | 418 | | ShouldSerialize = ShouldSerializeIgnoreWhenWritingDefault; |
| | 0 | 419 | | IgnoreDefaultValuesOnWrite = true; |
| | 0 | 420 | | } |
| | | 421 | | else |
| | 0 | 422 | | { |
| | 0 | 423 | | ThrowHelper.ThrowInvalidOperationException_IgnoreConditionOnValueTypeInvalid(MemberName!, Declar |
| | | 424 | | } |
| | 0 | 425 | | break; |
| | | 426 | | |
| | | 427 | | case JsonIgnoreCondition.WhenWritingDefault: |
| | 0 | 428 | | ShouldSerialize = ShouldSerializeIgnoreWhenWritingDefault; |
| | 0 | 429 | | IgnoreDefaultValuesOnWrite = true; |
| | 0 | 430 | | break; |
| | | 431 | | |
| | | 432 | | case JsonIgnoreCondition.WhenWriting: |
| | 0 | 433 | | ShouldSerialize = ShouldSerializeIgnoreConditionAlways; |
| | 0 | 434 | | break; |
| | | 435 | | |
| | | 436 | | case JsonIgnoreCondition.WhenReading: |
| | 0 | 437 | | Set = null; |
| | 0 | 438 | | break; |
| | | 439 | | |
| | | 440 | | default: |
| | 0 | 441 | | Debug.Fail($"Unknown value of JsonIgnoreCondition '{ignoreCondition}'"); |
| | | 442 | | break; |
| | | 443 | | } |
| | | 444 | | |
| | 0 | 445 | | static bool ShouldSerializeIgnoreConditionNever(object _, T? value) => true; |
| | 0 | 446 | | static bool ShouldSerializeIgnoreConditionAlways(object _, T? value) => false; |
| | | 447 | | static bool ShouldSerializeIgnoreWhenWritingDefault(object _, T? value) |
| | 0 | 448 | | { |
| | 0 | 449 | | return default(T) is null ? value is not null : !EqualityComparer<T>.Default.Equals(default, value); |
| | 0 | 450 | | } |
| | 7794 | 451 | | } |
| | | 452 | | |
| | | 453 | | private static bool IsDefaultValue(T? value) |
| | 0 | 454 | | { |
| | 0 | 455 | | return default(T) is null ? value is null : EqualityComparer<T>.Default.Equals(default, value); |
| | 0 | 456 | | } |
| | | 457 | | } |
| | | 458 | | } |