Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Jun 25, 2024
1 parent 4928f41 commit 653aff6
Show file tree
Hide file tree
Showing 21 changed files with 440 additions and 28 deletions.
34 changes: 34 additions & 0 deletions src/JSSoft.Communication/Converters/ArgumentExceptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <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");
var paramName = props[ParamName];
return new ArgumentException(message, paramName);
}

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,34 @@
// <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 paramName = props[ParamName];
var message = props[Message] ?? throw new JsonException($"{Message} is not found");
return new ArgumentNullException(paramName, 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,35 @@
// <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 paramName = props[ParamName];
var message = props[Message] ?? throw new JsonException($"{Message} is not found");
return new ArgumentOutOfRangeException(paramName, 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 653aff6

Please sign in to comment.