-
Notifications
You must be signed in to change notification settings - Fork 4
/
nbtencoder.py
76 lines (62 loc) · 1.58 KB
/
nbtencoder.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
72
73
74
75
76
class cmd:
def __init__(self, command, obj={}, as_string=False):
self.command = command.strip()
self.obj = obj
self.as_string = as_string
def __setitem__(self, a, b):
self.obj[a] = b
def __getitem__(self, a):
return self.obj[a]
def get(self, a, b=None):
return self.obj.get(a, b)
def __str__(self):
return JSON2Command(self)
def __repr__(self):
return str(self)
class int_b(int):
def __str__(self): return str(int(self)) + "b"
class int_s(int):
def __str__(self): return str(int(self)) + "s"
class int_l(int):
def __str__(self): return str(int(self)) + "l"
class float_f(float):
def __str__(self): return str(float(self)) + "f"
class noquote_str(str): pass
def JSON2Command(json):
command = ""
if isinstance(json, cmd):
command += json.command + " "
command += JSON2Command(json.obj)
if json.as_string:
command = command.replace('\\', '\\\\').replace(r'"', r'\"')
command = '"' + command + '"'
elif isinstance(json, dict):
command += "{"
for tag in json:
command += tag
command += ":"
command += JSON2Command(json[tag])
command += ","
if command[-1] == ",":
command = command[:-1]
command += "}"
elif isinstance(json, list):
command += "["
for tag in json:
command += JSON2Command(tag)
command += ","
else:
if command[-1] == ",":
command = command[:-1]
command += "]"
elif isinstance(json, noquote_str):
command += json
elif isinstance(json, str):
command += '"'
command += json.replace('\\', '\\\\').replace(r'"', r'\"')
command += '"'
elif json is None:
pass
else:
command += str(json)
return command