static function write_json_file($filename, $json_array, array $whitelist = null, $json_options = null)
	{
		$stop_snippet = "<?php printf('_%c%c}%c',34,10,10);__halt_compiler();?>";

		if ($whitelist === null) {
			// Put stop snippet on begin.
			$result = array_merge(array('_' => null), $json_array);
		} else {
			// Whitelisted keys first (if they exist in $json_array), then stop snippet, then rest.
			$header = array_intersect_key(array_flip($whitelist), $json_array);
			$header['_'] = null;
			$result = array_merge($header, $json_array);
			// Replace '<?' with '<_?' in all whitelisted values, so injected PHP will not execute.
			foreach ($whitelist as $k) {
				if (array_key_exists($k, $result) && is_string($result[$k])) {
					$result[$k] = str_replace('<?', '<_?', $result[$k]);
				}
			}
		}

		// Put stop snipped at marked position (it is here to prevent 
		// overwriting from $json_array).
		$result['_'] = $stop_snippet;

		$json_str = json_encode($result, $json_options === null
				? (defined('JSON_PRETTY_PRINT')
					? JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
					: JSON_NUMERIC_CHECK)
				: $json_options & ~(JSON_HEX_TAG | JSON_HEX_APOS));

		if ($filename === null) {
			return $json_str;
		} else {
			return file_put_contents($filename, $json_str);
		}
	}