From 84a4c5c05871833ac653aca0740469ac833f2999 Mon Sep 17 00:00:00 2001 From: sbansla <104902068+sbansla@users.noreply.github.com> Date: Fri, 8 Sep 2023 16:57:53 +0530 Subject: [PATCH] chore: added custom converters for nested objects (#688) --- .github/workflows/test-and-deploy.yml | 5 +- src/Twilio/Converters/CustomConverters.cs | 182 ++++++++++++++++++++++ 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/Twilio/Converters/CustomConverters.cs diff --git a/.github/workflows/test-and-deploy.yml b/.github/workflows/test-and-deploy.yml index 55ed94346..3039b9a99 100644 --- a/.github/workflows/test-and-deploy.yml +++ b/.github/workflows/test-and-deploy.yml @@ -24,7 +24,10 @@ jobs: - name: Setup .NET Core SDK uses: actions/setup-dotnet@v3 with: - dotnet-version: '3.1.x' + dotnet-version: 7.0.103 + + - name: Remove faulty SDK version + run: sudo rm -rf /usr/share/dotnet/sdk/7.0.4* - name: Build & Test run: make test diff --git a/src/Twilio/Converters/CustomConverters.cs b/src/Twilio/Converters/CustomConverters.cs new file mode 100644 index 000000000..448793732 --- /dev/null +++ b/src/Twilio/Converters/CustomConverters.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using Newtonsoft.Json; + + +namespace Twilio.Converters +{ + + public class DateTimeConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + // Serializing List objects + if (value is List dateTimes) + { + writer.WriteStartArray(); + foreach (var dateTime in dateTimes) + { + writer.WriteValue(Serializers.DateTimeIso8601(dateTime)); + } + + writer.WriteEndArray(); + } + else + { // Serializing DateTime object + writer.WriteValue(Serializers.DateTimeIso8601((DateTime)value)); + } + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.StartArray) + { + var dateTimes = new List(); + + while (reader.Read()) + { + if (reader.TokenType == JsonToken.String) + { + if (DateTime.TryParse((string)reader.Value, out DateTime dateTime)) + { + dateTimes.Add(dateTime); + } + } + else if (reader.TokenType == JsonToken.EndArray) + { + return dateTimes; + } + } + } + else if (reader.TokenType == JsonToken.String) + { + if (DateTime.TryParse((string)reader.Value, out DateTime dateTime)) + { + return dateTime; + } + } + throw new JsonSerializationException("Failed to deserialize DateTime."); + } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(DateTime?) || objectType ==typeof(List); + } + } + + public class UriConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + // Serializing List objects + if (value is List uris) + { + writer.WriteStartArray(); + foreach (var uri in uris) + { + writer.WriteValue(Serializers.Url(uri)); + } + + writer.WriteEndArray(); + } + else + { // Serializing Uri object + writer.WriteValue(Serializers.Url((Uri)value)); + } + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.StartArray) + { + var uris = new List(); + + while (reader.Read()) + { + if (reader.TokenType == JsonToken.String) + { + if (Uri.TryCreate((string)reader.Value, UriKind.RelativeOrAbsolute, out Uri uri)) + { + uris.Add(uri); + } + } + else if (reader.TokenType == JsonToken.EndArray) + { + return uris; + } + } + } + else if (reader.TokenType == JsonToken.String) + { + if (Uri.TryCreate((string)reader.Value, UriKind.RelativeOrAbsolute, out Uri uri)) + { + return uri; + } + } + throw new JsonSerializationException("Failed to deserialize URI."); + } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(Uri); + } + } + + public class JsonObjectConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + // Serializing List objects + if (value is List objects) + { + writer.WriteStartArray(); + foreach (var val in objects) + { + writer.WriteValue(Serializers.JsonObject(val)); + } + + writer.WriteEndArray(); + } + else + { // Serializing Uri object + writer.WriteValue(Serializers.JsonObject(value)); + } + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.StartArray) + { + var objects = new List(); + + while (reader.Read()) + { + if (reader.TokenType == JsonToken.String) + { + if (Uri.TryCreate((string)reader.Value, UriKind.RelativeOrAbsolute, out Uri uri)) + { + objects.Add(uri); + } + } + else if (reader.TokenType == JsonToken.EndArray) + { + return objects; + } + } + } + else if (reader.TokenType == JsonToken.String) + { + if (Uri.TryCreate((string)reader.Value, UriKind.RelativeOrAbsolute, out Uri uri)) + { + return uri; + } + } + throw new JsonSerializationException("Failed to deserialize object."); + } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(Uri); + } + } +} \ No newline at end of file