-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathjson_rapidjson.py
More file actions
43 lines (30 loc) · 1.19 KB
/
Copy pathjson_rapidjson.py
File metadata and controls
43 lines (30 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
python-rapidjson benchmark wrapper.
Call-path: prepare_data converts to dict (untimed). Timed path is dumps/loads only.
Stream mode is adapted (no native bytes stream API for our usage).
"""
from __future__ import annotations
import io
from typing import Any
import rapidjson
from .base import Serializer
from ..converters import to_dict
class RapidjsonSerializer(Serializer):
native_kind = "dict"
stream_mode = "adapted"
package_name = "python-rapidjson"
@property
def name(self) -> str:
return "rapidjson"
def prepare_data(self, obj: Any, test_data_name: str, test_data_type: type) -> Any:
return to_dict(obj)
def serialize_bytes(self, obj: Any) -> bytes:
# dumps returns str; encode is part of producing wire bytes for this binding
return rapidjson.dumps(obj, ensure_ascii=False).encode("utf-8")
def deserialize_bytes(self, data: bytes) -> Any:
return rapidjson.loads(data)
def serialize_stream(self, obj: Any, stream: io.BytesIO) -> None:
stream.write(self.serialize_bytes(obj))
def deserialize_stream(self, stream: io.BytesIO) -> Any:
stream.seek(0)
return self.deserialize_bytes(stream.read())