StringConverter
namespace System.
Text.
Json.
Serialization.
Converters
{
internal sealed class StringConverter :
JsonConverter<
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)
{
if (
value ==
null)
writer.
WriteNullValue();
else
writer.
WriteStringValue(
value.
AsSpan());
}
internal override string ReadWithQuotes(
ref Utf8JsonReader reader)
{
return reader.
GetString();
}
internal override void WriteWithQuotes(
Utf8JsonWriter writer,
string value,
JsonSerializerOptions options,
ref WriteStack state)
{
if (
options.
DictionaryKeyPolicy !=
null && !
state.
Current.
IgnoreDictionaryKeyPolicy) {
value =
options.
DictionaryKeyPolicy.
ConvertName(
value);
if (
value ==
null)
ThrowHelper.
ThrowInvalidOperationException_NamingPolicyReturnNull(
options.
DictionaryKeyPolicy);
}
writer.
WritePropertyName(
value);
}
}
}