11# -*- coding: utf-8 -*-
22
33from __future__ import absolute_import
4- from importlib_resources import open_text
54
6- try :
7- import simplejson as json
8- except ImportError :
9- import json
5+ import json
6+
107from six import string_types
118
9+ import cobra .io .schemata
1210from cobra .io .dict import model_to_dict , model_from_dict
1311
14- JSON_SPEC = "1"
1512
13+ JSON_SPEC = "1"
1614
17- def to_json (model , sort = False , ** kwargs ):
15+ JSON_FORMAT = {
16+ True : {
17+ "indent" : 2 ,
18+ "separators" : (", " , ": " ),
19+ "sort_keys" : True ,
20+ "allow_nan" : False
21+ },
22+ False : {
23+ "separators" : ("," , ":" ),
24+ "sort_keys" : False ,
25+ "allow_nan" : False
26+ }
27+ }
28+
29+
30+ def to_json (model , sort = False , pretty = False , ** kwargs ):
1831 """
1932 Return the model as a JSON document.
2033
@@ -27,6 +40,10 @@ def to_json(model, sort=False, **kwargs):
2740 sort : bool, optional
2841 Whether to sort the metabolites, reactions, and genes or maintain the
2942 order defined in the model.
43+ pretty : bool, optional
44+ Whether to format the JSON more compactly (default) or in a more
45+ verbose but easier to read fashion. Can be partially overwritten by the
46+ ``kwargs``.
3047
3148 Returns
3249 -------
@@ -37,10 +54,13 @@ def to_json(model, sort=False, **kwargs):
3754 --------
3855 save_json_model : Write directly to a file.
3956 json.dumps : Base function.
57+
4058 """
4159 obj = model_to_dict (model , sort = sort )
4260 obj [u"version" ] = JSON_SPEC
43- return json .dumps (obj , allow_nan = False , ** kwargs )
61+ options = JSON_FORMAT [pretty ]
62+ options .update (kwargs )
63+ return json .dumps (obj , ** options )
4464
4565
4666def from_json (document ):
@@ -60,6 +80,7 @@ def from_json(document):
6080 See Also
6181 --------
6282 load_json_model : Load directly from a file.
83+
6384 """
6485 return model_from_dict (json .loads (document ))
6586
@@ -89,25 +110,18 @@ def save_json_model(model, filename, sort=False, pretty=False, **kwargs):
89110 --------
90111 to_json : Return a string representation.
91112 json.dump : Base function.
113+
92114 """
93115 obj = model_to_dict (model , sort = sort )
94116 obj [u"version" ] = JSON_SPEC
95-
96- if pretty :
97- dump_opts = {
98- "indent" : 4 , "separators" : ("," , ": " ), "sort_keys" : True ,
99- "allow_nan" : False }
100- else :
101- dump_opts = {
102- "indent" : 0 , "separators" : ("," , ":" ), "sort_keys" : False ,
103- "allow_nan" : False }
104- dump_opts .update (** kwargs )
117+ options = JSON_FORMAT [pretty ]
118+ options .update (** kwargs )
105119
106120 if isinstance (filename , string_types ):
107121 with open (filename , "w" ) as file_handle :
108- json .dump (obj , file_handle , ** dump_opts )
122+ json .dump (obj , file_handle , ** options )
109123 else :
110- json .dump (obj , filename , ** dump_opts )
124+ json .dump (obj , filename , ** options )
111125
112126
113127def load_json_model (filename ):
@@ -128,17 +142,10 @@ def load_json_model(filename):
128142 See Also
129143 --------
130144 from_json : Load from a string.
145+
131146 """
132147 if isinstance (filename , string_types ):
133148 with open (filename , "r" ) as file_handle :
134149 return model_from_dict (json .load (file_handle ))
135150 else :
136151 return model_from_dict (json .load (filename ))
137-
138-
139- def init_json_schema ():
140- """ Import the JSON schema for schema validation. """
141- with open_text ("cobra.io.schemata" , "json_schema.json" ,
142- encoding = "utf-8" ) as file_handle :
143- json_schema = json .load (file_handle )
144- return json_schema
0 commit comments