charcoal logo

charcoal

CI status Pub version Maintained with Melos

A highly opinionated Dart build_runner with a unified annotation. Generates JSON, copy, equality, hash and toString as direct code for any annotated data class, with the whole type graph resolved at build time: no registry, no runtime lookup, nor dynamic dispatch. Configurations (and errors) are checked at build time.

The main goal of this project is to be a blazingly fast, static alternative to dart_mappable, which is itself a sane alternative to Google's json_serializable.

Install

dart pub add charcoal dev:charcoal_builder dev:build_runner

Usage

import 'package:charcoal/charcoal.dart';

part 'book.charcoal.g.dart';

@immutable
@Charcoal()
final class Book({
  required this.id,
  required this.title,
  required this.genres,
  this.notes,
  this.releasedAt,
}) with BookCharcoal {
  final Id<Book> id;
  final String title;
  final List<Genre> genres;
  final String? notes;
  final DateTime? releasedAt;
}
final book = BookCharcoal.fromJsonString(body);
final revised = book.copyWith(notes: null);
final json = revised.toJsonString();

Generate the code with dart run build_runner build, which you can add to your build script.

Benchmarks

The goal of Charcoal is to bring the ergonomics of dart_mappable but without the dynamic dispatching. The comparison with the other alternatives is not just speed then, but feature set. That being said, you can see how it compares to all three (json_serializable, dart_mappable, and freezed) using our benchmarks.

Contributing

See our CONTRIBUTING guide.

Libraries

charcoal
The annotation and runtime for classes generated by charcoal_builder.