Skip to content
pubscout

carp_serializable

3.0.2, released 5 days ago by carp.dk

Polymorphic JSON (de)serialization using json_serializable with type info.

WASM-ready
$ flutter pub add carp_serializable
Source repository

What it does

Extends json_serializable to support polymorphic classes by adding a __type field and a registry of fromJson functions. It re-exports uuid for ID generation.

AI-written summary of version 3.0.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to serialize/deserialize class hierarchies to/from JSON
  • You already use json_serializable and want minimal boilerplate for polymorphism
  • You need unique IDs in your models

Skip it when

  • You only need simple flat JSON models
  • You prefer runtime reflection over code generation
  • You are targeting non-Flutter platforms

Strengths

  • Adds polymorphic support with minimal extra code
  • Re-exports uuid for convenient ID generation
  • Integrates with existing json_serializable workflow
  • Provides graceful fallback for unknown types

Weaknesses

  • Requires build_runner and code generation
  • Adds extra registration step at startup
  • Limited to json_serializable ecosystem
  • No support for other serialization formats

Setup

Kind
Pure Dart
Effort
Medium
Code generation
Yes (build_runner)
Direct dependencies
2

Add carp_serializable and json_annotation to dependencies, build_runner and json_serializable to dev_dependencies, run build_runner to generate code, register classes in FromJsonFactory before use.

```dart
@JsonSerializable()
class A extends Serializable {
  int index;
  A([this.index = 0]) : super();
  @override
  Function get fromJsonFunction => _$AFromJson;
  factory A.fromJson(Map<String, dynamic> json) => FromJsonFactory().fromJson<A>(json);
  @override
  Map<String, dynamic> toJson() => _$AToJson(this);
}
```

Alternatives

Packages that do the same job and could replace carp_serializable.

  • built_value

    Provides code generation for immutable value types and polymorphic serialization via @BuiltValueSerializerPlugin

  • freezed_annotation

    Offers code generation for immutable classes with union types and JSON support

Works with

Packages commonly used together with carp_serializable: ones it builds on, ones built on it, and its own family.