encode function

String encode(
  1. Object? value, {
  2. EncodeOptions? options,
})

Encodes a value to TOON format.

value The value to encode (will be normalized to JSON-compatible types) options Optional encoding options Returns a TOON-formatted string

Implementation

String encode(Object? value, {EncodeOptions? options}) {
  final normalized = normalizeValue(value);
  final resolvedOptions = (options ?? const EncodeOptions()).resolve();

  // Apply flattening if enabled
  JsonValue valueToEncode = normalized;
  if (resolvedOptions.enforceFlatMap && isJsonObject(normalized)) {
    valueToEncode = flattenMap(normalized, resolvedOptions.flatMapSeparator);
  }

  return encodeValue(valueToEncode, resolvedOptions);
}