Sub topic 15
Related Tools and When to Choose Them
Assume you know Dart classes, null safety, generics, Map<String, dynamic>, and the basic role of json_serializable. This chapter compares related tools so you can choose a suitable approach instead of adding code generation automatically.
Start with the problem
JSON serialization answers one question: how does a Dart object become JSON, and how does JSON become a Dart object?
Other tools solve nearby problems:
dart:convertreads and writes JSON text.json_serializablegenerates conversion code for Dart classes.freezedgenerates immutable classes and commonly usesjson_serializablefor JSON.built_valuegenerates strongly structured immutable types and serializers.retrofitgenerates HTTP client methods. It complements a serialization tool but does not replace one.
The choice depends on model size, API complexity, team conventions, and how much generated code your project can maintain.
Compare the common choices
| Tool or approach | Choose it when | Main trade-off |
|---|---|---|
| `dart:convert` plus manual parsing | The model is small, local, or temporary | Repetitive code can drift from the API |
| `json_serializable` | You have ordinary API models and want predictable generated code | You must run the generator and commit or manage generated files |
| `freezed` with `json_serializable` | You need immutable models, `copyWith`, unions, or value equality | More annotations and generated files |
| `built_value` | Your team wants strict immutable types and a consistent builder pattern | Steeper syntax and more framework concepts |
| `retrofit` plus a serializer | You want generated REST client methods | It handles requests, not all model design decisions |
Manual parsing is still useful
Manual parsing has almost no setup cost. It is a reasonable choice for a one-off response or a tiny model:
import 'dart:convert';
class HealthStatus {
const HealthStatus({required this.ok});
factory HealthStatus.fromJson(Map<String, dynamic> json) {
return HealthStatus(ok: json['ok'] as bool);
}
final bool ok;
}
void main() {
const responseBody = '{"ok":true}';
final json = jsonDecode(responseBody) as Map<String, dynamic>;
final status = HealthStatus.fromJson(json);
print(status.ok);
}As models grow, manual parsing repeats casts, key names, defaults, and nested conversions. A renamed API field can require edits in several places. Generated code reduces that maintenance work, but it does not validate whether the server's schema is sensible.
Choose `freezed` for richer model behavior
freezed is often paired with json_serializable:
freezedgenerates immutable classes, constructors, equality, andcopyWith.json_serializablegeneratesfromJsonandtoJson.- Union types let one model represent states such as loading, success, and failure.
Choose this combination when models are part of application state, not merely transport objects. For simple request and response classes, json_serializable alone is usually easier to read.
Choose `built_value` for strict conventions
built_value is useful when a large team wants immutable objects, builders, validation conventions, and generated serializers across the whole codebase. It can provide strong structure, but its builder style adds concepts that small applications may not need.
Do not switch tools only because another project uses it. Consistency within your project is usually more valuable than theoretical feature coverage.
Select a tool deliberately
Check the project before adding a dependency
Look for existing annotations, generated files, build_runner scripts, and team conventions. Mixing several serialization styles in the same feature makes debugging harder. Prefer the smallest toolset that handles the model's actual requirements.
Exercise
You have 40 ordinary REST models, but no union types, builders, or generated HTTP client. Which approach is the best starting point, and why?
Show answer
Use jsonserializable with buildrunner. It removes repetitive parsing while keeping the model structure simpler than adding freezed or built_value.