Data Transformers for Serialization
Utilize tRPC's data transformers to serialize and deserialize custom data types like Dates or BigInts.
What are Data Transformers?
When building APIs, data often needs to be sent between different systems. This process is called serialization (converting data to a transportable format like JSON) and deserialization (converting it back).
tRPC's data transformers help handle complex data types that JSON doesn't natively support, ensuring they arrive on the client side just as they left the server.
JSON's Data Type Limits
JSON (JavaScript Object Notation) is great, but it has limitations. It natively supports strings, numbers, booleans, null, objects, and arrays.
- Dates: JSON represents dates as strings, losing their Date object functionality.
- BigInts: Large integer numbers (BigInt) are not a native JSON type.
- Maps, Sets, RegExps: These also don't have direct JSON representations.
Without transformers, these types can break or change unexpectedly when sent via tRPC.
All lessons in this course
- Handling tRPC Errors Gracefully
- Custom Error Types
- Data Transformers for Serialization
- Formatting Errors and Field-Level Validation Feedback