ifJsonAble function

String? ifJsonAble(
  1. String? character
)

ifJsonAble returns the given string if it can be json encoded and decoded. Returns null otherwise.

Implementation

String? ifJsonAble(String? character) {
  List<String> forbiddenChars = [
    "\t",
    "\u0000",
    "\u{0000}",
  ];
  if (forbiddenChars.contains(character)) {
    return (null);
  }
  try {
    String json = jsonEncode(
      character,
    );
    return (jsonDecode(json) == character ? character : null);
  } catch (e) {
    return (null);
  }
}