Skip to content

Instantly share code, notes, and snippets.

@GromNaN
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GromNaN/a00965b384fddf9f48c3 to your computer and use it in GitHub Desktop.
Save GromNaN/a00965b384fddf9f48c3 to your computer and use it in GitHub Desktop.
JSONX.php
<?php
function parseJsonx(\DOMNode $node)
{
switch ($node->nodeName)
{
case 'json:object':
$data = new stdClass();
foreach ($node->childNodes as $childNode) {
if ($childNode instanceof \DOMElement
&& null !== $childNode->attributes
&& $name = $childNode->attributes->getNamedItem('name')) {
$data->{$name->nodeValue} = parseJsonx($childNode);
}
}
break;
case 'json:array':
$data = array();
foreach ($node->childNodes as $index => $childNode) {
if ($childNode instanceof \DOMElement) {
$data[] = parseJsonx($childNode);
}
}
break;
case 'json:string':
$data = (string) $node->nodeValue;
break;
case 'json:boolean':
$data = 'true' == strtolower($node->nodeValue);
break;
case 'json:number':
$data = (float) $node->nodeValue;
break;
case 'json:null':
$data = null;
break;
default:
return;
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment