derive_serde_annotation
Dust serde annotations for Dart.
This package builds on top of
derive_annotation and adds the JSON-focused
derive surface used by Dust generation.
It contains:
Serializefor generatedtoJson()Deserializefor generatedfromJson(...)SerDe(...)for Rust-serde-style declaration and field metadataSerDeRenamefor automatic rename rulesSerDeCodec<DartT, JsonT>for custom field codecs
Install Dust
Install the Dust CLI before using these annotations.
macOS / Linux:
curl -fsSL https://raw.githubusercontent.com/y3l1n4ung/dust/main/install.sh | bash
Windows (PowerShell):
irm https://raw.githubusercontent.com/y3l1n4ung/dust/main/install.ps1 | iex
Or with Cargo:
cargo install dust_cli
Example
import 'package:derive_serde_annotation/derive_serde_annotation.dart';
part 'user.g.dart';
@Derive([Serialize(), Deserialize()])
@SerDe(renameAll: SerDeRename.snakeCase)
class User with _$UserDust {
@SerDe(rename: 'user_id')
final String userId;
@SerDe(defaultValue: <String>[])
final List<String> tags;
const User(this.userId, this.tags);
factory User.fromJson(Map<String, Object?> json) => _$UserFromJson(json);
}
Run Dust:
dust build
Dust writes user.g.dart, a generated toJson() mixin member, and the
_$UserFromJson(...) helper used by the forwarding factory.
Custom codec
final class UnixEpochDateTimeCodec implements SerDeCodec<DateTime, int> {
const UnixEpochDateTimeCodec();
@override
int serialize(DateTime value) => value.millisecondsSinceEpoch;
@override
DateTime deserialize(int value) =>
DateTime.fromMillisecondsSinceEpoch(value, isUtc: true);
}
const unixEpochDateTimeCodec = UnixEpochDateTimeCodec();
@Derive([Serialize(), Deserialize()])
class AuditLog {
@SerDe(using: unixEpochDateTimeCodec)
final DateTime createdAt;
const AuditLog(this.createdAt);
}
Design notes
- The package re-exports
derive_annotation, so one import is enough forDerive, core derive traits, and serde traits. SerDecan be used on declarations and fields, like Rust's#[serde(...)].SerDeCodecis the extension point for custom field conversion.SerializeandDeserializestay separate derive traits, which maps cleanly to Dust's existing trait/plugin architecture.- The package only defines annotation metadata. Code generation still lives in Dust Rust crates.
Planned Rust integration
The next Dust layers will use this package in three steps:
- parser captures field annotations
- resolver resolves serde symbols at class and field level
dust_plugin_serdeemitstoJson()andfromJson(...)
Libraries
- derive_serde_annotation
- Dust serde annotations for Dart.