Skip to content

Commit fe3796a

Browse files
committed
Merge master into alpha
1 parent 5096698 commit fe3796a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Twilio.Converters
4+
{
5+
/// <summary>
6+
/// Serialization methods for various datatypes before making requests to the API
7+
/// </summary>
8+
public class Serializers
9+
{
10+
11+
/// <summary>
12+
/// Produce a json string from input if possible
13+
/// </summary>
14+
/// <param name="input">Object to serialize to json</param>
15+
/// <returns>A json string</returns>
16+
public static string JsonObject(object input)
17+
{
18+
return (input is string) ? (string) input : JsonConvert.SerializeObject(input);
19+
}
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
using Twilio.Converters;
4+
5+
namespace Twilio.Tests.Converters
6+
{
7+
[TestFixture]
8+
public class SerializersTest : TwilioTest {
9+
10+
[Test]
11+
public void TestJsonObjectSerializesDictionary()
12+
{
13+
var inputDict = new Dictionary<string, string> {{"twilio", "rocks"}};
14+
var result = Serializers.JsonObject(inputDict);
15+
Assert.AreEqual("{\"twilio\":\"rocks\"}", result);
16+
}
17+
18+
[Test]
19+
public void TestJsonObjectSerializesList()
20+
{
21+
var inputDict = new List<object>{
22+
"twilio",
23+
new Dictionary<string, string> {{"join", "us"}}
24+
};
25+
var result = Serializers.JsonObject(inputDict);
26+
Assert.AreEqual("[\"twilio\",{\"join\":\"us\"}]", result);
27+
}
28+
29+
[Test]
30+
public void TestJsonObjectSerializesArray()
31+
{
32+
string[] inputDict = new string[2] {"twilio", "rocks"};
33+
var result = Serializers.JsonObject(inputDict);
34+
Assert.AreEqual("[\"twilio\",\"rocks\"]", result);
35+
}
36+
37+
[Test]
38+
public void TestJsonObjectPassesThroughString()
39+
{
40+
var input = "{\"twilio\":\"is dope\"}";
41+
var result = Serializers.JsonObject(input);
42+
Assert.AreEqual(input, result);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)