loadsTolerant function

Object? loadsTolerant(
  1. String text
)

Reads JSON, falling back to the Python-literal dialect weaker models emit.

repr output turns up about as often as JSON from a prompted model: single quotes, trailing commas, True/False/None. Python has ast.literal_eval for exactly this; Dart does not, so the fallback is a string-aware normalising pass followed by an ordinary decode.

It is a rewrite, never an evaluation: the output is fed to jsonDecode like any other text, so there is no execution surface here — nothing in the input can name or call anything.

Returns null when neither reading works. Never throws.

Implementation

Object? loadsTolerant(String text) {
  try {
    return jsonDecode(text);
  } on FormatException {
    // Fall through to the tolerant pass.
  }
  try {
    return jsonDecode(_normalise(text));
  } on FormatException {
    return null;
  }
}