pythonbeginner
Fast JSON Serialisation with orjson
Use orjson for 5-10x faster JSON serialisation of large Python dicts, dataclasses, and NumPy arrays.
pythonPress ⌘/Ctrl + Shift + C to copy
import orjson
import numpy as np
from datetime import datetime
from dataclasses import dataclass
@dataclass
class Record:
id: int
ts: datetime
values: list[float]
records = [Record(i, datetime.utcnow(), list(np.random.rand(5))) for i in range(10)]
# orjson serialises dataclasses, datetime, numpy natively
data = orjson.dumps(records, option=orjson.OPT_SERIALIZE_DATACLASS)
print(data[:200])
parsed = orjson.loads(data)
print(len(parsed), 'records')Use Cases
- high-throughput serialisation
- API responses
- event logging
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonbeginner
DataFrame to Dict Records
Convert DataFrames to lists of dicts for API responses, JSON export, or further processing.
Best for: API serialization
#pandas#records
typescriptadvanced
JSON Stream Parser
Parses large JSON arrays from a readable stream without loading the entire file into memory.
Best for: Processing large log files
#streaming#json
javaintermediate
Jackson — JSON Serialization and Parsing
Parse and generate JSON with Jackson: ObjectMapper, annotations, custom serializers, and streaming.
Best for: REST API request and response serialization
#java#json
javaintermediate
JSON Serialization Without Libraries
Serialize and deserialize Java objects to JSON manually with a lightweight builder-based approach.
Best for: Lightweight JSON generation without Jackson/Gson
#java#json