dart_json_mapper
2.2.20, released a year ago by edensapple.com
Annotate Dart objects to serialize and deserialize them from and to JSON.
What it does
Allows developers to serialize and deserialize Dart objects to and from JSON using annotations. It generates a single mapping file for the entire project via code generation and avoids `dart:mirrors` for cross-platform compatibility.
AI-written summary of version 2.2.20, based on the package's README and metadata. Report an inaccuracy
Use it when
- You want clean model classes free of manual JSON serialization logic.
- You need advanced JSON mapping features like inheritance, polymorphism, and object cloning.
- You prefer a single centralized mapping file over per-class generated files.
Skip it when
- You prefer the standard Google-backed `json_serializable` approach with per-class generated files.
- You want to avoid setting up `build_runner` code generation.
Strengths
- Keeps model classes clean with no boilerplate serialization methods inside them.
- Rich out-of-the-box features like polymorphism, object cloning, and case style conversion.
- Generates a single centralized mapper file for the whole project.
Weaknesses
- Requires running a code generator (`build_runner`) whenever model classes change.
- Less common choice in the Dart ecosystem compared to `json_serializable`.
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 9
Requires adding `build_runner` to dev_dependencies and configuring `build.yaml` to run code generation.
import 'package:dart_json_mapper/dart_json_mapper.dart' show JsonMapper, jsonSerializable;
import 'main.mapper.g.dart' show initializeJsonMapper;
@jsonSerializable
class MyData {
int a = 123;
MyData(this.a);
}
void main() {
initializeJsonMapper();
print(JsonMapper.serialize(MyData(456)));
}Alternatives
Packages that do the same job and could replace dart_json_mapper.
- json_annotation
It is the standard, widely adopted solution from the Dart team which generates per-class `*.g.dart` files.
84Compare - built_value
If you want immutable value types with built-in serialization and runtime validation.
88Compare - freezed_annotation
If you want union types, immutable classes, and pattern matching support along with JSON serialization.
78Compare
Works with
Packages commonly used together with dart_json_mapper: ones it builds on, ones built on it, and its own family.
- analyzerdart_json_mapper depends on itPerforms static analysis of Dart code for tool integration and embedding.
- pubspec_parsedart_json_mapper depends on itParse pubspec.yaml files with a type‑safe API and detailed error reporting.
- intldart_json_mapper depends on itA Dart library for internationalization and localization, providing message translation, plural/gender handling, and date/number formatting.
- builddart_json_mapper depends on itPackage for authoring build_runner compatible code generators.
- build_configdart_json_mapper depends on itFormat definition and support for parsing build.yaml configuration.
- reflectabledart_json_mapper depends on itProvides runtime reflection via code generation with configurable capabilities.
- build_runnerUsed togetherA build system for Dart code generation and modular compilation.