Skip to content

Generic of type <T>  #1035

Open
Open

Description

@masreplay

As mentioned herein documentation https://docs.serverpod.dev/concepts/serialization

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?

Activity

  1. Isakdl commented on Jul 4, 2023

    @Isakdl
    Collaborator

    There is no support for generic types yet unfortunately, but it would definitely be useful.

  2. added
    enhancement
    on Jul 4, 2023
  3. dhruvinnv commented on Jul 7, 2023

    @dhruvinnv
    • Check this package built_value, it's a bit boilerplate but it supports generic types.
    • Using it with custom serialization may help you here.
    • Additionally, you can use a global serializer inside your class when using toJson and fromJson methods to do parsing as:
      final value = standardSerializers.deserializeWith(
        FreezedCustomClass.serializer, serializedAccount)!;
      print(value);
    • Orignal reference is here
    • So your entity may look like this:
    • Additionally, you can use a global serializer inside your class when using toJson and fromJson methods to do parsing as:
      factory FreezedCustomClass.fromJson(
        Map<String, Object?> json,
        SerializationManager serializationManager,
      ) {
        final value = standardSerializers.deserializeWith(
        FreezedCustomClass.serializer, serializedAccount)!;
        return value;
      }
    • Any catches in this @Isakdl?
  4. masreplay commented on Jul 7, 2023

    @masreplay
    Author

    @DK070202 is this working example?

  5. dhruvinnv commented on Jul 8, 2023

    @dhruvinnv

    I haven't tested it with Serverpod. It's a possible path, u can evaluate it!

  6. added
    area: models
    on Dec 18, 2024
  7. coderxsle commented on Feb 1, 2025

    @coderxsle

    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! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: modelsenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions