-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDynamicJsonObject.cs
More file actions
70 lines (60 loc) · 2.37 KB
/
Copy pathDynamicJsonObject.cs
File metadata and controls
70 lines (60 loc) · 2.37 KB
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
69
70
// This code is provided under the MIT license. Originally by Alessandro Pilati.
// based on http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
namespace SnowyPeak.Duality.Plugin.Data
{
public class DynamicJsonObject : DynamicObject
{
private IDictionary<string, object> _dictionary { get; set; }
public DynamicJsonObject(IDictionary<string, object> dictionary)
{
_dictionary = dictionary;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _dictionary[binder.Name];
if (result is IDictionary<string, object>)
{
result = new DynamicJsonObject(result as IDictionary<string, object>);
}
else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
{
result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>)));
}
else if (result is ArrayList)
{
result = new List<object>((result as ArrayList).ToArray());
}
return _dictionary.ContainsKey(binder.Name);
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _dictionary.Keys;
}
public object this[string key]
{
get
{
object result = _dictionary[key];
if (result is IDictionary<string, object>)
{
result = new DynamicJsonObject(result as IDictionary<string, object>);
}
else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
{
result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>)));
}
else if (result is ArrayList)
{
result = new List<object>((result as ArrayList).ToArray());
}
return result;
}
}
}
}