@@ -179,7 +179,7 @@ def from_json_pickle(cls, value):
179
179
else :
180
180
logger .error ('jsonpickle library needs to be installed in order to run from_json_pickle' ) # pragma: no cover. Json pickle is getting deprecated.
181
181
182
- def to_json (self , default_mapping = None , ** kwargs ):
182
+ def to_json (self , default_mapping : dict | None = None , force_use_builtin_json = False , ** kwargs ):
183
183
"""
184
184
Dump json of the text view.
185
185
**Parameters**
@@ -190,6 +190,11 @@ def to_json(self, default_mapping=None, **kwargs):
190
190
If you have a certain object type that the json serializer can not serialize it, please pass the appropriate type
191
191
conversion through this dictionary.
192
192
193
+ force_use_builtin_json: Boolean, default = False
194
+ When True, we use Python's builtin Json library for serialization,
195
+ even if Orjson is installed.
196
+
197
+
193
198
kwargs: Any other kwargs you pass will be passed on to Python's json.dumps()
194
199
195
200
**Example**
@@ -212,7 +217,12 @@ def to_json(self, default_mapping=None, **kwargs):
212
217
'{"type_changes": {"root": {"old_type": "A", "new_type": "B", "old_value": "obj A", "new_value": "obj B"}}}'
213
218
"""
214
219
dic = self .to_dict (view_override = TEXT_VIEW )
215
- return json_dumps (dic , default_mapping = default_mapping , ** kwargs )
220
+ return json_dumps (
221
+ dic ,
222
+ default_mapping = default_mapping ,
223
+ force_use_builtin_json = force_use_builtin_json ,
224
+ ** kwargs ,
225
+ )
216
226
217
227
def to_dict (self , view_override = None ):
218
228
"""
@@ -637,14 +647,26 @@ def object_hook(self, obj):
637
647
return obj
638
648
639
649
640
- def json_dumps (item , default_mapping = None , ** kwargs ):
650
+ def json_dumps (item , default_mapping = None , force_use_builtin_json : bool = False , ** kwargs ):
641
651
"""
642
652
Dump json with extra details that are not normally json serializable
653
+
654
+ parameters
655
+ ----------
656
+
657
+ force_use_builtin_json: Boolean, default = False
658
+ When True, we use Python's builtin Json library for serialization,
659
+ even if Orjson is installed.
643
660
"""
644
- if orjson :
661
+ if orjson and not force_use_builtin_json :
645
662
indent = kwargs .pop ('indent' , None )
646
663
if indent :
647
664
kwargs ['option' ] = orjson .OPT_INDENT_2
665
+ if 'sort_keys' in kwargs :
666
+ raise TypeError (
667
+ "orjson does not accept the sort_keys parameter. "
668
+ "If you need to pass sort_keys, set force_use_builtin_json=True "
669
+ "to use Python's built-in json library instead of orjson." )
648
670
return orjson .dumps (
649
671
item ,
650
672
default = json_convertor_default (default_mapping = default_mapping ),
0 commit comments