jsonContains function

bool jsonContains({
  1. required Map<String, dynamic> json,
  2. required Map<String, dynamic> jsonToContain,
  3. JsonContainsListBehavior listBehavior = const JsonContainsListBehavior(),
  4. bool debugLog = false,
})

Used for comparing if a JSON object contains another JSON object. Specifically, if json contains all the fields and values that are in jsonToContain.

E.g. if json is:

{
  "name": "John",
  "age": 30,
  "address": {
    "street": "Main Street",
    "city": "New York"
   }
}

and jsonToContain is:

{
  "name": "John",
  "address": {
    "city": "New York"
  }
}

Then json contains jsonToContain as all the fields and values in jsonToContain are in json.

listBehavior defines the behavior when comparing if a JSON list contains another JSON list.

By default when two lists are compared:

  • lenght of the lists is not compared
  • ordering of items is not compared

Implementation

bool jsonContains({
  required Map<String, dynamic> json,
  required Map<String, dynamic> jsonToContain,
  JsonContainsListBehavior listBehavior = const JsonContainsListBehavior(),
  bool debugLog = false,
}) {
  try {
    _throwIfNotAllWantedReceived(json, jsonToContain, listBehavior);
    return true;
  } catch (e) {
    if (debugLog) {
      log('json_contains: $e');
    }
    return false;
  }
}