function phutil_validate_json()

in src/utils/utils.php [1354:1417]


function phutil_validate_json($value, $path = '') {
  if ($value === null) {
    return;
  }

  if ($value === true) {
    return;
  }

  if ($value === false) {
    return;
  }

  if (is_int($value)) {
    return;
  }

  if (is_float($value)) {
    return;
  }

  if (is_array($value)) {
    foreach ($value as $key => $subvalue) {
      if (strlen($path)) {
        $full_key = $path.' > ';
      } else {
        $full_key = '';
      }

      if (!phutil_is_utf8($key)) {
        $full_key = $full_key.phutil_utf8ize($key);
        return pht(
          'Dictionary key "%s" is not valid UTF8, and cannot be JSON encoded.',
          $full_key);
      }

      $full_key .= $key;
      $result = phutil_validate_json($subvalue, $full_key);
      if ($result !== null) {
        return $result;
      }
    }
  }

  if (is_string($value)) {
    if (!phutil_is_utf8($value)) {
      $display = substr($value, 0, 256);
      $display = phutil_utf8ize($display);
      if (!strlen($path)) {
        return pht(
          'String value is not valid UTF8, and can not be JSON encoded: %s',
          $display);
      } else {
        return pht(
          'Dictionary value at key "%s" is not valid UTF8, and cannot be '.
          'JSON encoded: %s',
          $path,
          $display);
      }
    }
  }

  return;
}