| | | 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.Text.Json.Nodes; |
| | | 6 | | using System.Text.Json.Schema; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | internal sealed class StringConverter : JsonPrimitiveConverter<string?> |
| | | 11 | | { |
| | | 12 | | public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 660 | 13 | | { |
| | 660 | 14 | | return reader.GetString(); |
| | 180 | 15 | | } |
| | | 16 | | |
| | | 17 | | public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) |
| | 0 | 18 | | { |
| | | 19 | | // For performance, lift up the writer implementation. |
| | 0 | 20 | | if (value == null) |
| | 0 | 21 | | { |
| | 0 | 22 | | writer.WriteNullValue(); |
| | 0 | 23 | | } |
| | | 24 | | else |
| | 0 | 25 | | { |
| | 0 | 26 | | writer.WriteStringValue(value.AsSpan()); |
| | 0 | 27 | | } |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | internal override string ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOpt |
| | 0 | 31 | | { |
| | 0 | 32 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | 0 | 33 | | return reader.GetString()!; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, string value, JsonSerializerOptions option |
| | 0 | 37 | | { |
| | 0 | 38 | | ArgumentNullException.ThrowIfNull(value); |
| | | 39 | | |
| | 0 | 40 | | if (options.DictionaryKeyPolicy != null && !isWritingExtensionDataProperty) |
| | 0 | 41 | | { |
| | 0 | 42 | | value = options.DictionaryKeyPolicy.ConvertName(value); |
| | | 43 | | |
| | 0 | 44 | | if (value == null) |
| | 0 | 45 | | { |
| | 0 | 46 | | ThrowHelper.ThrowInvalidOperationException_NamingPolicyReturnNull(options.DictionaryKeyPolicy); |
| | | 47 | | } |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | writer.WritePropertyName(value); |
| | 0 | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String }; |
| | | 54 | | } |
| | | 55 | | } |