isJson static method

bool isJson(
  1. String content
)

Checks whether a text string contains valid JSON data (Array or Object).

Implementation

static bool isJson(String content) {
  if (content.trim().isEmpty) return false;
  String text = content;
  if (text.startsWith('\uFEFF')) {
    text = text.substring(1);
  }
  text = text.trim();
  if (!((text.startsWith('[') && text.endsWith(']')) || (text.startsWith('{') && text.endsWith('}')))) {
    return false;
  }
  try {
    final dynamic decoded = jsonDecode(text);
    return decoded is List || decoded is Map;
  } catch (_) {
    return false;
  }
}