parseJsonBlocks static method
Parses all valid JSON objects or arrays found in text.
Implementation
static List<Object> parseJsonBlocks(String text) {
final results = <Object>[];
final markdownRegex = RegExp(r'```(?:json)?\s*([\s\S]*?)\s*```');
final Iterable<RegExpMatch> matches = markdownRegex.allMatches(text);
for (final match in matches) {
final String? content = match.group(1);
if (content != null) {
try {
results.add(jsonDecode(content) as Object);
} on FormatException catch (_) {}
}
}
if (results.isNotEmpty) {
return results;
}
final Object? firstBlock = parseFirstJsonBlock(text);
if (firstBlock != null) {
results.add(firstBlock);
}
return results;
}