Skip to content

Commit

Permalink
fix: add new Twiml type to deal with constructor overloading issue (#502
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Craig Dennis authored and childish-sambino committed Nov 15, 2019
1 parent 7f3e110 commit e359e25
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Twilio/Converters/TwimlConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Twilio.Converters
{
/// <summary>
/// Convert between strings and a Twiml
/// </summary>
public class TwimlConverter : JsonConverter
{
/// <summary>
/// Write value to JsonWriter
/// </summary>
/// <param name="writer">Writer to write to</param>
/// <param name="value">Value to write</param>
/// <param name="serializer">unused</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var t = JToken.FromObject(value.ToString());
t.WriteTo(writer);
}

/// <summary>
/// Convert a string to a Twiml
/// </summary>
/// <param name="reader">JsonReader to read from</param>
/// <param name="objectType">unused</param>
/// <param name="existingValue">unused</param>
/// <param name="serializer">unused</param>
/// <returns>Converted Twiml</returns>
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer
)
{
return new Types.Twiml(reader.Value as string);
}

/// <summary>
/// Determines if an object converted to a Twiml
/// </summary>
/// <param name="objectType">Type of object</param>
/// <returns>true if an object can be converted; false otherwise</returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Enum);
}
}
}
39 changes: 39 additions & 0 deletions src/Twilio/Types/Twiml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Twilio.Types
{
/// <summary>
/// Twiml endpoint
/// </summary>
public class Twiml
{
private readonly string _twiml;

/// <summary>
/// Create a new Twiml
/// </summary>
/// <param name="twiml">Twiml</param>
public Twiml(string twiml)
{
_twiml = twiml;
}

/// <summary>
/// Add implicit constructor for Twiml to make it assignable from string
/// </summary>
/// <param name="twiml">Twiml</param>
/// <returns></returns>
public static implicit operator Twiml(string twiml)
{
return new Twiml(twiml);
}

/// <summary>
/// Convert to string
/// </summary>
/// <returns>String representation</returns>
public override string ToString()
{
return _twiml;
}
}
}

0 comments on commit e359e25

Please sign in to comment.