Skip to content

Commit

Permalink
#1109: change json output generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mimmi20 committed Oct 31, 2016
1 parent 41c6a92 commit 12a4ebb
Showing 1 changed file with 6 additions and 55 deletions.
61 changes: 6 additions & 55 deletions src/Browscap/Formatter/JsonFormatter.php
Expand Up @@ -51,7 +51,7 @@ public function getType()
*/
public function formatPropertyName($name)
{
return $this->jsonEncode($name);
return json_encode($name);
}

/**
Expand All @@ -68,26 +68,26 @@ public function formatPropertyValue($value, $property)

switch ($propertyHolder->getPropertyType($property)) {
case PropertyHolder::TYPE_STRING:
$valueOutput = $this->jsonEncode(trim($value));
$valueOutput = json_encode(trim($value));
break;
case PropertyHolder::TYPE_BOOLEAN:
if (true === $value || $value === 'true') {
$valueOutput = '"true"';
$valueOutput = 'true';
} elseif (false === $value || $value === 'false') {
$valueOutput = '"false"';
$valueOutput = 'false';
} else {
$valueOutput = '""';
}
break;
case PropertyHolder::TYPE_IN_ARRAY:
try {
$valueOutput = $this->jsonEncode($propertyHolder->checkValueInArray($property, $value));
$valueOutput = json_encode($propertyHolder->checkValueInArray($property, $value));
} catch (\InvalidArgumentException $ex) {
$valueOutput = '""';
}
break;
default:
$valueOutput = $this->jsonEncode($value);
$valueOutput = json_encode($value);
break;
}

Expand Down Expand Up @@ -117,53 +117,4 @@ public function getFilter()
{
return $this->filter;
}

/**
* @param string $val
*
* @return string
*/
private function jsonEncode($val)
{
if ($val === null) {
return '"null"';
}

if ($val === true) {
return '"true"';
}

if ($val === false) {
return '"false"';
}

if (is_string($val)) {
return json_encode($val);
}

if (is_numeric($val)) {
return '"' . json_encode($val) . '"';
}

$assoc = false;
$i = 0;
foreach ($val as $k => $v) {
if ($k !== $i++) {
$assoc = true;
break;
}
}
$res = [];
foreach ($val as $k => $v) {
$v = $this->jsonEncode($v);
if ($assoc) {
$k = json_encode($k);
$v = $k . ':' . $v;
}
$res[] = $v;
}
$res = implode(',', $res);

return ($assoc) ? '{' . $res . '}' : '[' . $res . ']';
}
}

0 comments on commit 12a4ebb

Please sign in to comment.