summaryrefslogtreecommitdiff
path: root/ddl/out/java/LambdaCube/TypeInfo/JSON.java
blob: e13fa63bc2682d86e6c437bca9f9b6be90ba9308 (plain)
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
// generated file, do not modify!
// 2016-09-15T19:44:48.120020034Z

package LambdaCube.TypeInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.*;
import RT.*;

import LambdaCube.IR.*;

public class JSON {
  // JSON deserializer
  public enum Type { 
    Int,
    String,
    Array_Range,
    Array_TypeInfo,
    CompileResult,
    Pipeline,
    Range,
    TypeInfo
  }

  public static Object fromJSON(Type type, Object rawObj) throws JSONException, Exception {
    switch (type) {
      case Int: return ((Number)rawObj).intValue();
      case String: return (String)rawObj;
      case Array_Range: {
        JSONArray obj = (JSONArray)rawObj;
        ArrayList<Range> v = new ArrayList<Range> ();
        for (int i = 0; i < obj.length(); i++) {
          v.add((Range)fromJSON (Type.Range, obj.get(i)));
        }
        return v;
      }
      case Array_TypeInfo: {
        JSONArray obj = (JSONArray)rawObj;
        ArrayList<TypeInfo> v = new ArrayList<TypeInfo> ();
        for (int i = 0; i < obj.length(); i++) {
          v.add((TypeInfo)fromJSON (Type.TypeInfo, obj.get(i)));
        }
        return v;
      }

      case Range: {
        JSONObject obj = (JSONObject)rawObj;
        String tag = obj.getString("tag");
        switch (tag) {
          case "Range": {
            Range.Range_ tv = new Range().new Range_();
            tv.startLine = (Integer)fromJSON(Type.Int,obj.get("startLine"));
            tv.startColumn = (Integer)fromJSON(Type.Int,obj.get("startColumn"));
            tv.endLine = (Integer)fromJSON(Type.Int,obj.get("endLine"));
            tv.endColumn = (Integer)fromJSON(Type.Int,obj.get("endColumn"));
            return tv;
          }
          default: throw new Exception("unknown constructor: " + tag);
        }
      }
      case TypeInfo: {
        JSONObject obj = (JSONObject)rawObj;
        String tag = obj.getString("tag");
        switch (tag) {
          case "TypeInfo": {
            TypeInfo.TypeInfo_ tv = new TypeInfo().new TypeInfo_();
            tv.range = (Range)fromJSON(Type.Range,obj.get("range"));
            tv.text = (String)fromJSON(Type.String,obj.get("text"));
            return tv;
          }
          default: throw new Exception("unknown constructor: " + tag);
        }
      }
      case CompileResult: {
        JSONObject obj = (JSONObject)rawObj;
        String tag = obj.getString("tag");
        switch (tag) {
          case "CompileError": {
            CompileResult.CompileError_ tv = new CompileResult().new CompileError_();
            tv._0 = (ArrayList<Range>)fromJSON(Type.Array_Range,obj.get("arg0"));
            tv._1 = (String)fromJSON(Type.String,obj.get("arg1"));
            tv._2 = (ArrayList<TypeInfo>)fromJSON(Type.Array_TypeInfo,obj.get("arg2"));
            return tv;
          }
          case "Compiled": {
            CompileResult.Compiled_ tv = new CompileResult().new Compiled_();
            tv._0 = (String)fromJSON(Type.String,obj.get("arg0"));
            tv._1 = (String)fromJSON(Type.String,obj.get("arg1"));
            tv._2 = (Pipeline)fromJSON(Type.Pipeline,obj.get("arg2"));
            tv._3 = (ArrayList<TypeInfo>)fromJSON(Type.Array_TypeInfo,obj.get("arg3"));
            return tv;
          }
          default: throw new Exception("unknown constructor: " + tag);
        }
      }
    }
    throw new Exception("unknown type");
  }

  public static Object toJSON(Type type, Object rawObj) throws JSONException, Exception {
    switch (type) {
      case Range: {
        Range v = (Range)rawObj;
        JSONObject obj = new JSONObject();
        switch (v.tag) { 
          case Range:
            obj.put("tag", "Range");
            {
              Range.Range_ tv = (Range.Range_)v;
              obj.put("startLine", toJSON(Type.Int,tv.startLine));
              obj.put("startColumn", toJSON(Type.Int,tv.startColumn));
              obj.put("endLine", toJSON(Type.Int,tv.endLine));
              obj.put("endColumn", toJSON(Type.Int,tv.endColumn));
            }
            break;
        }
        return obj;
      }
      case TypeInfo: {
        TypeInfo v = (TypeInfo)rawObj;
        JSONObject obj = new JSONObject();
        switch (v.tag) { 
          case TypeInfo:
            obj.put("tag", "TypeInfo");
            {
              TypeInfo.TypeInfo_ tv = (TypeInfo.TypeInfo_)v;
              obj.put("range", toJSON(Type.Range,tv.range));
              obj.put("text", toJSON(Type.String,tv.text));
            }
            break;
        }
        return obj;
      }
      case CompileResult: {
        CompileResult v = (CompileResult)rawObj;
        JSONObject obj = new JSONObject();
        switch (v.tag) { 
          case CompileError:
            obj.put("tag", "CompileError");
            {
              CompileResult.CompileError_ tv = (CompileResult.CompileError_)v;
              obj.put("arg0", toJSON(Type.Array_Range,tv._0));
              obj.put("arg1", toJSON(Type.String,tv._1));
              obj.put("arg2", toJSON(Type.Array_TypeInfo,tv._2));
            }
            break;
          case Compiled:
            obj.put("tag", "Compiled");
            {
              CompileResult.Compiled_ tv = (CompileResult.Compiled_)v;
              obj.put("arg0", toJSON(Type.String,tv._0));
              obj.put("arg1", toJSON(Type.String,tv._1));
              obj.put("arg2", toJSON(Type.Pipeline,tv._2));
              obj.put("arg3", toJSON(Type.Array_TypeInfo,tv._3));
            }
            break;
        }
        return obj;
      }

      case Int: { return rawObj; }
      case String: { return rawObj; }
      case Array_Range: {
        ArrayList<Range> v = (ArrayList<Range>)rawObj;
        JSONArray obj = new JSONArray();
        for(Range i : v) {
          obj.put(toJSON(Type.Range,i));
        }
        return obj;
      }
      case Array_TypeInfo: {
        ArrayList<TypeInfo> v = (ArrayList<TypeInfo>)rawObj;
        JSONArray obj = new JSONArray();
        for(TypeInfo i : v) {
          obj.put(toJSON(Type.TypeInfo,i));
        }
        return obj;
      }
    }
    throw new Exception("unknown type");
  }
}