tryEncodeJson static method

List<int>? tryEncodeJson(
  1. Object? json, {
  2. String? indent,
  3. bool toStringEncodable = false,
  4. Object? toEncodable(
    1. dynamic
    )?,
})

Attempts to encode a JSON-serializable json object into a list of bytes, returning null if the conversion to JSON string fails.

Implementation

static List<int>? tryEncodeJson(
  Object? json, {
  String? indent,
  bool toStringEncodable = false,
  Object? Function(dynamic)? toEncodable,
}) {
  if (json == null) return null;
  try {
    return encodeJson(json,
        indent: indent,
        toEncodable: toEncodable,
        toStringEncodable: toStringEncodable);
  } catch (_) {
    return null;
  }
}