0Pricing
Dart Academy · Lesson

json_serializable in Action

Auto-generate model serialization.

json_serializable in Action is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The JSON Boilerplate Problem

Writing fromJson and toJson by hand is tedious and error-prone. The json_serializable package generates both for you from a model. 📦

Add the Packages

You need json_annotation as a normal dependency, plus json_serializable and build_runner as dev dependencies for generation.

dependencies:
  json_annotation: ^4.9.0

Annotate the Class

Mark your model with @JsonSerializable. This annotation tells the generator to produce serialization code for that class.

@JsonSerializable()
class User {
  final String name;
  User(this.name);
}

Declare the Part File

Add a part directive so your file can include the generated helpers. Its name mirrors your file with a .g.dart suffix.

part 'user.g.dart';

Wire fromJson

Add a factory that forwards to the generated function. The _$UserFromJson helper is created for you by the builder.

factory User.fromJson(Map<String, dynamic> j) =>
    _$UserFromJson(j);

Wire toJson

Add a method that calls the generated writer. The _$UserToJson helper turns your object back into a JSON map.

Map<String, dynamic> toJson() => _$UserToJson(this);

Generate the Code

Run build_runner once, and the user.g.dart file appears with both helper functions fully implemented and ready to use.

dart run build_runner build

Rename a JSON Key

When the API field differs from your Dart name, use @JsonKey to map it. Your code stays clean while the JSON matches the server.

@JsonKey(name: 'full_name')
final String fullName;

Provide a Default

You can supply a fallback for missing fields. The defaultValue option fills in a value when the key is absent from the JSON.

@JsonKey(defaultValue: 0)
final int score;

Nested Models Just Work

If a field is another @JsonSerializable class, the generator serializes it recursively. Nested objects need no extra effort from you.

Use It Like Normal Dart

Once generated, you parse and encode with plain method calls. The generated helpers stay hidden, so your call sites read naturally.

final u = User.fromJson(data);
final back = u.toJson();

Quick Check

Which annotation marks a class for JSON code generation?

Recap

Annotate with @JsonSerializable, wire fromJson and toJson, then run build_runner. Use @JsonKey to map field names and defaults. 🎉

Frequently asked questions

Is the “json_serializable in Action” lesson free?

Yes — the full text of “json_serializable in Action” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “json_serializable in Action”?

Auto-generate model serialization. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “json_serializable in Action” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Built-in Annotations: override, deprecated
  2. Writing Custom Annotations
  3. Code Generation With build_runner
  4. json_serializable in Action
← Back to Dart Academy