tryDecode method

Object? tryDecode()

Attempts to decode this string as JSON, returning the original string on failure.

Useful for lenient parsing where the input might or might not be JSON. Empty strings (after trimming) return the original value unchanged.

Returns Map, List, or primitive types on success; returns this on failure.

Implementation

Object? tryDecode() {
  final s = trim();
  if (s.isEmpty) return this;
  try {
    return jsonDecode(s);
  } catch (_) {
    return this;
  }
}