41 lines
1018 B
C#
41 lines
1018 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace System.Text.Json.Serialization
|
|
{
|
|
public class JsonStringConverter : JsonConverter<string>
|
|
{
|
|
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.Null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
{
|
|
return reader.GetString();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
|
{
|
|
if (value == null)
|
|
{
|
|
writer.WriteNullValue();
|
|
}
|
|
else
|
|
{
|
|
writer.WriteStringValue(value);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|