Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.