printNotation function

String printNotation(
  1. Json document, {
  2. required Catalog catalog,
  3. void whole(
    1. String why
    )?,
})

document in the notation. Parse it back with notationOptionsFor. A document of a schema other than kSchemaVersion, or with none, is refused (UnsupportedSchemaException).

whole, when given, is told why a document had to be printed whole as data (a single document {…} line): the measure's breakdown.

Implementation

String printNotation(Json document, {required Catalog catalog, void Function(String why)? whole}) {
  requireSchema(document);
  document = printNormalized(document, catalog); // one spelling of what the API reads in two (print_events.dart)
  document = _componentNormalized(document);
  final options = notationOptionsFor(document);
  final context = PrintContext(document: document, catalog: catalog, options: options);
  String? why;
  for (final plan in _Plan.values) {
    final printer = _Printer(document, context, plan);
    final out = printer.run();
    if (out != null) return out;
    why = printer.why;
  }
  whole?.call(why ?? 'the print does not read back');
  // A document the tree's words cannot carry: the whole of it as data.
  final empty = parseNotation('', catalog: catalog, options: options).document;
  return 'document ${jsonEncode(_sorted(diffPatch(empty, document) ?? const {}))}';
}