Skip to content

Commit e922062

Browse files
committed
Switch to ujson
1 parent 976e89e commit e922062

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

.github/workflows/gh-tests-ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ jobs:
2626
- name: Test with pytest
2727
run: |
2828
pytest --cache-clear --cov=./azure --cov-report=xml --cov-branch tests
29+
- name: Test with pytest and ujson
30+
run: |
31+
python -m pip install ujson
32+
pytest --cache-clear tests
2933
- name: Codecov
3034
if: ${{ matrix.python-version }} == 3.9
3135
uses: codecov/codecov-action@v2

azure/functions/_durable_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _serialize_custom_object(obj):
1212
1313
This function gets called when `json.dumps` cannot serialize
1414
an object and returns a serializable dictionary containing enough
15-
metadata to recontrust the original object.
15+
metadata to reconstruct the original object.
1616
1717
Parameters
1818
----------

azure/functions/_json.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
from enum import Enum
55
import os
66

7+
AZUREFUNCTIONS_UJSON_ENV_VAR = 'AZUREFUNCTIONS_UJSON'
8+
79
try:
8-
import orjson
9-
if 'AZUREFUNCTIONS_ORJSON' in os.environ:
10-
HAS_ORJSON = bool(os.environ['AZUREFUNCTIONS_ORJSON'])
10+
import ujson
11+
if AZUREFUNCTIONS_UJSON_ENV_VAR in os.environ:
12+
HAS_UJSON = bool(os.environ[AZUREFUNCTIONS_UJSON_ENV_VAR])
1113
else:
12-
HAS_ORJSON = True
14+
HAS_UJSON = True
1315
import json
1416
except ImportError:
1517
import json
16-
HAS_ORJSON = False
18+
HAS_UJSON = False
1719

1820

1921
class StringifyEnum(Enum):
@@ -22,6 +24,10 @@ class StringifyEnum(Enum):
2224
def __str__(self):
2325
return str(self.name)
2426

27+
def __json__(self):
28+
"""For ujson encoding."""
29+
return f'"{self.name}"'
30+
2531

2632
class StringifyEnumJsonEncoder(json.JSONEncoder):
2733
def default(self, o):
@@ -33,25 +39,21 @@ def default(self, o):
3339

3440
JSONDecodeError = json.JSONDecodeError
3541

36-
if HAS_ORJSON:
42+
if HAS_UJSON:
3743
def dumps(v, **kwargs):
38-
sort_keys = False
39-
if 'sort_keys' in kwargs:
40-
del kwargs['sort_keys']
41-
sort_keys = True
42-
if kwargs: # Unsupported arguments
43-
return json.dumps(v, sort_keys=sort_keys, **kwargs)
44-
if sort_keys:
45-
r = orjson.dumps(v, option=orjson.OPT_SORT_KEYS)
46-
else:
47-
r = orjson.dumps(v)
48-
return r.decode(encoding='utf-8')
44+
if 'default' in kwargs:
45+
return json.dumps(v, **kwargs)
46+
47+
if 'cls' in kwargs:
48+
del kwargs['cls']
49+
50+
return ujson.dumps(v, **kwargs)
4951

5052
def loads(*args, **kwargs):
5153
if kwargs:
5254
return json.loads(*args, **kwargs)
53-
else: # ORjson takes no kwargs
54-
return orjson.loads(*args)
55+
else: # ujson takes no kwargs
56+
return ujson.loads(*args)
5557

5658
else:
5759
dumps = json.dumps

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'requests==2.*',
1515
'coverage'
1616
],
17-
'orjson': ['orjson>=3.6.8,<4.0']
17+
'ujson': ['ujson>=5.3.0,<6.0']
1818
}
1919

2020
with open("README.md") as readme:
@@ -36,6 +36,7 @@
3636
'Programming Language :: Python :: 3.7',
3737
'Programming Language :: Python :: 3.8',
3838
'Programming Language :: Python :: 3.9',
39+
'Programming Language :: Python :: 3.10',
3940
'Operating System :: Microsoft :: Windows',
4041
'Operating System :: POSIX',
4142
'Operating System :: MacOS :: MacOS X',

0 commit comments

Comments
 (0)