JSON option to force unicode

Notice: This thread is very old.
Pavel Kravčík
Member | 1180
+
0
-

I'm using PHP version > 5.5, but I need Unicode output. Native json_encode($data) returns Unicode output until I add JSON_UNESCAPED_UNICODE. But Nette doesn't let me choice and force non-Unicode output.

Yeah, I can simply workaround here by extending this class. IMHO this function should has one more parameter or some settings if we already have JSON class.

My point is – output shouldn't be dependend on PHP version but on options instead.

Silly example:

/**
	 * Returns the JSON representation of a value.
	 * @param  mixed
	 * @param  int  accepts Json::PRETTY
	 * @return string
	 */
	public static function encode($value, $options = 0, $FORCE_UNICODE)
	{
		if($FORCE_UNICODE)
		{
			$flags = 0;
		}
		else
		{
			$flags = PHP_VERSION_ID >= 50400 ? (JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)) : 0;
		}

		if (PHP_VERSION_ID < 50500) {
			$json = Callback::invokeSafe('json_encode', array($value, $flags), function($message) { // needed to receive 'recursion detected' error
				throw new JsonException($message);
			});
		} else {
			$json = json_encode($value, $flags);
		}

		if ($error = json_last_error()) {
			$message = isset(static::$messages[$error]) ? static::$messages[$error]
				: (PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error');
			throw new JsonException($message, $error);
		}

		$json = str_replace(array("\xe2\x80\xa8", "\xe2\x80\xa9"), array('\u2028', '\u2029'), $json);
		return $json;
	}

Last edited by kzk_cz (2015-02-23 13:49)

Jan Tvrdík
Nette guru | 2595
+
0
-

WTF? Nette does return valid JSON with unicode characters. What is your problem?

enumag
Member | 2118
+
+2
-

@kzk_cz The default options are dependent on PHP version because the flags don't exist in PHP 5.3.

@JanTvrdík If I get it correctly he wishes to have the unicode characters escaped for whatever reason. Which means he wants to disable the JSON_UNESCAPED_UNICODE flag. It could be implemented as another flag for the $options parameter, certainly not as a third parameter.

Jan Tvrdík
Nette guru | 2595
+
0
-

@enumag I get that, but his statement are terribly confusing, e.g. he said “Nette doesn't let me choice and force non-Unicode output which is not true. Nette does return a valid UTF-8 (which is Unicode encoding) string which is a default JSON encoding.

Pavel Kravčík
Member | 1180
+
0
-

@JanTvrdík @enumag: Problems are my english skills and my brain. I'm sorry about it.

Yeah, but function json_encode can return old behaviour in new verion of PHP. But Nette disable this option with this condition.

$flags = PHP_VERSION_ID >= 50400 ? (JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)) : 0;

I just suggest some improvement. That's all. Third parametr is silly example, but I wrote there it is silly example. I just want to clearly show, what is my point.

And I can simply do it myself. Reason is I exporting JSON arrays for third side app, which stores all data in old JSON format. So I need \u0002 and not š. :)

I apologize for upseting you.

Jan Tvrdík
Nette guru | 2595
+
0
-

There is no such thing as old and new JSON format. Default encoding of JSON was always UTF-8.