Skip to content

Commit 3fadb01

Browse files
committed
updated json2xml.php
1 parent 7e0c241 commit 3fadb01

File tree

1 file changed

+63
-79
lines changed

1 file changed

+63
-79
lines changed

api.php

Lines changed: 63 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -773,108 +773,92 @@ public function rollbackTransaction() {
773773
}
774774

775775
public function jsonEncode($object) {
776+
$a = $object;
777+
$d = new DOMDocument();
778+
$c = $d->createElement("root");
779+
$d->appendChild($c);
776780
$t = function($v) {
777-
switch(gettype($v)) {
778-
case 'boolean': return 'boolean';
781+
$type = gettype($v);
782+
switch($type) {
779783
case 'integer': return 'number';
780784
case 'double': return 'number';
781-
case 'string': return 'string';
782-
case 'array': return 'array';
783-
case 'object': return 'object';
784-
case 'NULL': return 'null';
785+
default: return strtolower($type);
785786
}
786787
};
787-
$a = $object;
788-
$c = new SimpleXMLElement('<root/>');
789-
$f = function($f,$c,$a,$s=false) use ($t) {
790-
$c->addAttribute('type', $t($a));
791-
if (is_scalar($a)||is_null($a)) {
792-
if(is_bool($a)){
793-
$c[0] = $a?'true':'false';
788+
$f = function($f,$c,$a,$s=false) use ($t,$d) {
789+
$c->setAttribute('type', $t($a));
790+
if ($t($a) != 'array' && $t($a) != 'object') {
791+
if ($t($a) == 'boolean') {
792+
$c->appendChild($d->createTextNode($a?'true':'false'));
794793
} else {
795-
$c[0] = $a;
794+
$c->appendChild($d->createTextNode($a));
796795
}
797796
} else {
798797
foreach($a as $k=>$v) {
799-
if ($k=='__type' && is_object($a)) {
800-
$c->addAttribute('__type', $v);
798+
if ($k == '__type' && $t($a) == 'object') {
799+
$c->setAttribute('__type', $v);
801800
} else {
802-
if(is_object($v)) {
803-
$ch=$c->addChild($s?'item':$k);
804-
$f($f,$ch,$v);
805-
} else if(is_array($v)) {
806-
$ch=$c->addChild($s?'item':$k);
807-
$f($f,$ch,$v,true);
808-
} else if(is_bool($v)) {
809-
$ch=$c->addChild($s?'item':$k,$v?'true':'false');
810-
$ch->addAttribute('type', $t($v));
801+
if ($t($v) == 'object') {
802+
$ch = $c->appendChild($d->createElementNS(null, $s ? 'item' : $k));
803+
$f($f, $ch, $v);
804+
} else if ($t($v) == 'array') {
805+
$ch = $c->appendChild($d->createElementNS(null, $s ? 'item' : $k));
806+
$f($f, $ch, $v, true);
811807
} else {
812-
$ch=$c->addChild($s?'item':$k,$v);
813-
$ch->addAttribute('type', $t($v));
808+
$va = $d->createElementNS(null, $s ? 'item' : $k);
809+
if ($t($v) == 'boolean') {
810+
$va->appendChild($d->createTextNode($v?'true':'false'));
811+
} else {
812+
$va->appendChild($d->createTextNode($v));
813+
}
814+
$ch = $c->appendChild($va);
815+
$ch->setAttribute('type', $t($v));
814816
}
815817
}
816818
}
817819
}
818820
};
819821
$f($f,$c,$a,$t($a)=='array');
820-
return trim($c->asXML());
822+
return $d->saveXML($d->documentElement);
821823
}
822824

823825
public function jsonDecode($string) {
824-
$p = function ($p,$n) {
825-
foreach($n->childNodes as $node) {
826-
if($node->hasChildNodes()) {
827-
$p($p,$node);
828-
} else {
829-
if($n->hasAttributes() && strlen($n->nodeValue)){
830-
$n->setAttribute('value', $node->textContent);
831-
$node->nodeValue = '';
832-
}
833-
}
834-
}
826+
$a = dom_import_simplexml(simplexml_load_string($string));
827+
$t = function($v) {
828+
return $v->getAttribute('type');
835829
};
836-
$dom = new DOMDocument();
837-
$dom->loadXML($string);
838-
$p($p,$dom);
839-
$xml = simplexml_load_string($dom->saveXML());
840-
$a = json_decode(json_encode($xml));
841-
$f = function($f,&$a) {
842-
foreach($a as $k=>&$v) {
843-
if($k==='@attributes') {
844-
if (isset($v->__type) && is_object($a)) {
845-
$a->__type = $v->__type;
846-
}
847-
if ($v->type=='null') {
848-
$a = null;
849-
return;
850-
}
851-
if ($v->type=='boolean') {
852-
$b = substr(strtolower($v->value[0]),0,1);
853-
$a = in_array($b,array('1','t'));
854-
return;
855-
}
856-
if ($v->type=='number') {
857-
$a = $v->value+0;
858-
return;
859-
}
860-
if ($v->type=='string') {
861-
$a = $v->value;
862-
return;
863-
}
864-
unset($a->$k);
865-
} else {
866-
if (is_object($v)) {
867-
$f($f,$v);
868-
} else if (is_array($v)) {
869-
$f($f,$v);
870-
$a = $v;
871-
return;
872-
}
873-
}
830+
$f = function($f,$a) use ($t) {
831+
$c = null;
832+
if ($t($a)=='null') {
833+
$c = null;
834+
} else if ($t($a)=='boolean') {
835+
$b = substr(strtolower($a->textContent),0,1);
836+
$c = in_array($b,array('1','t'));
837+
} else if ($t($a)=='number') {
838+
$c = $a->textContent+0;
839+
} else if ($t($a)=='string') {
840+
$c = $a->textContent;
841+
} else if ($t($a)=='object') {
842+
$c = array();
843+
if ($a->getAttribute('__type')) {
844+
$c['__type'] = $a->getAttribute('__type');
845+
}
846+
for ($i=0;$i<$a->childNodes->length;$i++) {
847+
$v = $a->childNodes[$i];
848+
$c[$v->nodeName] = $f($f,$v);
874849
}
850+
$c = (object)$c;
851+
} else if ($t($a)=='array') {
852+
$c = array();
853+
for ($i=0;$i<$a->childNodes->length;$i++) {
854+
$v = $a->childNodes[$i];
855+
$c[$i] = $f($f,$v);
856+
}
857+
}
858+
return $c;
875859
};
876-
$f($f,$a);
877-
return $a;
860+
$c = $f($f,$a);
861+
return $c;
878862
}
879863
}
880864

0 commit comments

Comments
 (0)