| | | 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.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Text.Json.Nodes; |
| | | 7 | | using System.Text.Json.Schema; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Converters |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Inherited by built-in converters serializing types as JSON primitives that support property name serialization. |
| | | 13 | | /// </summary> |
| | | 14 | | internal abstract class JsonPrimitiveConverter<T> : JsonConverter<T> |
| | | 15 | | { |
| | | 16 | | public sealed override void WriteAsPropertyName(Utf8JsonWriter writer, [DisallowNull] T value, JsonSerializerOpt |
| | 0 | 17 | | { |
| | 0 | 18 | | ArgumentNullException.ThrowIfNull(value); |
| | | 19 | | |
| | 0 | 20 | | WriteAsPropertyNameCore(writer, value, options, isWritingExtensionDataProperty: false); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public sealed override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions |
| | 0 | 24 | | { |
| | 0 | 25 | | if (reader.TokenType != JsonTokenType.PropertyName) |
| | 0 | 26 | | { |
| | 0 | 27 | | ThrowHelper.ThrowInvalidOperationException_ExpectedPropertyName(reader.TokenType); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | return ReadAsPropertyNameCore(ref reader, typeToConvert, options); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private protected static JsonSchema GetSchemaForNumericType(JsonSchemaType schemaType, JsonNumberHandling number |
| | 0 | 34 | | { |
| | 0 | 35 | | Debug.Assert(schemaType is JsonSchemaType.Integer or JsonSchemaType.Number); |
| | 0 | 36 | | Debug.Assert(!isIeeeFloatingPoint || schemaType is JsonSchemaType.Number); |
| | | 37 | | #if NET11_0_OR_GREATER |
| | 0 | 38 | | Debug.Assert(isIeeeFloatingPoint == (typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) |
| | 0 | 39 | | typeof(T) == typeof(System.Numerics.BFloat16) || typeof(T) == typeof(System.Numerics.Decimal32) || |
| | 0 | 40 | | typeof(T) == typeof(System.Numerics.Decimal64) || typeof(T) == typeof(System.Numerics.Decimal128))); |
| | | 41 | | #elif NET |
| | | 42 | | Debug.Assert(isIeeeFloatingPoint == (typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) |
| | | 43 | | #endif |
| | 0 | 44 | | string? pattern = null; |
| | | 45 | | |
| | 0 | 46 | | if ((numberHandling & (JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)) != 0) |
| | 0 | 47 | | { |
| | 0 | 48 | | pattern = schemaType is JsonSchemaType.Integer |
| | 0 | 49 | | ? @"^-?(?:0|[1-9]\d*)$" |
| | 0 | 50 | | : isIeeeFloatingPoint |
| | 0 | 51 | | ? @"^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$" |
| | 0 | 52 | | : @"^-?(?:0|[1-9]\d*)(?:\.\d+)?$"; |
| | | 53 | | |
| | 0 | 54 | | schemaType |= JsonSchemaType.String; |
| | 0 | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | if (isIeeeFloatingPoint && (numberHandling & JsonNumberHandling.AllowNamedFloatingPointLiterals) != 0) |
| | 0 | 58 | | { |
| | 0 | 59 | | return new JsonSchema |
| | 0 | 60 | | { |
| | 0 | 61 | | AnyOf = |
| | 0 | 62 | | [ |
| | 0 | 63 | | new JsonSchema { Type = schemaType, Pattern = pattern }, |
| | 0 | 64 | | new JsonSchema { Enum = [(JsonNode)"NaN", (JsonNode)"Infinity", (JsonNode)"-Infinity"] }, |
| | 0 | 65 | | ] |
| | 0 | 66 | | }; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | return new JsonSchema { Type = schemaType, Pattern = pattern }; |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | private protected static JsonValueType GetSupportedJsonValueTypesForNumericType(JsonNumberHandling numberHandlin |
| | 0 | 73 | | (numberHandling & JsonNumberHandling.AllowReadingFromString) != 0 |
| | 0 | 74 | | ? JsonValueType.Number | JsonValueType.String |
| | 0 | 75 | | : JsonValueType.Number; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |