-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataListener.cs
244 lines (211 loc) · 10.9 KB
/
DataListener.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;
namespace Lemmings {
public class DataListener: NameableComponent {
public bool JSONIsPopulated;
public String JSONString;
enum DataStructure { Singleton, List, Tree };
public DataListener()
: base("Lemmings Data Listener", "DataListen", "Saves textual data as JSON to a Lemmings ZIP file") {
this.JSONIsPopulated = false;
this.JSONString = "";
this.namePrefix = "dvar_";
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddGenericParameter("Values", "V", "Any kind of data (we'll do what we can)", GH_ParamAccess.tree);
//TODO: add flag to allow/disallow nulls
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {
pManager.AddTextParameter("JSON String", "J", "Resulting JSON string", GH_ParamAccess.item);
}
protected override void SolveInstance(IGH_DataAccess DA) {
this.JSONIsPopulated = false;
this.JSONString = "";
Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo> tree = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo>();
if (!DA.GetDataTree(0, out tree)) { return; }
String JSONOpen = "";
String JSONClose = "";
JSONOpen = @"""" + this.ComponentName + @""":{";
JSONClose = "}";
DataStructure dim;
if (tree.PathCount == 1) {
if (tree.DataCount == 1) dim = DataStructure.Singleton; // a singleton
else dim = DataStructure.List; // a list
} else dim = DataStructure.Tree; // a tree
JSONOpen += @"""DataStructure"":""" + dim.ToString() + @""",";
String UniformType = "";
bool IsUniform = JSONConverter.UniformDataType(tree, ref UniformType);
if (IsUniform) JSONOpen += @"""Type"":""" + UniformType + @""",";
if (dim == DataStructure.Singleton) {
// SINGLETONS
//
String ObjString = "";
IGH_Goo obj = tree.get_FirstItem(false);
if ((obj != null)&&(JSONConverter.ObjectToJSON(obj, ref ObjString,false))){
this.JSONString = JSONOpen + @"""Value"":"+ObjString + JSONClose;
DA.SetData(0, "{" + this.JSONString + "}");
this.JSONIsPopulated = true;
return;
} else {
// TODO: return failed item
this.JSONIsPopulated = false;
return;
}
} else if (dim == DataStructure.List) {
// LISTS
//
JSONOpen += @"""Items"":{";
JSONClose += "}";
List<IGH_Goo> failures = new List<IGH_Goo>();
List<String> ObjStrings = JSONConverter.ObjectsToJSON(tree.get_Branch(tree.Paths[0]), ref failures, !IsUniform);
if (failures.Count > 0) AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Some items failed to be converted to JSON.");
if (ObjStrings.Count == 0) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Nothing was successfully converted to JSON.");
this.JSONIsPopulated = false;
return;
}
this.JSONString = JSONOpen + String.Join(",", ObjStrings.ToArray()) + JSONClose;
DA.SetData(0, "{"+this.JSONString+"}");
this.JSONIsPopulated = true;
// TODO: return failed items
return;
} else {
// TREES
//
JSONOpen += @"""BranchCount"":" + tree.Branches.Count + @",""DataCount"":" + tree.DataCount + @", ""Paths"":{";
JSONClose += "}";
List<IGH_Goo> failures = new List<IGH_Goo>();
List<String> BranchStrings = new List<string>();
foreach (Grasshopper.Kernel.Data.GH_Path path in tree.Paths) {
String BRANCHOpen = @"""" + path.ToString() + @""":{";
String BRANCHClose = "}";
List<String> ObjStrings = JSONConverter.ObjectsToJSON(tree.get_Branch(path), ref failures, !IsUniform);
if (ObjStrings.Count >= 0) {
BranchStrings.Add(BRANCHOpen + String.Join(",", ObjStrings.ToArray()) + BRANCHClose);
} else {
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "An entire branch failed to be converted to JSON.");
}
}
if (failures.Count > 0) AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Some items failed to be converted to JSON.");
this.JSONString = JSONOpen + String.Join(",", BranchStrings.ToArray()) + JSONClose;
DA.SetData(0, "{" + this.JSONString + "}");
this.JSONIsPopulated = true;
// TODO: return failed items
return;
}
//if (JSONConverter.ObjectToJSON(obj, ref JSONString)) DA.SetData(0, JSONString);
//DA.SetData(0, tree.PathCount.ToString());
}
protected override System.Drawing.Bitmap Icon {
get {return Lemmings.Properties.Resources.Icons_DataListener;}
}
public override Guid ComponentGuid { get { return new Guid("{0f35898d-021f-4093-bc10-d815730eb0cc}"); } }
}
public class JSONConverter {
public static String TypeToString(IGH_Goo obj) {
String[] split = obj.GetType().ToString().Split('.');
return split[split.Length - 1];
}
public static bool UniformDataType(Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo> tree, ref String UniformType) {
if (tree.get_FirstItem(true) == null) return false;
Type t = tree.get_FirstItem(true).GetType();
foreach (IGH_Goo obj in tree.AllData(false)) {
if (obj == null) return false;
if (obj.GetType() != t) return false;
}
UniformType = TypeToString(tree.get_FirstItem(true));
return true;
}
public static List<string> ObjectsToJSON(System.Collections.IList objects, ref List<IGH_Goo> failures, bool IncludeType = true, bool AllowNulls = true) {
List<String> ObjStrings = new List<String>();
int n = 0;
foreach (IGH_Goo obj in objects) {
String str = "";
if (JSONConverter.ObjectToJSON(obj, ref str, IncludeType, AllowNulls)) ObjStrings.Add(@"""" + n.ToString() + @""":" + str);
else failures.Add(obj);
n++;
}
return ObjStrings;
}
public static bool ObjectToJSON(IGH_Goo obj, ref String str, bool IncludeType = true, bool AllowNulls = true) {
if (obj == null) {
if (!AllowNulls) return false;
str = @"{""Type"":""null""}";
return true;
}
if (obj.GetType() == typeof(GH_Boolean)){
String val = @"""" + ((GH_Boolean)obj).Value.ToString() + @"""";
if (IncludeType) str = @"{""Type"":"""+TypeToString(obj)+@""",""Value"":" + val + @"}";
else str = val;
return true;
}
if (obj.GetType() == typeof(GH_Number)){
String val = ((GH_Number)obj).Value.ToString();
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",""Value"":" + val + @"}";
else str = val;
return true;
}
if (obj.GetType() == typeof(GH_Integer)) {
String val = ((GH_Integer)obj).Value.ToString();
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",""Value"":" + val + @"}";
else str = val;
return true;
}
if (obj.GetType() == typeof(GH_String)) {
//TODO: escape bad string characters according to http://stackoverflow.com/questions/3020094/how-should-i-escape-strings-in-json
String val = @"""" + ((GH_String)obj).Value.ToString().Replace(@"""",@"\""") + @"""";
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",""Value"":" + val + @"}";
else str = val;
return true;
}
if (obj.GetType() == typeof(GH_Guid)) {
String val = @"""" + ((GH_Guid)obj).Value.ToString() + @"""";
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",""Value"":" + val + @"}";
else str = val;
return true;
}
if (obj.GetType() == typeof(GH_Time)) {
String val = @"""" + ((GH_Time)obj).Value.ToString() + @"""";
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",""Value"":" + val + @"}";
else str = val;
return true;
}
if (obj.GetType() == typeof(GH_Colour)) {
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",";
else str = @"{";
GH_Colour clr = (GH_Colour)obj;
str += @"""A"":" + clr.Value.A.ToString() + ",";
str += @"""R"":" + clr.Value.R.ToString() + ",";
str += @"""G"":" + clr.Value.G.ToString() + ",";
str += @"""B"":" + clr.Value.B.ToString();
str += "}";
return true;
}
if (obj.GetType() == typeof(GH_Interval)) {
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",";
else str = @"{";
GH_Interval ival = (GH_Interval)obj;
str += @"""T0"":" + ival.Value.T0.ToString() + ",";
str += @"""T1"":" + ival.Value.T1.ToString();
str += "}";
return true;
}
if (obj.GetType() == typeof(GH_Interval2D)) {
if (IncludeType) str = @"{""Type"":""" + TypeToString(obj) + @""",";
else str = @"{";
GH_Interval2D ival = (GH_Interval2D)obj;
str += @"""U0"":" + ival.Value.U0.ToString() + ",";
str += @"""U1"":" + ival.Value.U1.ToString() + ",";
str += @"""V0"":" + ival.Value.V0.ToString() + ",";
str += @"""V1"":" + ival.Value.V1.ToString();
str += "}";
return true;
}
return false;
}
}
}