1
1
# -*- coding: utf-8 -*-
2
2
3
3
from __future__ import absolute_import
4
- from importlib_resources import open_text
5
4
6
- try :
7
- import simplejson as json
8
- except ImportError :
9
- import json
5
+ import json
6
+
10
7
from six import string_types
11
8
9
+ import cobra .io .schemata
12
10
from cobra .io .dict import model_to_dict , model_from_dict
13
11
14
- JSON_SPEC = "1"
15
12
13
+ JSON_SPEC = "1"
16
14
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 ):
18
31
"""
19
32
Return the model as a JSON document.
20
33
@@ -27,6 +40,10 @@ def to_json(model, sort=False, **kwargs):
27
40
sort : bool, optional
28
41
Whether to sort the metabolites, reactions, and genes or maintain the
29
42
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``.
30
47
31
48
Returns
32
49
-------
@@ -37,10 +54,13 @@ def to_json(model, sort=False, **kwargs):
37
54
--------
38
55
save_json_model : Write directly to a file.
39
56
json.dumps : Base function.
57
+
40
58
"""
41
59
obj = model_to_dict (model , sort = sort )
42
60
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 )
44
64
45
65
46
66
def from_json (document ):
@@ -60,6 +80,7 @@ def from_json(document):
60
80
See Also
61
81
--------
62
82
load_json_model : Load directly from a file.
83
+
63
84
"""
64
85
return model_from_dict (json .loads (document ))
65
86
@@ -89,25 +110,18 @@ def save_json_model(model, filename, sort=False, pretty=False, **kwargs):
89
110
--------
90
111
to_json : Return a string representation.
91
112
json.dump : Base function.
113
+
92
114
"""
93
115
obj = model_to_dict (model , sort = sort )
94
116
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 )
105
119
106
120
if isinstance (filename , string_types ):
107
121
with open (filename , "w" ) as file_handle :
108
- json .dump (obj , file_handle , ** dump_opts )
122
+ json .dump (obj , file_handle , ** options )
109
123
else :
110
- json .dump (obj , filename , ** dump_opts )
124
+ json .dump (obj , filename , ** options )
111
125
112
126
113
127
def load_json_model (filename ):
@@ -128,17 +142,10 @@ def load_json_model(filename):
128
142
See Also
129
143
--------
130
144
from_json : Load from a string.
145
+
131
146
"""
132
147
if isinstance (filename , string_types ):
133
148
with open (filename , "r" ) as file_handle :
134
149
return model_from_dict (json .load (file_handle ))
135
150
else :
136
151
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