Verified Commit 68ff3d22 authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Unescape JSON strings when parsing

parent 99846ef3
Pipeline #7129 failed with stages
...@@ -217,54 +217,21 @@ class JsonTokenizer ...@@ -217,54 +217,21 @@ class JsonTokenizer
$tail = substr($chunk, 0, $matches[0][1]); $tail = substr($chunk, 0, $matches[0][1]);
$end = strlen($tail); $end = strlen($tail);
$size += $end; $size += $end;
fwrite($buffer, $tail); fwrite($buffer, $this->unescape($tail));
fseek($this->stream, (-$length+$end+1), SEEK_CUR); fseek($this->stream, (-$length+$end+1), SEEK_CUR);
rewind($buffer); rewind($buffer);
if ($size < $this->threshold) { if ($size < $this->threshold) {
return stream_get_contents($buffer); return $this->unescape(stream_get_contents($buffer));
} }
return $buffer; return $buffer;
} }
fwrite($buffer, $chunk); fwrite($buffer, $this->unescape($chunk));
$size += $this->threshold; $size += $this->threshold;
} while ($chunk); } while ($chunk);
return $buffer; return $buffer;
while (true) {
$char = fread($this->stream, 1);
// Unterminated string (waiting for quotes)
if ($char === false || $char === "") {
throw new Exception("String not terminated correctly " . ftell($this->stream));
}
// Terminated string
if ($quotes == $char && !$escaped) {
if (is_resource($buffer)) {
return $buffer;
} else {
return json_decode($quotes . $buffer . $quotes);
}
}
// Continued
if (is_string($buffer)) {
$buffer .= $char;
$size++;
if (strlen($buffer) == $this->threshold) {
$tmp = fopen('php://temp', 'w+');
fwrite($tmp, $buffer);
$buffer = $tmp;
}
} else {
fwrite($buffer, $char);
}
$escaped = !$escaped && $quotes === "\"" && $char == "\\";
}
} }
/** /**
...@@ -332,4 +299,16 @@ class JsonTokenizer ...@@ -332,4 +299,16 @@ class JsonTokenizer
{ {
return end($this->context); return end($this->context);
} }
/**
* Unescape string or chunk
*/
protected function unescape($string)
{
$escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
$replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
$result = str_replace($replacements, $escapers, $string);
return $result;
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment