summaryrefslogtreecommitdiff
path: root/ddl/templates
diff options
context:
space:
mode:
Diffstat (limited to 'ddl/templates')
-rw-r--r--ddl/templates/data.cs.ede46
-rw-r--r--ddl/templates/data.java.ede2
-rw-r--r--ddl/templates/json.java.ede114
3 files changed, 139 insertions, 23 deletions
diff --git a/ddl/templates/data.cs.ede b/ddl/templates/data.cs.ede
index dffad32..74e0dac 100644
--- a/ddl/templates/data.cs.ede
+++ b/ddl/templates/data.cs.ede
@@ -33,7 +33,7 @@ namespace {{ moduleName }} {
33 33
34 // JSON deserializer 34 // JSON deserializer
35 enum Type { {% for t in usedTypes %} 35 enum Type { {% for t in usedTypes %}
36 {{ t.value | csTypeEnum }}{% if !t.last %},{% endif %}{% endfor %} 36 {{ t.value | typeEnum }}{% if !t.last %},{% endif %}{% endfor %}
37 } 37 }
38 38
39 class Loader { 39 class Loader {
@@ -43,52 +43,52 @@ namespace {{ moduleName }} {
43 {% case t.value.tag %} 43 {% case t.value.tag %}
44 {% when "Data" %} 44 {% when "Data" %}
45 {% when "V2" %} 45 {% when "V2" %}
46 case Type.{{ t.value | csTypeEnum }}: 46 case Type.{{ t.value | typeEnum }}:
47 return new {{ t.value | csType }} 47 return new {{ t.value | csType }}
48 { x = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["x"]) 48 { x = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["x"])
49 , y = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["y"]) 49 , y = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["y"])
50 }; 50 };
51 {% when "V3" %} 51 {% when "V3" %}
52 case Type.{{ t.value | csTypeEnum }}: 52 case Type.{{ t.value | typeEnum }}:
53 return new {{ t.value | csType }} 53 return new {{ t.value | csType }}
54 { x = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["x"]) 54 { x = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["x"])
55 , y = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["y"]) 55 , y = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["y"])
56 , z = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["z"]) 56 , z = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["z"])
57 }; 57 };
58 {% when "V4" %} 58 {% when "V4" %}
59 case Type.{{ t.value | csTypeEnum }}: 59 case Type.{{ t.value | typeEnum }}:
60 return new {{ t.value | csType }} 60 return new {{ t.value | csType }}
61 { x = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["x"]) 61 { x = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["x"])
62 , y = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["y"]) 62 , y = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["y"])
63 , z = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["z"]) 63 , z = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["z"])
64 , w = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | csTypeEnum }},obj["w"]) 64 , w = ({{ t.value.type_ | csType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj["w"])
65 }; 65 };
66 {% when "Map" %} 66 {% when "Map" %}
67 case Type.{{ t.value | csTypeEnum }}: { 67 case Type.{{ t.value | typeEnum }}: {
68 var map = new {{ t.value | csType }} (); 68 var map = new {{ t.value | csType }} ();
69 foreach(var i in (JObject)obj) { 69 foreach(var i in (JObject)obj) {
70 map.Add( ({{ t.value.key_ | csType }})fromJSON(Type.{{ t.value.key_ | csTypeEnum }},i.Key) 70 map.Add( ({{ t.value.key_ | csType }})fromJSON(Type.{{ t.value.key_ | typeEnum }},i.Key)
71 , ({{ t.value.value_ | csType }})fromJSON(Type.{{ t.value.value_ | csTypeEnum }},i.Value)); 71 , ({{ t.value.value_ | csType }})fromJSON(Type.{{ t.value.value_ | typeEnum }},i.Value));
72 } 72 }
73 return map; 73 return map;
74 } 74 }
75 {% when "Array" %} 75 {% when "Array" %}
76 case Type.{{ t.value | csTypeEnum }}: return ((JArray)obj).Select(x => fromJSON (Type.{{ t.value.type_ | csTypeEnum }}, x)).ToList(); 76 case Type.{{ t.value | typeEnum }}: return ((JArray)obj).Select(x => fromJSON (Type.{{ t.value.type_ | typeEnum }}, x)).ToList();
77 {% when "List" %} 77 {% when "List" %}
78 case Type.{{ t.value | csTypeEnum }}: return ((JArray)obj).Select(x => fromJSON (Type.{{ t.value.type_ | csTypeEnum }}, x)).ToList(); 78 case Type.{{ t.value | typeEnum }}: return ((JArray)obj).Select(x => fromJSON (Type.{{ t.value.type_ | typeEnum }}, x)).ToList();
79 {% when "Maybe" %} 79 {% when "Maybe" %}
80 case Type.{{ t.value | csTypeEnum }}: { 80 case Type.{{ t.value | typeEnum }}: {
81 var m = new {{ t.value | csType }} (); 81 var m = new {{ t.value | csType }} ();
82 if (obj == null || obj.Type == JTokenType.Null) { 82 if (obj == null || obj.Type == JTokenType.Null) {
83 m.valid = false; 83 m.valid = false;
84 } else { 84 } else {
85 m.valid = true; 85 m.valid = true;
86 m.data = ({{ t.value.type_ | csType }})fromJSON (Type.{{ t.value.type_ | csTypeEnum }},obj); 86 m.data = ({{ t.value.type_ | csType }})fromJSON (Type.{{ t.value.type_ | typeEnum }},obj);
87 } 87 }
88 return m; 88 return m;
89 } 89 }
90 {% else %} 90 {% else %}
91 case Type.{{ t.value | csTypeEnum }}: return ({{ t.value | csType }})obj; 91 case Type.{{ t.value | typeEnum }}: return ({{ t.value | csType }})obj;
92 {% endcase %} 92 {% endcase %}
93 {% endfor %} 93 {% endfor %}
94 94
@@ -101,8 +101,8 @@ namespace {{ moduleName }} {
101 tagType = {{ t.value.dataName }}.Tag.{{ c.value.name }}; 101 tagType = {{ t.value.dataName }}.Tag.{{ c.value.name }};
102 {% if !(c.value.fields | empty) %} 102 {% if !(c.value.fields | empty) %}
103 data.{{ c.value.name }} tv = new data.{{ c.value.name }}();{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %} 103 data.{{ c.value.name }} tv = new data.{{ c.value.name }}();{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %}
104 tv.{{ f.value.fieldName }} = ({{ f.value.fieldType | csType }})fromJSON(Type.{{ f.value.fieldType | csTypeEnum }},obj["{{ f.value.fieldName }}"]);{% else %} 104 tv.{{ f.value.fieldName }} = ({{ f.value.fieldType | csType }})fromJSON(Type.{{ f.value.fieldType | typeEnum }},obj["{{ f.value.fieldName }}"]);{% else %}
105 tv._{{ f.index0 }} = ({{ f.value.fieldType | csType }})fromJSON(Type.{{ f.value.fieldType | csTypeEnum }},obj["arg{{ f.index0 }}"]);{% endif %}{% endfor %} 105 tv._{{ f.index0 }} = ({{ f.value.fieldType | csType }})fromJSON(Type.{{ f.value.fieldType | typeEnum }},obj["arg{{ f.index0 }}"]);{% endif %}{% endfor %}
106 return tv; 106 return tv;
107 {% endif %} 107 {% endif %}
108 } 108 }
diff --git a/ddl/templates/data.java.ede b/ddl/templates/data.java.ede
index 2aec628..71e3bd2 100644
--- a/ddl/templates/data.java.ede
+++ b/ddl/templates/data.java.ede
@@ -5,6 +5,7 @@ package {{ moduleName }};
5 5
6import java.util.ArrayList; 6import java.util.ArrayList;
7import java.util.HashMap; 7import java.util.HashMap;
8import RT.*;
8 9
9{% for m in imports %} 10{% for m in imports %}
10import {{ m.value }}.*; 11import {{ m.value }}.*;
@@ -21,6 +22,7 @@ public class {{ def.dataName }} {
21 public class {{ c.value.name }}_ extends {{ def.dataName }} { {% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %} 22 public class {{ c.value.name }}_ extends {{ def.dataName }} { {% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %}
22 public {{ f.value.fieldType | javaType }} {{ f.value.fieldName }};{% else %} 23 public {{ f.value.fieldType | javaType }} {{ f.value.fieldName }};{% else %}
23 public {{ f.value.fieldType | javaType }} _{{ f.index0 }};{% endif %}{% endfor %} 24 public {{ f.value.fieldType | javaType }} _{{ f.index0 }};{% endif %}{% endfor %}
25 public {{ c.value.name }}_() { tag = {{ def.dataName }}.Tag.{{ c.value.name }}; }
24 }{% endif %}{% endfor %} 26 }{% endif %}{% endfor %}
25} 27}
26 28
diff --git a/ddl/templates/json.java.ede b/ddl/templates/json.java.ede
new file mode 100644
index 0000000..35bbfd7
--- /dev/null
+++ b/ddl/templates/json.java.ede
@@ -0,0 +1,114 @@
1// generated file, do not modify!
2// {{ dateTime }}
3
4package {{ moduleName }};
5
6import java.util.ArrayList;
7import java.util.HashMap;
8import org.json.*;
9import RT.*;
10
11{% for m in imports %}
12import {{ m.value }}.*;
13{% endfor %}
14
15public class JSON {
16 // JSON deserializer
17 public enum Type { {% for t in usedTypes %}
18 {{ t.value | typeEnum }}{% if !t.last %},{% endif %}{% endfor %}
19 }
20
21 public static Object fromJSON(Type type, Object rawObj) throws JSONException, Exception {
22 switch (type) {
23 {% for t in usedTypes %}
24 {% case t.value.tag %}
25 {% when "Data" %}
26 {% when "V2" %}
27 case {{ t.value | typeEnum }}: {
28 JSONObject obj = (JSONObject)rawObj;
29 return new {{ t.value | javaType }}
30 ( ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("x"))
31 , ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("y"))
32 );
33 }
34 {% when "V3" %}
35 case {{ t.value | typeEnum }}: {
36 JSONObject obj = (JSONObject)rawObj;
37 return new {{ t.value | javaType }}
38 ( ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("x"))
39 , ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("y"))
40 , ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("z"))
41 );
42 }
43 {% when "V4" %}
44 case {{ t.value | typeEnum }}: {
45 JSONObject obj = (JSONObject)rawObj;
46 return new {{ t.value | javaType }}
47 ( ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("x"))
48 , ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("y"))
49 , ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("z"))
50 , ({{ t.value.type_ | javaType }})fromJSON(Type.{{ t.value.type_ | typeEnum }},obj.get("w"))
51 );
52 }
53 {% when "Map" %}
54/*
55 case {{ t.value | typeEnum }}: {
56 var map = new {{ t.value | javaType }} ();
57 foreach(var i in (JSONObject)rawObj) {
58 map.Add( ({{ t.value.key_ | javaType }})fromJSON(Type.{{ t.value.key_ | typeEnum }},i.Key)
59 , ({{ t.value.value_ | javaType }})fromJSON(Type.{{ t.value.value_ | typeEnum }},i.Value));
60 }
61 return map;
62 }
63*/
64 {% when "Array" %}
65// case {{ t.value | typeEnum }}: return ((JArray)obj).Select(x => fromJSON (Type.{{ t.value.type_ | typeEnum }}, x)).ToList();
66 {% when "List" %}
67// case {{ t.value | typeEnum }}: return ((JArray)obj).Select(x => fromJSON (Type.{{ t.value.type_ | typeEnum }}, x)).ToList();
68 {% when "Maybe" %}
69/*
70 case {{ t.value | typeEnum }}: {
71 var m = new {{ t.value | javaType }} ();
72 if (obj == null || obj.Type == JTokenType.Null) {
73 m.valid = false;
74 } else {
75 m.valid = true;
76 m.data = ({{ t.value.type_ | javaType }})fromJSON (Type.{{ t.value.type_ | typeEnum }},obj);
77 }
78 return m;
79 }
80*/
81 {% else %}
82 case {{ t.value | typeEnum }}: return ({{ t.value | javaType }})rawObj;
83 {% endcase %}
84 {% endfor %}
85
86 {% for t in definitions %}
87 case {{ t.value.dataName }}: {
88 JSONObject obj = (JSONObject)rawObj;
89 String tag = obj.getString("tag");
90 {{ t.value.dataName }}.Tag tagType;
91 switch (tag) {
92 {% for c in t.value.constructors %}
93 case "{{ c.value.name }}":{% if (c.value.fields | empty) %} tagType = {{ t.value.dataName }}.Tag.{{ c.value.name }}; break;
94{% else %} {
95 {{ t.value.dataName }}.{{ c.value.name }}_ tv = new {{ t.value.dataName }}().new {{ c.value.name }}_();{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %}
96 tv.{{ f.value.fieldName }} = ({{ f.value.fieldType | javaType }})fromJSON(Type.{{ f.value.fieldType | typeEnum }},obj.get("{{ f.value.fieldName }}"));{% else %}
97 tv._{{ f.index0 }} = ({{ f.value.fieldType | javaType }})fromJSON(Type.{{ f.value.fieldType | typeEnum }},obj.get("arg{{ f.index0 }}"));{% endif %}{% endfor %}
98 return tv;
99 }
100 {% endif %}
101 {% endfor %}
102 default: throw new Exception("unknown constructor: " + tag);
103 }
104 {% if t.value | hasEnumConstructor %}
105 {{ t.value.dataName }} o = new {{ t.value.dataName }}();
106 o.tag = tagType;
107 return o;
108 {% endif %}
109 }
110 {% endfor %}
111 }
112 throw new Exception("unknown type");
113 }
114} \ No newline at end of file