/
Utilities.cs
68 lines (60 loc) · 2.41 KB
/
Utilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Nancy;
using Nancy.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Web;
namespace StockQuoteService
{
public static class Utilities
{
//turns an IDictionary into an ExpandoObject
public static ExpandoObject DictionaryToExpando(this IDictionary<string, object> dictionary)
{
var expando = new ExpandoObject();
var expandoDic = (IDictionary<string, object>)expando;
foreach (var item in dictionary)
{
bool alreadyProcessed = false;
if (item.Value is IDictionary<string, object>)
{
expandoDic.Add(item.Key, DictionaryToExpando((IDictionary<string, object>)item.Value));
alreadyProcessed = true;
}
else if (item.Value is ICollection)
{
var itemList = new List<object>();
foreach (var item2 in (ICollection)item.Value)
if (item2 is IDictionary<string, object>)
itemList.Add(DictionaryToExpando((IDictionary<string, object>)item2));
else
itemList.Add(DictionaryToExpando(new Dictionary<string, object> { { "Unknown", item2 } }));
if (itemList.Count > 0)
{
expandoDic.Add(item.Key, itemList);
alreadyProcessed = true;
}
}
if (!alreadyProcessed)
expandoDic.Add(item);
}
return expando;
}
//will attempt to serialize an object of any type
public static Response SerializeToJsonResponse(dynamic input)
{
Nancy.Json.JavaScriptSerializer ser = new JavaScriptSerializer();
var response = (Response)ser.Serialize(input);
response.ContentType = "application/json";
return response;
}
//will deserialize json into an expando object
public static ExpandoObject DeserializeJsonToExpando(string json)
{
Nancy.Json.JavaScriptSerializer ser = new Nancy.Json.JavaScriptSerializer();
var dictionary = ser.Deserialize<IDictionary<string, object>>(json);
return dictionary.DictionaryToExpando();
}
}
}