Format Code Now
/
ConvertYAML to Dart Next: YAML to Go ›

YAML to Dart Class Generator

CV-88

Paste a YAML document and press Convert to get classes with fromJson and toJson. The YAML is read into JSON first, so the types come from its values, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

YAML

Dart

How to use it

  1. Paste one YAML document, or open a .yaml or .yml file. Use a sample that shows every section you expect.
  2. Choose fromJson and toJson written out, or json_serializable annotations, and keep or drop the final fields.
  3. Press Convert, then copy the result or download it as a .dart file.
  4. Decode with jsonDecode and call Root.fromJson(map), then call toJson() to write it back. The names follow the JSON form of your YAML, so add the tags your YAML library needs if you load YAML directly.

How the classes are built

Each object becomes a class with fields in camelCase, a constructor with named parameters, a fromJson factory and a toJson method. Mixed types, empty arrays and keys that are always null become dynamic, and Dart words such as class or default get a trailing underscore. The json_serializable choice writes @JsonSerializable and @JsonKey annotations and a part directive; you run build_runner to create the .g.dart file. Types are int, double, String, bool and List<T>; a number with a decimal point becomes double, and fromJson reads it through num so a whole-number value still loads.

How the YAML becomes classes

true/false, null, integers and floats get their usual types, while dates stay strings. Anchors, aliases and << merge keys are resolved, so the values they point to appear in the generated types. Comments are dropped, only one document is read, and keys that are not text become text. A value written as 1.0 is read as the number 1, and an integer beyond 9007199254740991 is rounded by the YAML reader. Keys that are not valid Dart names, such as first-name, are renamed, and the original key is kept in the fromJson and toJson code (or the @JsonKey annotation).

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay String; parse them with DateTime.parse in the fromJson code if you need DateTime values. A YAML file with optional sections should be pasted with every section present, or the missing sections will not appear in the output.

Questions people ask

How do I convert YAML to a Dart class?

Paste the YAML into the input box, choose your options and press Convert to get the classes. Decode with jsonDecode and call Root.fromJson(map), then call toJson() to write it back. The classes follow the JSON form of the YAML. The conversion happens in your browser.

Why is a Dart field typed dynamic?

Mixed types, an empty array and a key that is always null have no single type in the sample, so the field is dynamic. Replace it with the type you expect, or paste a sample where the key holds a value. This applies to the JSON form of your YAML.

Are the fields final in the Dart output?

By default yes, with a const constructor, which suits immutable models. Turn the final option off to get plain mutable fields and a normal constructor. This applies to the JSON form of your YAML.

Should I use fromJson and toJson or json_serializable for Dart?

The default writes fromJson and toJson by hand, so the file works with no extra packages. Choose json_serializable if your project already uses build_runner; the output then has annotations and a part directive, and you run the build to create the .g.dart file. This applies to the JSON form of your YAML.

Why did a YAML value like 1.0 come out as an integer type?

The YAML is converted to JSON before types are chosen, and 1.0 is the same number as 1 there. A field that always holds whole-looking decimals in your sample can come out as an integer type. Add a non-whole decimal to the sample, or change the type by hand. The Dart classes are written from that converted data.

Can I paste a YAML file with several documents?

No. Only a single document is read; a file with --- separators between documents gives an error. Paste one document at a time, or merge them into a list under one top-level key yourself. The Dart classes are written from that converted data.

About this tool

The YAML is read with the core schema (true/false, null, integers and floats), converted to JSON, and the code is generated from that. Dates stay text, comments are dropped, anchors, aliases and << merge keys are resolved, keys that are not text become text, and only one document is read. A value such as 1.0 is read as the number 1, so a field that holds whole-looking decimals can come out as an integer type; an integer beyond 9007199254740991 is rounded by the YAML reader before any code is written; .nan and .inf become null. Every object becomes a class with fields in camelCase, a constructor with named parameters, a fromJson factory and a toJson method, working on Map<String, dynamic>. Types are int, double, String, bool and List<T>; a number written with a decimal point, or beyond int64, becomes double (read through num, so 20 still loads into a double field), and a comment marks integers beyond int64. A key that is null or missing in some items gets a nullable type (T?) and is left out of toJson when it is missing; mixed types, always-null keys and empty arrays become dynamic. Dart words such as class or default get a trailing underscore. The json_serializable choice writes the annotations and a part directive instead, and you still run build_runner to create the .g.dart file. It does not detect dates. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.