| File: System\Text\Json\Serialization\Converters\Value\Int16Converter.cs | Web Access |
| Project: src\runtime\src\libraries\System.Text.Json\src\System.Text.Json.csproj (System.Text.Json) |
// 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.Text.Json.Nodes; using System.Text.Json.Schema; namespace System.Text.Json.Serialization.Converters { internal sealed class Int16Converter : JsonPrimitiveConverter<short> { public Int16Converter() { IsInternalConverterForNumberType = true; } public override short Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (options?.NumberHandling is not null and not JsonNumberHandling.Strict) { return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options); } return reader.GetInt16(); } public override void Write(Utf8JsonWriter writer, short value, JsonSerializerOptions options) { if (options?.NumberHandling is not null and not JsonNumberHandling.Strict) { WriteNumberWithCustomHandling(writer, value, options.NumberHandling); return; } // For performance, lift up the writer implementation. writer.WriteNumberValue((long)value); } internal override short ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); return reader.GetInt16WithQuotes(); } internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, short value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) { writer.WritePropertyName(value); } internal override short ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String && (JsonNumberHandling.AllowReadingFromString & handling) != 0) { return reader.GetInt16WithQuotes(); } return reader.GetInt16(); } internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, short value, JsonNumberHandling handling) { if ((JsonNumberHandling.WriteAsString & handling) != 0) { writer.WriteNumberValueAsString(value); } else { // For performance, lift up the writer implementation. writer.WriteNumberValue((long)value); } } internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling) => GetSchemaForNumericType(JsonSchemaType.Integer, numberHandling); internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling numberHandling) => GetSupportedJsonValueTypesForNumericType(numberHandling); } }