Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added custom converters for nested objects #688

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/test-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
182 changes: 182 additions & 0 deletions src/Twilio/Converters/CustomConverters.cs
Original file line number Diff line number Diff line change
@@ -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<DateTime> objects
if (value is List<DateTime> 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<DateTime>();

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<DateTime>);
}
}

public class UriConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// Serializing List<Uri> objects
if (value is List<Uri> 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<Uri>();

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<Uri> objects
if (value is List<object> 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<object>();

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);
}
}
}
Loading