-
Notifications
You must be signed in to change notification settings - Fork 33
/
kast-json.py
executable file
·55 lines (49 loc) · 1.55 KB
/
kast-json.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
#!/usr/bin/env python
import json
import sys
from collections import OrderedDict
filename = sys.argv[1]
with open(filename) as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
def escape(data):
return data.encode('unicode_escape')
def print_kast(data):
if isinstance(data, list):
sys.stdout.write('`[_]_IELE-DATA`(')
for elem in data:
sys.stdout.write('`_,__IELE-DATA`(')
print_kast(elem)
sys.stdout.write(',')
sys.stdout.write('`.List{"_,__IELE-DATA"}`(.KList)')
for elem in data:
sys.stdout.write(')')
sys.stdout.write(')')
elif isinstance(data, OrderedDict):
sys.stdout.write('`{_}_IELE-DATA`(')
for key, value in data.items():
sys.stdout.write('`_,__IELE-DATA`(`_:__IELE-DATA`(')
print_kast(key)
sys.stdout.write(',')
print_kast(value)
sys.stdout.write('),')
sys.stdout.write('`.List{"_,__IELE-DATA"}`(.KList)')
for key in data:
sys.stdout.write(')')
sys.stdout.write(')')
elif isinstance(data, str) or isinstance(data, unicode):
sys.stdout.write('#token('),
sys.stdout.write(json.dumps(json.dumps(data)))
sys.stdout.write(',"String")')
elif isinstance(data, bool):
sys.stdout.write('#token("'),
sys.stdout.write("true" if data else "false")
sys.stdout.write('","Bool")')
elif isinstance(data, long) or isinstance(data, int):
sys.stdout.write('#token("'),
sys.stdout.write(str(data))
sys.stdout.write('","Int")')
else:
sys.stdout.write(type(data))
raise AssertionError
print_kast(data)
print