-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2model.py
executable file
·72 lines (52 loc) · 1.89 KB
/
json2model.py
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
#!/usr/bin/env python3
import sys
import json
from os.path import basename
from objcclass import ObjCClass, TYPE_TYPE_MAP
def to_singular(s):
# TODO ask interactively for singular form
if s[-1] == 's':
return s[0:-1]
return s
def traverse_node(node, name):
result = []
current = ObjCClass(name)
def is_container(x):
return isinstance(x, dict) or isinstance(x, list)
def update_result(result, k, v):
if isinstance(v, dict):
child_class_name = (k.title()+'Model')
result = result + traverse_node(v, child_class_name)
current.add_field(k, child_class_name+'*')
elif isinstance(v, list):
if len(v) and isinstance(v[0], dict):
result = result + traverse_node(v[0], to_singular(k).title())
current.add_field(k, "NSArray*")
else:
current.add_field(k, TYPE_TYPE_MAP[type(v)])
return result
for k, v in node.items():
result = update_result(result, k, v)
result.append(current)
return result
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: {} Foo.json".format(sys.argv[0]))
filename = sys.argv[1]
class_name = basename(filename).split('.')[0].title() + 'Model'
with open(filename) as fi:
root = json.load(fi)
if not isinstance(root, dict):
raise RuntimeError("input must be json dict")
objc_classes = traverse_node(root, class_name)
# XXX: test code
print("\n".join(map(str, objc_classes)))
for c in objc_classes:
print('===================================')
print(c.dump_header())
print('-----------------------------------')
print(c.dump_implementation())
with open(class_name+'.h', 'wt') as fo_h:
fo_h.write(c.dump_header())
with open(class_name+'.m', 'wt') as fo_m:
fo_m.write(c.dump_implementation())