You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1- I made a shared package
2- create a generic pagination class
import 'package:json_annotation/json_annotation.dart';
import 'package:serverpod/serverpod.dart';
part 'pagination.g.dart';
@JsonSerializable(genericArgumentFactories: true)
class Pagination<T> {
final int page;
final int perPage;
final int total;
final List<T> items;
const Pagination({
required this.page,
required this.perPage,
required this.total,
required this.items,
});
factory Pagination.fromJson(
Map<String, dynamic> json,
SerializationManager serializationManager,
) =>
_$PaginationFromJson<T>(json, serializationManager.deserialize<T>);
}
Map<String, dynamic> toJson(Object? Function(T value) toJsonT) =>
_$PaginationToJson<T>(this, toJsonT);
3- I read some of the source code and I found this serializationManager.deserialize<T> that find the type to serialize the type
4- I got this error Converting object to an encodable object failed: Instance of 'Pagination<Printer>'
is there any other way to serialize a generic class in Serverpod?
Hi everyone, 👋
I’m using Serverpod 2.3.1, and I’d like to implement a standardized API response format, such as: { "code": 20000, "message": "success", "data": { } }
or { "code": 20000, "message": "success", "data": { "page": 1, "total": 2, "items": [{}] } }
For example:
However, I found that Serverpod 2.x does not support automatic serialization for generic types (T), making it impossible to return CommonResult directly.
I’d love to hear your thoughts:
1️⃣ Is there an official recommendation for handling generic API responses?
2️⃣ How are you implementing a unified API response format in your Serverpod projects?
3️⃣ Are there any workarounds?
Looking forward to your insights—thanks in advance! 🙏
Activity
Isakdl commentedon Jul 4, 2023
There is no support for generic types yet unfortunately, but it would definitely be useful.
dhruvinnv commentedon Jul 7, 2023
masreplay commentedon Jul 7, 2023
@DK070202 is this working example?
dhruvinnv commentedon Jul 8, 2023
I haven't tested it with Serverpod. It's a possible path, u can evaluate it!
coderxsle commentedon Feb 1, 2025
Hi everyone, 👋
I’m using Serverpod 2.3.1, and I’d like to implement a standardized API response format, such as:
{ "code": 20000, "message": "success", "data": { } }or
{ "code": 20000, "message": "success", "data": { "page": 1, "total": 2, "items": [{}] } }For example:
However, I found that Serverpod 2.x does not support automatic serialization for generic types (T), making it impossible to return CommonResult directly.
I’d love to hear your thoughts:
1️⃣ Is there an official recommendation for handling generic API responses?
2️⃣ How are you implementing a unified API response format in your Serverpod projects?
3️⃣ Are there any workarounds?
Looking forward to your insights—thanks in advance! 🙏