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 29, 2024
1 parent 20ea223 commit 0279fd9
Show file tree
Hide file tree
Showing 23 changed files with 444 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,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;

Check failure on line 7 in src/JSSoft.Communication/Converters/ArgumentExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
using System.Text.Json.Serialization;

Check failure on line 8 in src/JSSoft.Communication/Converters/ArgumentExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)

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;

Check failure on line 7 in src/JSSoft.Communication/Converters/ArgumentNullExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
using System.Text.Json.Serialization;

Check failure on line 8 in src/JSSoft.Communication/Converters/ArgumentNullExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)

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;

Check failure on line 7 in src/JSSoft.Communication/Converters/ArgumentOutOfRangeExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
using System.Text.Json.Serialization;

Check failure on line 8 in src/JSSoft.Communication/Converters/ArgumentOutOfRangeExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)

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;

Check failure on line 7 in src/JSSoft.Communication/Converters/ConverterExtensions.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)

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;

Check failure on line 7 in src/JSSoft.Communication/Converters/ExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
using System.Text.Json.Serialization;

Check failure on line 8 in src/JSSoft.Communication/Converters/ExceptionConverter.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)

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;

Check failure on line 8 in src/JSSoft.Communication/Converters/ExceptionConverterFactory.cs

View workflow job for this annotation

GitHub Actions / pack

The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
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 0279fd9

Please sign in to comment.