Skip to content

Commit 43ed684

Browse files
committed
JSON content decoding Field objects
1 parent 08fc31d commit 43ed684

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

datamodel/parsers/json.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ from asyncpg.pgproto import pgproto
99
from dataclasses import _MISSING_TYPE, MISSING
1010
from typing import Any, Union
1111
from decimal import Decimal
12-
from datamodel.exceptions cimport ParserError
12+
from ..exceptions cimport ParserError
13+
from ..fields import Field
1314
import orjson
1415

1516

@@ -45,6 +46,8 @@ cdef class JSONContent:
4546
return None
4647
elif obj is MISSING:
4748
return None
49+
elif isinstance(obj, Field):
50+
return obj.to_dict()
4851
raise TypeError(f"{obj!r} is not JSON serializable")
4952

5053
def encode(self, object obj, **kwargs) -> str:

examples/dict_model.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# from datamodel.libs.mapping import ClassDict
77
from datamodel.libs.mutables import ClassDict
88

9+
10+
runner = pyperf.Runner()
911
class QueryObject(ClassDict):
1012
"""Base Class for all options passed to Parsers.
1113
"""
@@ -39,16 +41,14 @@ def create_models():
3941

4042

4143
print('=== TESTING CREATION OF DICT === ')
42-
time = timeit.timeit(create_dicts, number=10000)
44+
time = timeit.timeit(create_dicts, number=1000)
4345
print(f"Execution time: {time:.6f} seconds")
4446
print('===')
45-
runner = pyperf.Runner()
4647
runner.bench_func('create_objects', create_dicts)
4748

4849

49-
# print('=== TESTING CREATION OF MODEL === ')
50-
# time = timeit.timeit(create_models, number=10000)
51-
# print(f"Execution time: {time:.6f} seconds")
52-
# print('===')
53-
# runner = pyperf.Runner()
54-
# runner.bench_func('create_models', create_models)
50+
print('=== TESTING CREATION OF MODEL === ')
51+
time = timeit.timeit(create_models, number=1000)
52+
print(f"Execution time: {time:.6f} seconds")
53+
print('===')
54+
runner.bench_func('create_models', create_models)

0 commit comments

Comments
 (0)