File tree Expand file tree Collapse file tree 4 files changed +19
-12
lines changed Expand file tree Collapse file tree 4 files changed +19
-12
lines changed Original file line number Diff line number Diff line change 1
- import json
2
-
3
1
from ..template import TemplateFile
4
- from ..util import conv_bool
2
+ from ..util import conv_bool , dump_json
5
3
from .base import formater , FmtBase
6
4
7
5
_TPL_PAC = TemplateFile ('res/tpl-pac.js' , True )
@@ -42,7 +40,7 @@ def pre_generate(self):
42
40
return super ().pre_generate ()
43
41
44
42
def generate (self , replacements ):
45
- rules = json . dumps (
43
+ rules = dump_json (
46
44
self .precise_rules if self .options .precise else self .rules ,
47
45
indent = None if self .options .compress else 4 ,
48
46
separators = (',' , ':' ) if self .options .compress else None )
Original file line number Diff line number Diff line change 1
- import json
2
-
3
- from ..util import conv_lower , Namespace
1
+ from ..util import conv_lower , Namespace , dump_json
4
2
from .base import formater
5
3
from .ip import IPInterface , _IP_FAMILIES , _CC_DEF
6
4
@@ -30,4 +28,4 @@ def generate(self, replacements):
30
28
data .rules .append ({
31
29
'ip_cidr' : [str (d ) for d in self .iter_ip_cidr (self .options .ip_family , self .options .ip_cc )]
32
30
})
33
- return json . dumps ( dict ( data ), indent = 2 )
31
+ return dump_json ( data )
Original file line number Diff line number Diff line change 1
- import json
2
-
3
1
from ..util import dump_yaml
4
- from ..util import conv_lower
2
+ from ..util import conv_lower , dump_json
5
3
from .base import formater , FmtBase , TPL_LEAD_COMMENT
6
4
7
- V2RAY_DUMPER = {'json' : lambda d : json . dumps ( d , indent = 4 ),
5
+ V2RAY_DUMPER = {'json' : lambda d : dump_json ( d ),
8
6
'yaml' : lambda d : f'{ TPL_LEAD_COMMENT } \n ' + dump_yaml (d )}
9
7
_DEF_FORMAT = list (V2RAY_DUMPER .keys ())[0 ]
10
8
_DEF_PROXY_TAG = 'proxy'
Original file line number Diff line number Diff line change @@ -59,6 +59,19 @@ def dump_yaml(data, **kwargs):
59
59
return yaml .dump (data , ** kwargs )
60
60
61
61
62
+ class JSONEncoder (json .JSONEncoder ):
63
+ def default (self , o ):
64
+ if isinstance (o , Namespace ):
65
+ return dict (o )
66
+ return super ().default (o )
67
+
68
+
69
+ def dump_json (data , ** kwargs ):
70
+ kwargs .setdefault ('indent' , 2 )
71
+ kwargs .setdefault ('cls' , JSONEncoder )
72
+ return json .dumps (data , ** kwargs )
73
+
74
+
62
75
class Error (Exception ):
63
76
def __init__ (self , msg ):
64
77
self .msg = msg
You can’t perform that action at this time.
0 commit comments