forked from M-Labs/artiq
1
0
Fork 0

pyon: partial JSON compatibility

This commit is contained in:
Sebastien Bourdeauducq 2014-12-03 23:46:59 +08:00
parent fd8f3be946
commit 4c7749bd01
1 changed files with 16 additions and 5 deletions

View File

@ -46,19 +46,20 @@ class _Encoder:
return " "*self.indent_level return " "*self.indent_level
def encode_none(self, x): def encode_none(self, x):
return "None" return "null"
def encode_bool(self, x): def encode_bool(self, x):
if x: if x:
return "True" return "true"
else: else:
return "False" return "false"
def encode_number(self, x): def encode_number(self, x):
return str(x) return str(x)
def encode_str(self, x): def encode_str(self, x):
return repr(x) tt = {ord("\""): "\\\"", ord("\\"): "\\\\", ord("\n"): "\\n"}
return "\"" + x.translate(tt) + "\""
def encode_tuple(self, x): def encode_tuple(self, x):
if len(x) == 1: if len(x) == 1:
@ -83,8 +84,13 @@ class _Encoder:
else: else:
self.indent_level += 1 self.indent_level += 1
r += "\n" r += "\n"
first = True
for k, v in x.items(): for k, v in x.items():
r += self.indent() + self.encode(k) + ": " + self.encode(v) + ",\n" if not first:
r += ",\n"
first = False
r += self.indent() + self.encode(k) + ": " + self.encode(v)
r += "\n" # no ','
self.indent_level -= 1 self.indent_level -= 1
r += self.indent() r += self.indent()
r += "}" r += "}"
@ -121,6 +127,11 @@ def _nparray(shape, dtype, data):
_eval_dict = { _eval_dict = {
"__builtins__": None, "__builtins__": None,
"null": None,
"false": False,
"true": True,
"Fraction": Fraction, "Fraction": Fraction,
"nparray": _nparray "nparray": _nparray
} }