3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class handles functions for JSON format
21 class CRM_Utils_JSON
{
24 * Output json to the client.
27 public static function output($input) {
28 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
29 echo json_encode($input);
30 CRM_Utils_System
::civiExit();
34 * Test whether the input string is valid JSON.
38 public static function isValidJSON($str) {
40 return json_last_error() == JSON_ERROR_NONE
;
44 * Do not use this function. See CRM-16353.
47 * @param array $params
48 * Associated array of row elements.
50 * Datatable needs this to make it more secure.
53 * @param int $iFilteredTotal
54 * Total records on a page.
55 * @param array $selectorElements
59 public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) {
61 $sOutput .= '"sEcho": ' . intval($sEcho) . ', ';
62 $sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
63 $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
64 $sOutput .= '"aaData": [ ';
65 foreach ((array) $params as $key => $value) {
68 foreach ($selectorElements as $element) {
72 // CRM-7130 --lets addslashes to only double quotes,
73 // since we are using it to quote the field value.
74 // str_replace helps to provide a break for new-line
75 $sOutput .= '"' . addcslashes(str_replace(["\r\n", "\n", "\r"], '<br />', $value[$element]), '"\\') . '"';
77 // remove extra spaces and tab character that breaks dataTable CRM-12551
78 $sOutput = preg_replace("/\s+/", " ", $sOutput);
83 $sOutput = substr_replace($sOutput, "", -1);