json_serializable
6.15.2, released 4 weeks ago by google.dev
Automatically generate code for converting Dart classes to and from JSON.
Flutter FavoriteRock solid
What it does
Provides Dart Build System builders to generate `fromJson` and `toJson` methods for annotated classes. It inspects class annotations and fields to handle serialization boilerplate safely.
AI-written summary of version 6.15.2, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need to serialize and deserialize JSON data into strongly-typed Dart objects.
- You want compile-time safety and better performance compared to manual casting or runtime reflection.
Skip it when
- Your project cannot use code generation or build tools.
- You prefer writing manual parsing logic or using runtime reflection.
Strengths
- Eliminates boilerplate for JSON parsing and serialization
- Supports custom type converters, enums, and JSON Schema generation
Weaknesses
- Requires running a code generator (build_runner) during development
- Generated files add to your source tree and require regeneration on model changes
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- Yes (build_runner)
- Direct dependencies
- 12
Add json_annotation to dependencies, and json_serializable and build_runner to dev_dependencies. Run `dart run build_runner build` to generate code.
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable()
class Person {
final String firstName, lastName;
Person({required this.firstName, required this.lastName});
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}Alternatives
Packages that do the same job and could replace json_serializable.
Works with
Packages commonly used together with json_serializable: ones it builds on, ones built on it, and its own family.
- analyzerjson_serializable depends on itPerforms static analysis of Dart code for tool integration and embedding.
- pub_semverjson_serializable depends on itVersions and version constraints implementing pub's versioning policy.
- dart_stylejson_serializable depends on itProvides an API and CLI to format Dart source code automatically.
- pubspec_parsejson_serializable depends on itParse pubspec.yaml files with a type‑safe API and detailed error reporting.
- asyncjson_serializable depends on itUtility classes and helpers that extend Dart's async library.
- json_annotationjson_serializable depends on itProvides annotations for json_serializable to generate JSON (de)serialization code.
- buildjson_serializable depends on itPackage for authoring build_runner compatible code generators.
- build_configjson_serializable depends on itFormat definition and support for parsing build.yaml configuration.