Skip to content

Commit

Permalink
refactor: Chnage newtonsoft to System.Text.Json.
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Jun 26, 2024
1 parent 4928f41 commit ae9d975
Show file tree
Hide file tree
Showing 23 changed files with 451 additions and 40 deletions.
20 changes: 10 additions & 10 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ SOFTWARE. -->
<TargetFramework Condition="'$(_IsPacking)'!='true'">net8.0</TargetFramework>
<AssemblyVersion>2.0</AssemblyVersion>
<FileVersion>2.0.3</FileVersion>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/s2quake/communication</RepositoryUrl>
<LicenseUrl>https://github.com/s2quake/communication/blob/main/LICENSE.md</LicenseUrl>
<Copyright>Copyright (c) 2024 Jeesu Choi</Copyright>
<Description>grpc-based communication library for crema project</Description>
<Authors>s2quake</Authors>
<PackageVersion>$(FileVersion)</PackageVersion>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/s2quake/communication</RepositoryUrl>
<LicenseUrl>https://github.com/s2quake/communication/blob/main/LICENSE.md</LicenseUrl>
<Copyright>Copyright (c) 2024 Jeesu Choi</Copyright>
<Description>grpc-based communication library for crema project</Description>
<Authors>s2quake</Authors>
<PackageVersion>$(FileVersion)</PackageVersion>
<PackageProjectUrl>https://github.com/s2quake/communication</PackageProjectUrl>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -67,9 +67,9 @@ SOFTWARE. -->
<RootPath Condition="'$(SolutionDir)' == ''">$(MSBuildThisFileDirectory)\</RootPath>
<ConfigurationType Condition="'$(Configuration)' == ''">Debug</ConfigurationType>
<ConfigurationType Condition="'$(Configuration)' != ''">$(Configuration)</ConfigurationType>
<DelaySign>true</DelaySign>
<SignAssembly Condition="'$(Configuration)' == 'Release'">true</SignAssembly>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<DelaySign>true</DelaySign>
<SignAssembly Condition="'$(Configuration)' == 'Release'">true</SignAssembly>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PublishGroup>communication</PublishGroup>
Expand Down
33 changes: 33 additions & 0 deletions src/JSSoft.Communication/Converters/ArgumentExceptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <copyright file="ArgumentExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class ArgumentExceptionConverter : JsonConverter<ArgumentException>
{
public const string ParamName = "paramName";
public const string Message = "message";

public override ArgumentException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var props = reader.ReadObject(capacity: 2);
var message = props[Message] ?? throw new JsonException($"{Message} is not found");
return new ArgumentException(message);
}

public override void Write(
Utf8JsonWriter writer, ArgumentException value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(Message, value.Message);
writer.WriteString(ParamName, value.ParamName);
writer.WriteEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <copyright file="ArgumentNullExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class ArgumentNullExceptionConverter : JsonConverter<ArgumentNullException>
{
public const string ParamName = "paramName";
public const string Message = "message";

public override ArgumentNullException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var props = reader.ReadObject(capacity: 2);
var message = props[Message] ?? throw new JsonException($"{Message} is not found");
return new ArgumentNullException(message);
}

public override void Write(
Utf8JsonWriter writer, ArgumentNullException value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(ParamName, value.ParamName);
writer.WriteString(Message, value.Message);
writer.WriteEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="ArgumentOutOfRangeExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class ArgumentOutOfRangeExceptionConverter
: JsonConverter<ArgumentOutOfRangeException>
{
public const string ParamName = "paramName";
public const string Message = "message";

public override ArgumentOutOfRangeException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var props = reader.ReadObject(capacity: 2);
var message = props[Message] ?? throw new JsonException($"{Message} is not found");
return new ArgumentOutOfRangeException(message);
}

public override void Write(
Utf8JsonWriter writer, ArgumentOutOfRangeException value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(ParamName, value.ParamName);
writer.WriteString(Message, value.Message);
writer.WriteEndObject();
}
}
43 changes: 43 additions & 0 deletions src/JSSoft.Communication/Converters/ConverterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="ConverterExtensions.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System.Collections.Generic;
using System.Text.Json;

namespace JSSoft.Communication.Converters;

internal static class ConverterExtensions
{
public static IDictionary<string, string?> ReadObject(
this ref Utf8JsonReader @this, int capacity)
{
var props = new Dictionary<string, string?>(capacity);
if (@this.TokenType != JsonTokenType.StartObject)
{
throw new JsonException("Invalid Json format");
}

while (@this.Read())
{
if (@this.TokenType == JsonTokenType.EndObject)
{
@this.Read();
return props;
}

if (@this.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid Json format");
}

var key = @this.GetString() ?? throw new JsonException("Invalid Json format");
@this.Read();
var value = @this.GetString();
props.Add(key, value);
}

throw new JsonException("Invalid Json format");
}
}
21 changes: 21 additions & 0 deletions src/JSSoft.Communication/Converters/ExceptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="ExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class ExceptionConverter : JsonConverter<Exception>
{
public override Exception? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() is string message ? new Exception(message) : null;

public override void Write(
Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.Message);
}
46 changes: 46 additions & 0 deletions src/JSSoft.Communication/Converters/ExceptionConverterFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <copyright file="ExceptionConverterFactory.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class ExceptionConverterFactory : JsonConverterFactory
{
private readonly Dictionary<Type, JsonConverter> _converterByType = [];

public override bool CanConvert(Type typeToConvert)
=> typeof(Exception).IsAssignableFrom(typeToConvert);

public override JsonConverter? CreateConverter(
Type typeToConvert, JsonSerializerOptions options)
{
if (_converterByType.TryGetValue(typeToConvert, out var converter) != true)
{
var genericType = typeof(InternalJsonConverter<>).MakeGenericType(typeToConvert);
converter = (JsonConverter)Activator.CreateInstance(genericType)!;
_converterByType.Add(typeToConvert, converter);
}

return converter;
}

private sealed class InternalJsonConverter<T> : JsonConverter<T>
where T : Exception
{
public override T? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() is string message
? (T)Activator.CreateInstance(typeToConvert, message)!
: null;

public override void Write(
Utf8JsonWriter writer, T value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="IndexOutOfRangeExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class IndexOutOfRangeExceptionConverter : JsonConverter<IndexOutOfRangeException>
{
public override IndexOutOfRangeException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() is string message ? new IndexOutOfRangeException(message) : null;

public override void Write(
Utf8JsonWriter writer, IndexOutOfRangeException value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.Message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="InvalidOperationExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class InvalidOperationExceptionConverter : JsonConverter<InvalidOperationException>
{
public override InvalidOperationException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() is string message ? new InvalidOperationException(message) : null;

public override void Write(
Utf8JsonWriter writer, InvalidOperationException value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.Message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="NotSupportedExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class NotSupportedExceptionConverter : JsonConverter<NotSupportedException>
{
public override NotSupportedException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() is string message ? new NotSupportedException(message) : null;

public override void Write(
Utf8JsonWriter writer, NotSupportedException value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.Message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="NullReferenceExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class NullReferenceExceptionConverter : JsonConverter<NullReferenceException>
{
public override NullReferenceException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() is string message ? new NullReferenceException(message) : null;

public override void Write(
Utf8JsonWriter writer, NullReferenceException value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.Message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="ObjectDisposedExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class ObjectDisposedExceptionConverter : JsonConverter<ObjectDisposedException>
{
public const string ObjectName = "objectName";
public const string Message = "message";

public override ObjectDisposedException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var props = reader.ReadObject(capacity: 2);
var objectName = props[ObjectName] ?? throw new JsonException($"{ObjectName} is not found");
var message = props[Message] ?? throw new JsonException($"{Message} is not found");
return new ObjectDisposedException(objectName, message);
}

public override void Write(
Utf8JsonWriter writer, ObjectDisposedException value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(ObjectName, value.ObjectName);
writer.WriteString(Message, value.Message);
writer.WriteEndObject();
}
}
21 changes: 21 additions & 0 deletions src/JSSoft.Communication/Converters/SystemExceptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="SystemExceptionConverter.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSSoft.Communication.Converters;

internal sealed class SystemExceptionConverter : JsonConverter<SystemException>
{
public override SystemException? Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() is string message ? new SystemException(message) : null;

public override void Write(
Utf8JsonWriter writer, SystemException value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.Message);
}
Loading

0 comments on commit ae9d975

Please sign in to comment.