dumpObject function Dumping Scalars Dumping Sequence Dumping Mapping Dumping Types Dumping YAML Documents

String dumpObject(
  1. Object? object, {
  2. required YamlDumper dumper,
  3. int indent = 0,
  4. bool includeYamlDirective = false,
  5. Iterable<Directive>? directives,
  6. OnProperties? objectProperties,
  7. bool includeGlobalTags = true,
  8. bool includeDocumendEnd = false,
})

Dumps an object with the specified indent. Uses the dumper provided.

If includeYamlDirective is true, a YAML directive will be included. The version directive represents the YAML version this dumper adheres to. In most cases, this matches the YAML version supported by package:rookie_yaml. See parserVersion.

Any global tags extracted while dumping the object are included as directives before the object's YAML content if includeGlobalTags is true. If objectProperties is provided, they will be omitted from the object string.

Any additional directives provided will be dumped. However, any GlobalTags found in the object take precedence over those present in directives.

Implementation

String dumpObject(
  Object? object, {
  required YamlDumper dumper,
  int indent = 0,
  bool includeYamlDirective = false,
  Iterable<Directive>? directives,
  OnProperties? objectProperties,
  bool includeGlobalTags = true,
  bool includeDocumendEnd = false,
}) {
  final dumpable = dumper.dumpable(object);

  var dumpedObject = '';
  var commentsAsBlock = true;
  int? offsetFromMargin;

  // Saves the output from a dumped map/iterable.
  void dumpedCollection(DumpedCollection dumped) {
    commentsAsBlock = !dumped.applyTrailingComments;
    dumpedObject = dumped.node;
  }

  unwrappedDumpable(
    dumpable,
    onIterable: (iterable) => dumpedCollection(
      dumper.iterableDumper.dumpIterableLike(
        iterable,
        expand: identity,
        indent: indent,
        tag: dumpable.tag,
        anchor: dumpable.anchor,
      ),
    ),
    onMap: (map) => dumpedCollection(
      dumper.mapDumper.dumpMapLike(
        map,
        expand: identity,
        indent: indent,
        tag: dumpable.tag,
        anchor: dumpable.anchor,
      ),
    ),
    onScalar: () {
      final (isMultiline: _, :node, :tentativeOffsetFromMargin) = dumper
          .scalarDumper
          .dump(dumpable, indent: indent, style: null);

      commentsAsBlock =
          dumper.scalarDumper.defaultStyle.nodeStyle == NodeStyle.block;
      offsetFromMargin = tentativeOffsetFromMargin;
      dumpedObject = node;
    },
  );

  final buffer = StringBuffer();

  _writeDirectives(
    dumper,
    buffer,
    includeYamlDirective: includeYamlDirective,
    includeGlobalTags: includeGlobalTags,
    docDirectives: directives,
    onProperties: objectProperties,
  );

  dumpedObject = _applyCommentsIfAny(
    dumper.commentDumper,
    dumpedObject,
    comments: dumpable.comments,
    indent: indent,
    forceCommentsAsBlock: commentsAsBlock,
    offsetFromMargin: offsetFromMargin,
  );

  buffer.write(dumpedObject.indented(indent));

  if (includeDocumendEnd) {
    buffer.write(
      '${dumpedObject.endsWith('\n') ? '' : '\n'}'
      '${DocumentMarker.documentEnd.indicator}',
    );
  }

  dumper.onComplete();
  return buffer.toString();
}