| File: System\Text\Json\Serialization\Converters\Value\StringConverter.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 StringConverter : JsonPrimitiveConverter<string?> { public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return reader.GetString(); } public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) { // For performance, lift up the writer implementation. if (value == null) { writer.WriteNullValue(); } else { writer.WriteStringValue(value.AsSpan()); } } internal override string ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); return reader.GetString()!; } internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, string value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) { ArgumentNullException.ThrowIfNull(value); if (options.DictionaryKeyPolicy != null && !isWritingExtensionDataProperty) { value = options.DictionaryKeyPolicy.ConvertName(value); if (value == null) { ThrowHelper.ThrowInvalidOperationException_NamingPolicyReturnNull(options.DictionaryKeyPolicy); } } writer.WritePropertyName(value); } internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String }; internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String; } }