-
Notifications
You must be signed in to change notification settings - Fork 1
/
json2
executable file
·120 lines (101 loc) · 3.22 KB
/
json2
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
#!/usr/bin/env python
# Created by _Vi in 2013; License: MIT or 2-clause BSD.
from __future__ import print_function
import json
import os
import re
import sys
if sys.version_info[0] < 3:
str=unicode
import codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
else:
long=int
f = sys.stdin
if len(sys.argv)>1: f=open(sys.argv[1],"rt")
dom = json.load(f)
#print(dom)
def str2bool(v):
# http://stackoverflow.com/a/715468/266720
return v.lower() in ("yes", "true", "t", "1")
always_output_stubs = str2bool(os.environ.get("JSON2_ALWAYS_STUBS","False"))
always_mark_strings = str2bool(os.environ.get("JSON2_ALWAYS_MARK_STRINGS","False"))
tricky_strings = set([""
,"null"
,"[]"
,"{}"
,"true"
,"false"
,"="
,"NaN"
,"Infinity"
,"-Infinity"
])
def is_number(s):
try:
float(s)
return True
except ValueError:
try:
int(s) # example: "+ 1" on Python 2
return True
except ValueError:
return False
def can_be_misinterpreted(s):
if always_mark_strings: return True
if s.lower().strip() in tricky_strings: return True
if s.strip()[0] == '"': return True
if is_number(s): return True
return False
# ensure the key does not have "/", "=" or "\n" in it
# make sure to run test.sh if you change this fucntion
def mangle_key(s):
if not s: return ""
s = s\
.replace("\\", "\\!")\
.replace("\n", "\\n")\
.replace("\r", "\\r")\
.replace("\t", "\\t")\
.replace("/", "\\|")\
.replace("\"", "\\'")\
.replace("=", "\\_")
if is_number(s): s="\\"+s;
return s
def recursive_outputter(file_, object_, prefix):
if type(object_) == int:
file_.write(prefix+"="+str(object_)+"\n")
elif type(object_) == long:
file_.write(prefix+"="+str(object_)+"\n")
elif type(object_) == float:
file_.write(prefix+"="+str(object_)+"\n")
elif type(object_) == bool:
file_.write(prefix+"="+ ("true" if object_ else "false") + "\n")
elif type(object_) == str:
splits = object_.split("\n")
mark_strings_here = always_mark_strings
if len(splits) > 1:
mark_strings_here = True
for s in object_.split("\n"):
if mark_strings_here or can_be_misinterpreted(s):
s='"'+s
try:
file_.write(prefix+"="+s+"\n")
except:
file_.write(prefix+"=error\n")
elif object_ is None:
file_.write(prefix+"=null\n")
elif type(object_) == list:
if always_output_stubs or len(object_)==0:
file_.write(prefix+"=[]\n")
for i in range(0,len(object_)):
subobject = object_[i]
recursive_outputter(file_, subobject, prefix+"/"+str(i))
elif type(object_) == dict:
if always_output_stubs or len(object_)==0:
file_.write(prefix+"={}\n")
for k,v in object_.items():
#print((prefix,k,mangle_key(str(k))),file=sys.stderr)
recursive_outputter(file_, v, prefix+"/"+mangle_key(str(k)))
else:
raise Exception("Unknown type "+str(type(object_)))
recursive_outputter(sys.stdout, dom, "")