-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson.sc
49 lines (41 loc) · 1.07 KB
/
Json.sc
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
JSON {
*convert {|obj| ^JSON.toJSON(obj); }
*toJSON { |obj|
switch(obj.class.name,
\Dictionary, {^JSON.dictToJSON(obj)},
\List, { ^JSON.listToJSON(obj) },
\Array, { ^JSON.listToJSON(obj) },
\Association, { ^JSON.pairToJSON(obj)},
\Set, {^JSON.listToJSON(obj)},
{
if(obj.isKindOf(Number)) {
^obj.asString;
}
^obj.asString.quote;
}
);
}
*pairToJSON { |pair|
pair.postln;
}
*dictToJSON { |dict|
var str = "{";
var new_lst = List.new;
dict.keysValuesDo { |k, v|
new_lst.add(JSON.toJSON(k) ++ ":" + JSON.toJSON(v));
};
str = str ++ new_lst.join(", ");
str = str ++ "}";
^str;
}
*listToJSON { |lst|
var str = "[";
var new_lst = List.new;
lst.do { |v|
new_lst.add(JSON.toJSON(v));
};
str = str + new_lst.join(", ");
str = str + "]";
^str;
}
}