censorJsonData static method
Censors the given elementsToCensor
in the given body
with the given censorString
.
Implementation
static String censorJsonData(
String body, String censorString, List<CensorElement> elementsToCensor) {
try {
Map<String, dynamic> jsonMap = jsonDecode(body);
Map<String, dynamic> censoredJsonMap =
censorMap(jsonMap, censorString, elementsToCensor);
return jsonEncode(censoredJsonMap);
} catch (e) {
// body is not a JSON dictionary
try {
List<dynamic> jsonList = jsonDecode(body);
List<dynamic> censoredJsonList =
censorList(jsonList, censorString, elementsToCensor);
return jsonEncode(censoredJsonList);
} catch (e) {
// short circuit if body is not a JSON dictionary or JSON list
return body;
}
}
}