-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add new Twiml type to deal with constructor overloading issue (#502
- Loading branch information
Craig Dennis
authored and
childish-sambino
committed
Nov 15, 2019
1 parent
7f3e110
commit e359e25
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|