--- lang: ja-jp breaks: true --- # 'System.Text.Json' 組み込みのカスタムコンバータ `JsonConverter` 2022-08-13 https://github.com/dotnet/runtime/tree/81bf79fd9aa75305e55abe2f7e9ef3f60624a3a1/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters > .NET で JSON シリアル化 (マーシャリング) のためのカスタム コンバーターを作成する方法 > https://docs.microsoft.com/ja-jp/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-6-0 ## カスタムコンバータの実装例 ```csharp= [System.Text.Json.Serialization.JsonConverter(typeof(Base64StringJsonConverter))] [Serializable] public sealed class Base64String { private string m_value; /// <summary> /// /// </summary> public Base64String() : base() { } [System.Text.Json.Serialization.JsonInclude] public string Value { get { return this.m_value; } internal set { this.m_value = value; } } } internal sealed class Base64StringJsonConverter : System.Text.Json.Serialization.JsonConverter<Base64String> { public override Base64String Read( ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options ) { return new Base64String() { Value = reader.GetString() }; } public override void Write( System.Text.Json.Utf8JsonWriter writer, Base64String value, System.Text.Json.JsonSerializerOptions options ) { writer.WriteStringValue(value.Value); } } ``` ###### tags: `System.Text.Json` `JsonConverterFactory`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up