writeValueAsYaml method

  1. @override
String writeValueAsYaml(
  1. Object? value
)
override

Serializes a Dart object into a YAML string.

Converts the given value into a YAML-encoded string using the current NamingStrategy, annotations, and registered serializers.

Example

final yaml = mapper.writeValueAsYaml(user);
print(yaml);
// user:
//   id: 1
//   name: Alice

Notes

  • Respects YAML-specific naming conventions
  • Handles nested objects, collections, and custom types
  • Supports YAML features like anchors and aliases for deduplication
  • Produces human-readable, indented output by default

Implementation

@override
String writeValueAsYaml(Object? value) {
  if (value == null) return "";

  final generator = getYamlGenerator();

  return synchronized(generator, () {
    getYamlSerializationContext().serialize(value, generator);
    final yaml = generator.toString();
    generator.close();

    return yaml;
  });
}