method fixJson documentation in bbn\X
function($json)
{
$jsonLength = strlen($json);
$escaped = false;
$opened_b = 0;
$opened_cb = 0;
$unescaped = false;
$squotes = false;
$dquotes = false;
$current = '';
$end_value = false;
$end_prop = false;
$last_quotes = '';
$prop = false;
$last_char = '';
for ($i = 0; $i < $jsonLength; $i++) {
//var_dump($a);
$add = '';
$a = $json[$i];
switch ($a) {
case '\\':
if ($escaped) {
$escaped = false;
$unescaped = true;
}
else {
$escaped = true;
}
break;
case '"':
if (!$escaped && !$squotes) {
$dquotes = !$dquotes;
$last_quotes = '"';
}
break;
case "'":
if (!$escaped && !$dquotes) {
$squotes = !$squotes;
$last_quotes = "'";
}
break;
case '{':
if (!$dquotes && !$squotes) {
$opened_cb++;
$last_quotes = "";
}
break;
case '}':
if (!$dquotes && !$squotes) {
$opened_cb--;
$end_value = true;
if ($last_char === ',') {
$newJSON = substr($newJSON, 0, -1);
}
}
break;
case '[':
if (!$dquotes && !$squotes) {
$opened_b++;
$last_quotes = "";
}
break;
case ']':
if (!$dquotes && !$squotes) {
$opened_b--;
$end_value = true;
if ($last_char === ',') {
$newJSON = substr($newJSON, 0, -1);
}
}
break;
case ':':
if (!$dquotes && !$squotes) {
$end_prop = true;
}
break;
case ',':
if (!$dquotes && !$squotes) {
$end_value = true;
}
break;
case '/':
if ($last_char !== '\\') {
//$current .= '\\';
}
break;
default:
if ($escaped) {
$escaped = false;
}
if ($unescaped) {
$unescaped = false;
}
}
if ($end_prop) {
if ($last_quotes === '"') {
$add .= $current;
}
elseif ($last_quotes === "'") {
$current = trim($current);
$add .= '"'.Str::escapeDquote(Str::unescapeSquote(substr($current, 1, -1))).'":';
}
else {
$add .= '"'.Str::escapeDquote($current).'":';
}
$end_prop = false;
}
elseif ($end_value) {
if ($current) {
if ($last_quotes) {
$current = trim($current);
$add .= '"'.Str::escapeDquote(substr($current, 1, -1)).'"';
}
else {
$add .= Str::escapeDquote($current);
}
if ($a !== ' ') {
$add .= $a;
}
}
else {
$current .= $a;
}
$last_quotes = "";
$end_value = false;
}
elseif (!$dquotes && !$squotes && (($a === '[') || ($a === '{'))) {
$add .= $a;
}
elseif ($dquotes || $squotes || ($a !== ' ')) {
$current .= $a;
}
if ($add) {
$newJSON .= $add;
$current = '';
}
if ($a !== ' ') {
$last_char = $a;
}
}
if ($current) {
$newJSON .= $current;
}
return $newJSON;
}
BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions