-
Notifications
You must be signed in to change notification settings - Fork 7
/
FriendlyDynamic.cs
183 lines (149 loc) · 5.35 KB
/
FriendlyDynamic.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
namespace HastyAPI {
/// <summary>
/// A javascript-like dynamic object that returns null for undefined properties
/// </summary>
public class FriendlyDynamic : DynamicObject, IDictionary<string, object> {
Dictionary<string, object> _dictionary = new Dictionary<string, object>();
public override bool TrySetMember(SetMemberBinder binder, object value) {
_dictionary[binder.Name] = value;
return true;
}
public override bool TryGetMember(GetMemberBinder binder, out object result) {
if(!_dictionary.TryGetValue(binder.Name, out result))
result = null;
return true;
}
public override string ToString() {
return DynamicToString(this, 0, false);
}
private static string DynamicToString(FriendlyDynamic dyn, int level, bool insideList) {
var canCompact = (level < 2 || insideList) && CanCompact(dyn);
var str = "{";
str += canCompact ? " " : "\r\n";
var i = 0;
foreach(var pair in dyn._dictionary) {
str += PairToString(pair, level + 1, canCompact);
if(i < dyn._dictionary.Count - 1) str += "," + (canCompact ? " " : "");
str += canCompact ? "" : "\r\n";
i++;
}
str += (canCompact ? " " : Indent(level)) + "}";
return str;
}
private static string PairToString(KeyValuePair<string, object> pair, int level, bool canCompact) {
var str = canCompact ? "" : Indent(level);
str += "\"" + pair.Key + "\": " + ObjectToString(pair.Value, level, false);
return str;
}
private static string ObjectToString(object obj, int level, bool insideList) {
if(obj is IList<object>) {
return ListToString(obj as IList<object>, level);
} else if(obj is FriendlyDynamic) {
return DynamicToString(obj as FriendlyDynamic, level, insideList);
} else if(obj is string) {
return "\"" + obj + "\"";
} else if(obj is bool) {
return obj.ToString().ToLower();
} else return obj.ToString();
}
private static string ListToString(IList<object> list, int level) {
var canCompact = CanCompact(list);
var str = "[";
str += canCompact ? "" : "\r\n";
var i = 0;
foreach(var obj in list) {
str += (canCompact ? (i == 0 ? "" : " ") : Indent(level + 1)) + ObjectToString(obj, level + 1, true);
if(i < list.Count - 1) str += ",";
str += canCompact ? "" : "\r\n";
i++;
}
str += (canCompact ? "" : Indent(level)) + "]";
return str;
}
private static bool CanCompact(FriendlyDynamic dyn) {
if(dyn._dictionary.Count > 5) return false;
foreach(var pair in dyn._dictionary) {
if(!CanCompact(pair.Value)) return false;
}
return true;
}
private static bool CanCompact(object obj) {
if(obj is List<object>) return false;
if(obj is FriendlyDynamic) return false;
return true;
}
private static bool CanCompact(IList<object> list) {
foreach(var obj in list) {
if(!CanCompact(obj)) return false;
}
return true;
}
private static string Indent(int level) {
return new string(' ', level * 4);
}
#region IDictionary<TKey, TValue> implementation
void IDictionary<string, object>.Add(string key, object value) {
_dictionary.Add(key, value);
}
bool IDictionary<string, object>.ContainsKey(string key) {
return _dictionary.ContainsKey(key);
}
ICollection<string> IDictionary<string, object>.Keys {
get { return _dictionary.Keys; }
}
bool IDictionary<string, object>.Remove(string key) {
return _dictionary.Remove(key);
}
bool IDictionary<string, object>.TryGetValue(string key, out object value) {
return _dictionary.TryGetValue(key, out value);
}
ICollection<object> IDictionary<string, object>.Values {
get { return _dictionary.Values; }
}
object IDictionary<string, object>.this[string key] {
get {
return _dictionary[key];
}
set {
_dictionary[key] = value;
}
}
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item) {
((ICollection<KeyValuePair<string, object>>)_dictionary).Add(item);
}
void ICollection<KeyValuePair<string, object>>.Clear() {
((ICollection<KeyValuePair<string, object>>)_dictionary).Clear();
}
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item) {
return ((ICollection<KeyValuePair<string, object>>)_dictionary).Contains(item);
}
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) {
((ICollection<KeyValuePair<string, object>>)_dictionary).CopyTo(array, arrayIndex);
}
int ICollection<KeyValuePair<string, object>>.Count {
get {
return ((ICollection<KeyValuePair<string, object>>)_dictionary).Count;
}
}
bool ICollection<KeyValuePair<string, object>>.IsReadOnly {
get {
return ((ICollection<KeyValuePair<string, object>>)_dictionary).IsReadOnly;
}
}
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item) {
return ((ICollection<KeyValuePair<string, object>>)_dictionary).Remove(item);
}
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() {
return ((ICollection<KeyValuePair<string, object>>)_dictionary).GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return ((System.Collections.IEnumerable)_dictionary).GetEnumerator();
}
#endregion
}
}