Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Maarch
maarchRM
Commits
68ff3d22
Verified
Commit
68ff3d22
authored
Mar 03, 2020
by
Cyril Vazquez
Browse files
Unescape JSON strings when parsing
parent
99846ef3
Pipeline
#7129
failed with stages
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
core/Encoding/JsonTokenizer.php
View file @
68ff3d22
...
...
@@ -217,54 +217,21 @@ class JsonTokenizer
$tail
=
substr
(
$chunk
,
0
,
$matches
[
0
][
1
]);
$end
=
strlen
(
$tail
);
$size
+=
$end
;
fwrite
(
$buffer
,
$tail
);
fwrite
(
$buffer
,
$this
->
unescape
(
$tail
)
)
;
fseek
(
$this
->
stream
,
(
-
$length
+
$end
+
1
),
SEEK_CUR
);
rewind
(
$buffer
);
if
(
$size
<
$this
->
threshold
)
{
return
stream_get_contents
(
$buffer
);
return
$this
->
unescape
(
stream_get_contents
(
$buffer
)
)
;
}
return
$buffer
;
}
fwrite
(
$buffer
,
$chunk
);
fwrite
(
$buffer
,
$this
->
unescape
(
$chunk
)
)
;
$size
+=
$this
->
threshold
;
}
while
(
$chunk
);
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
{
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment