safeParseJSON function

dynamic safeParseJSON(
  1. String? jsonStr, {
  2. bool shouldLogError = true,
})

Safely parse a JSON string, returning null on failure.

Implementation

dynamic safeParseJSON(String? jsonStr, {bool shouldLogError = true}) {
  if (jsonStr == null || jsonStr.isEmpty) return null;
  try {
    final stripped = _stripBOM(jsonStr);
    return json.decode(stripped);
  } catch (e) {
    if (shouldLogError) {
      // In the Dart port, log to stderr as a placeholder
      stderr.writeln('safeParseJSON error: $e');
    }
    return null;
  }
}