Skip to content

Commit 03d55e9

Browse files
committed
TypedDict
1 parent 9628b95 commit 03d55e9

6 files changed

Lines changed: 39 additions & 7 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ keywords = ["fast", "json", "datetime", "rfc", "3339"]
1212
include = [
1313
"Cargo.toml",
1414
"CHANGELOG.md",
15+
"conftest.py",
1516
"data/*",
1617
"LICENSE-APACHE",
1718
"LICENSE-MIT",

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def dumps(__obj: Any, default: Optional[Callable[[Any], Any]] = ..., option: Opt
5757
`dumps()` serializes Python objects to JSON.
5858

5959
It natively serializes
60-
`str`, `dict`, `list`, `tuple`, `int`, `float`, `bool`, `datetime.datetime`,
60+
`str`, `dict`, `list`, `tuple`, `int`, `float`, `bool`,
61+
`typing.TypedDict`, `datetime.datetime`,
6162
`datetime.date`, `datetime.time`, and `None` instances. It supports
6263
arbitrary types through `default`. It does not serialize subclasses of
6364
supported types natively, but `default` may be used.

conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
import sys
3+
4+
is_python35 = sys.version_info.minor == 5
5+
6+
7+
def pytest_ignore_collect(path, config):
8+
if is_python35 and str(path).endswith("test_typeddict.py"):
9+
return True

test/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pendulum;sys_platform!="win"
33
psutil
44
pytest
55
pytz
6+
typing_extensions;python_version>"3.5" and python_version <"3.8"

test/test_datetime.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,11 @@ def test_time_microsecond_max(self):
490490
datetime.time microsecond max
491491
"""
492492
self.assertEqual(
493-
orjson.dumps(datetime.time(0, 0, 0, 999999)),
494-
b'"00:00:00.999999"',
493+
orjson.dumps(datetime.time(0, 0, 0, 999999)), b'"00:00:00.999999"'
495494
)
496495

497496
def test_time_microsecond_min(self):
498497
"""
499498
datetime.time microsecond min
500499
"""
501-
self.assertEqual(
502-
orjson.dumps(datetime.time(0, 0, 0, 1)),
503-
b'"00:00:00.000001"',
504-
)
500+
self.assertEqual(orjson.dumps(datetime.time(0, 0, 0, 1)), b'"00:00:00.000001"')

test/test_typeddict.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
2+
3+
import unittest
4+
5+
try:
6+
from typing import TypedDict
7+
except ImportError:
8+
from typing_extensions import TypedDict
9+
10+
import orjson
11+
12+
13+
class TypedDictTests(unittest.TestCase):
14+
def test_typeddict(self):
15+
"""
16+
dumps() TypedDict
17+
"""
18+
19+
class TypedDict1(TypedDict):
20+
a: str
21+
b: int
22+
23+
obj = TypedDict1(a="a", b=1)
24+
self.assertEqual(orjson.dumps(obj), b'{"a":"a","b":1}')

0 commit comments

Comments
 (0)