diff --git a/src/ModelResult.Text.Json/Converters/ConverterHelper.cs b/src/ModelResult.Text.Json/Converters/ConverterHelper.cs deleted file mode 100644 index e696620..0000000 --- a/src/ModelResult.Text.Json/Converters/ConverterHelper.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Byndyusoft.ModelResult.Converters -{ - using System.Text.Json; - - public static class ConverterHelper - { - public static JsonEncodedText GetPropertyName(string name, JsonSerializerOptions options) - { - if (options.PropertyNamingPolicy != null) - name = options.PropertyNamingPolicy.ConvertName(name); - return JsonEncodedText.Encode(name, options.Encoder); - } - } -} \ No newline at end of file diff --git a/src/ModelResult.Text.Json/Converters/ErrorInfoConverter.cs b/src/ModelResult.Text.Json/Converters/ErrorInfoConverter.cs deleted file mode 100644 index 02c17d5..0000000 --- a/src/ModelResult.Text.Json/Converters/ErrorInfoConverter.cs +++ /dev/null @@ -1,162 +0,0 @@ -namespace Byndyusoft.ModelResult.Converters -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Text.Json; - using System.Text.Json.Serialization; - using Extensions; - using ModelResult; - - public class ErrorInfoConverter : JsonConverter - { - public override ErrorInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.Null) - return null; - - if (reader.TokenType != JsonTokenType.StartObject) - throw new JsonException(); - - var code = (string?) null; - var message = (string?) null; - var items = (ErrorInfoItem[]?) null; - var itemsConverter = GetItemConverter(options); - - var codeName = GetCodeName(options); - var codeNameLower = GetCodeNameLower(options); - var messageName = GetMessageName(options); - var messageNameLower = GetMessageNameLower(options); - var itemsName = GetItemsName(options); - var itemsNameLower = GetItemsNameLower(options); - - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) - break; - - if (reader.TokenType != JsonTokenType.PropertyName) - throw new JsonException(); - - if (reader.ValueTextEquals(codeName.EncodedUtf8Bytes) || reader.ValueTextEquals(codeNameLower.EncodedUtf8Bytes)) - code = GetStringValue(ref reader); - else if (reader.ValueTextEquals(messageName.EncodedUtf8Bytes) || reader.ValueTextEquals(messageNameLower.EncodedUtf8Bytes)) - message = GetStringValue(ref reader); - else if (reader.ValueTextEquals(itemsName.EncodedUtf8Bytes) || reader.ValueTextEquals(itemsNameLower.EncodedUtf8Bytes)) - items = GetErrorInfoItems(ref reader, options, itemsConverter); - else - throw new JsonException(); - } - - if (code == null) - throw new InvalidOperationException("Code is null"); - - if (message == null) - throw new InvalidOperationException("Message is null"); - - var errorInfo = new ErrorInfo(code, message, items); - return errorInfo; - } - - private JsonEncodedText GetCodeName(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfo.Code), options); - } - - private JsonEncodedText GetCodeNameLower(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfo.Code).ToFirstLowerChar(), options); - } - - private JsonEncodedText GetMessageName(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfo.Message), options); - } - - private JsonEncodedText GetMessageNameLower(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfo.Message).ToFirstLowerChar(), options); - } - - private JsonEncodedText GetItemsName(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfo.Items), options); - } - - private JsonEncodedText GetItemsNameLower(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfo.Items).ToFirstLowerChar(), options); - } - - public JsonConverter GetItemConverter(JsonSerializerOptions options) - { - return (JsonConverter) options.GetConverter(typeof(ErrorInfoItem)); - } - - private string GetStringValue(ref Utf8JsonReader reader) - { - reader.Read(); - - if (reader.TokenType != JsonTokenType.String) - throw new JsonException(); - - var value = reader.GetString(); - - return value; - } - - private ErrorInfoItem[]? GetErrorInfoItems(ref Utf8JsonReader reader, JsonSerializerOptions options, - JsonConverter itemsConverter) - { - reader.Read(); - - if (reader.TokenType == JsonTokenType.Null) - return null; - - if (reader.TokenType != JsonTokenType.StartArray) - throw new JsonException(); - - var items = new List(); - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndArray) - break; - - var errorInfoItem = itemsConverter.Read(ref reader, typeof(ErrorInfoItem), options); - items.Add(errorInfoItem); - } - - return items.ToArray(); - } - - public override void Write(Utf8JsonWriter writer, ErrorInfo? value, JsonSerializerOptions options) - { - if (value == null) - writer.WriteNullValue(); - else - { - var itemsConverter = GetItemConverter(options); - writer.WriteStartObject(); - writer.WriteString(GetMessageName(options), value.Message); - writer.WriteString(GetCodeName(options), value.Code); - WriteItems(writer, value.Items, options, itemsConverter); - writer.WriteEndObject(); - } - } - - private void WriteItems(Utf8JsonWriter writer, ErrorInfoItem[]? items, JsonSerializerOptions options, - JsonConverter itemsConverter) - { - if (items == null || items.Any() == false) - writer.WriteNull(GetItemsName(options)); - - else - { - writer.WriteStartArray(GetItemsName(options)); - foreach (var errorInfoItem in items) - itemsConverter.Write(writer, errorInfoItem, options); - writer.WriteEndArray(); - } - } - } -} \ No newline at end of file diff --git a/src/ModelResult.Text.Json/Converters/ErrorInfoItemConverter.cs b/src/ModelResult.Text.Json/Converters/ErrorInfoItemConverter.cs deleted file mode 100644 index 7690b5b..0000000 --- a/src/ModelResult.Text.Json/Converters/ErrorInfoItemConverter.cs +++ /dev/null @@ -1,97 +0,0 @@ -namespace Byndyusoft.ModelResult.Converters -{ - using System; - using System.Text.Json; - using System.Text.Json.Serialization; - using Extensions; - using ModelResult; - - public class ErrorInfoItemConverter : JsonConverter - { - public override ErrorInfoItem? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.Null) - return null; - - if (reader.TokenType != JsonTokenType.StartObject) - throw new JsonException(); - - var propertyNameName = GetPropertyNameName(options); - var propertyNameNameLower = GetPropertyNameNameLower(options); - var errorName = GetErrorName(options); - var errorNameLower = GetErrorNameLower(options); - - var property = (string?) null; - var error = (string?) null; - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) - break; - - if (reader.TokenType != JsonTokenType.PropertyName) - throw new JsonException(); - - if (reader.ValueTextEquals(propertyNameName.EncodedUtf8Bytes) || reader.ValueTextEquals(propertyNameNameLower.EncodedUtf8Bytes)) - property = GetValue(ref reader); - else if (reader.ValueTextEquals(errorName.EncodedUtf8Bytes) || reader.ValueTextEquals(errorNameLower.EncodedUtf8Bytes)) - error = GetValue(ref reader); - else - throw new JsonException(); - } - - if (property == null) - throw new InvalidOperationException("Property is null"); - - if (error == null) - throw new InvalidOperationException("Error is null"); - - var errorInfoItem = new ErrorInfoItem(property, error); - return errorInfoItem; - } - - private JsonEncodedText GetPropertyNameName(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfoItem.PropertyName), options); - } - - private JsonEncodedText GetPropertyNameNameLower(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfoItem.PropertyName).ToFirstLowerChar(), options); - } - - private JsonEncodedText GetErrorName(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfoItem.Error), options); - } - - private JsonEncodedText GetErrorNameLower(JsonSerializerOptions options) - { - return ConverterHelper.GetPropertyName(nameof(ErrorInfoItem.Error).ToFirstLowerChar(), options); - } - - private string GetValue(ref Utf8JsonReader reader) - { - reader.Read(); - - if (reader.TokenType != JsonTokenType.String) - throw new JsonException(); - - var value = reader.GetString(); - - return value; - } - - public override void Write(Utf8JsonWriter writer, ErrorInfoItem? value, JsonSerializerOptions options) - { - if (value == null) - writer.WriteNullValue(); - else - { - writer.WriteStartObject(); - writer.WriteString(GetPropertyNameName(options), value.PropertyName); - writer.WriteString(GetErrorName(options), value.Error); - writer.WriteEndObject(); - } - } - } -} \ No newline at end of file diff --git a/src/ModelResult.Text.Json/Extensions/JsonSerializerOptionsExtensions.cs b/src/ModelResult.Text.Json/Extensions/JsonSerializerOptionsExtensions.cs deleted file mode 100644 index c641cb6..0000000 --- a/src/ModelResult.Text.Json/Extensions/JsonSerializerOptionsExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Byndyusoft.ModelResult.Extensions -{ - using System.Text.Json; - using Converters; - - public static class JsonSerializerOptionsExtensions - { - public static void AddErrorInfoConverter(this JsonSerializerOptions options) - { - options.Converters.Add(new ErrorInfoConverter()); - options.Converters.Add(new ErrorInfoItemConverter()); - } - } -} \ No newline at end of file diff --git a/src/ModelResult.Text.Json/Extensions/StringExtensions.cs b/src/ModelResult.Text.Json/Extensions/StringExtensions.cs deleted file mode 100644 index 128d0a7..0000000 --- a/src/ModelResult.Text.Json/Extensions/StringExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Byndyusoft.ModelResult.Extensions -{ - using System.Linq; - - public static class StringExtensions - { - public static string ToFirstLowerChar(this string propertyName) - { - if (string.IsNullOrEmpty(propertyName)) - return string.Empty; - return propertyName.First().ToString().ToLower() + propertyName.Substring(1); - } - } -} \ No newline at end of file diff --git a/src/ModelResult.Text.Json/ModelResult.Text.Json.csproj b/src/ModelResult.Text.Json/ModelResult.Text.Json.csproj deleted file mode 100644 index a99eefc..0000000 --- a/src/ModelResult.Text.Json/ModelResult.Text.Json.csproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - netcoreapp3.1 - enable - - - - - - - - - - - - -