| | | 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 NET |
| | 0 | 38 | | Debug.Assert(isIeeeFloatingPoint == (typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) |
| | | 39 | | #endif |
| | 0 | 40 | | string? pattern = null; |
| | | 41 | | |
| | 0 | 42 | | if ((numberHandling & (JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)) != 0) |
| | 0 | 43 | | { |
| | 0 | 44 | | pattern = schemaType is JsonSchemaType.Integer |
| | 0 | 45 | | ? @"^-?(?:0|[1-9]\d*)$" |
| | 0 | 46 | | : isIeeeFloatingPoint |
| | 0 | 47 | | ? @"^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$" |
| | 0 | 48 | | : @"^-?(?:0|[1-9]\d*)(?:\.\d+)?$"; |
| | | 49 | | |
| | 0 | 50 | | schemaType |= JsonSchemaType.String; |
| | 0 | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | if (isIeeeFloatingPoint && (numberHandling & JsonNumberHandling.AllowNamedFloatingPointLiterals) != 0) |
| | 0 | 54 | | { |
| | 0 | 55 | | return new JsonSchema |
| | 0 | 56 | | { |
| | 0 | 57 | | AnyOf = |
| | 0 | 58 | | [ |
| | 0 | 59 | | new JsonSchema { Type = schemaType, Pattern = pattern }, |
| | 0 | 60 | | new JsonSchema { Enum = [(JsonNode)"NaN", (JsonNode)"Infinity", (JsonNode)"-Infinity"] }, |
| | 0 | 61 | | ] |
| | 0 | 62 | | }; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return new JsonSchema { Type = schemaType, Pattern = pattern }; |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |