-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Chnage newtonsoft to System.Text.Json.
- Loading branch information
Showing
23 changed files
with
444 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/JSSoft.Communication/Converters/ArgumentExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/JSSoft.Communication/Converters/ArgumentNullExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/JSSoft.Communication/Converters/ArgumentOutOfRangeExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
src/JSSoft.Communication/Converters/ConverterExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/JSSoft.Communication/Converters/ExceptionConverterFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/JSSoft.Communication/Converters/IndexOutOfRangeExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/JSSoft.Communication/Converters/InvalidOperationExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/JSSoft.Communication/Converters/NotSupportedExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/JSSoft.Communication/Converters/NullReferenceExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/JSSoft.Communication/Converters/ObjectDisposedExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/JSSoft.Communication/Converters/SystemExceptionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.