encodeJSON function

String encodeJSON(
  1. Object? json, {
  2. String? indent,
  3. bool withIndent = false,
  4. bool clearNullEntries = false,
  5. Object? toEncodable(
    1. Object? object
    ) = toEncodableJSON,
})

Encodes json to a JSON string.

withIndent If true applies ident. indent By default uses 2 spaces: . clearNullEntries If true will apply removeNullEntries and remove any null entry in tree. toEncodable Function to transform an object to JSON.

Implementation

String encodeJSON(Object? json,
    {String? indent,
    bool withIndent = false,
    bool clearNullEntries = false,
    Object? Function(Object? object) toEncodable = toEncodableJSON}) {
  if (clearNullEntries) {
    removeNullEntries(json);
  }

  if (withIndent && (indent == null || indent.isEmpty)) {
    indent = '  ';
  }

  dart_convert.JsonEncoder encoder;

  if (indent != null && indent.isNotEmpty) {
    encoder = dart_convert.JsonEncoder.withIndent(indent, toEncodable);
  } else {
    encoder = dart_convert.JsonEncoder(toEncodable);
  }

  return encoder.convert(json);
}