Skip to content
pubscout

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
$ flutter pub add json_serializable
Source repository

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.

  • freezed

    If you want immutable data classes, union types, and built-in copyWith methods alongside JSON serialization.

  • pigeon

    If you are generating strongly-typed platform channel messaging code between Flutter and host platforms rather than standard JSON objects.

Works with

Packages commonly used together with json_serializable: ones it builds on, ones built on it, and its own family.