Skip to content

Commit 58780ca

Browse files
committed
Base: add sort_keys option for to_json and to_yaml
1 parent 4ddb6ea commit 58780ca

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

modelspec/BaseTypes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def to_dict_format(cls, var, ordered=True):
297297
d = OrderedDict({var.id: d}) if ordered else {var.id: d}
298298
return d
299299

300-
def to_json(self, indent=" "):
300+
def to_json(self, indent=" ", sort_keys=False):
301301

302302
d = Base.to_dict_format(self)
303303
import pprint
@@ -306,13 +306,13 @@ def to_json(self, indent=" "):
306306
if verbose:
307307
print("Converted to dict:")
308308
pp.pprint(dict(d))
309-
ret = json.dumps(d, indent=len(indent))
309+
ret = json.dumps(d, indent=len(indent), sort_keys=sort_keys)
310310
if verbose:
311311
print("OD to json: [%s]" % ret)
312312

313313
return ret
314314

315-
def to_yaml(self, indent=" "):
315+
def to_yaml(self, indent=" ", sort_keys=False):
316316

317317
import yaml
318318

@@ -327,7 +327,7 @@ def to_yaml(self, indent=" "):
327327
if sys.version_info[0] == 2:
328328
ret = yaml.dump(d, indent=len(indent), default_flow_style=False)
329329
else:
330-
ret = yaml.dump(d, indent=len(indent), sort_keys=False)
330+
ret = yaml.dump(d, indent=len(indent), sort_keys=sort_keys)
331331
if verbose:
332332
print("OD to yaml: [%s]" % ret)
333333

0 commit comments

Comments
 (0)