Re: SimpleXML and JSON

From: Date: Tue, 15 Aug 2023 12:09:59 +0000
Subject: Re: SimpleXML and JSON
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-120910@lists.php.net to get a copy of this message
On 14/08/2023 23:56, Rowan Tommins wrote: > On 14 August 2023 13:40:40 BST, Niels Dossche <dossche.niels@gmail.com> wrote: >>And you load it into simpleXML, the result of calling json_encode($the_simplexml_object) > > My usual reaction to this is "why would you take an object designed for accessing parts of > an XML document, and serialise it to JSON?" Often, the answer turns out to be "because I > don't understand SimpleXML objects, and have copied and pasted a weird hack to get a less > useful array representation by round-tripping to JSON". > That's fair, I'm just trying to hear what people want here :-) > On the other hand, the fact that the *debug* representation of SimpleXML objects misses out > some parts causes a lot of confusion, and I've actually considered the *opposite* of what you > suggest - leave the JSON alone, because people will have written production code based on it, but > make the debug array more descriptive of how to use the object. > I agree the debug info is lackluster at the moment. e.g. If you var_dump a simpleXML object you don't even see the name or the type. So when you're debugging it becomes a bit more difficult to see what you're working with. Similarly, a getType() method would be handy to verify what's actually "behind" the simpleXML object. > (...) > > Ultimately, I'm not that interested in trying to come up with a JSON or array > representation that covers every possibility, because I think the only consistent answer would be > horribly verbose - basically, describe every property that DOM would expose on each node. Probably yeah, I tried to come up with something but it was indeed verbose. > For debug output, the main concern is showing what you'll get with various styles of > access in SimpleXML, so a single "@text" => "foobarbaz" would make sense; or > maybe even "(string)" => "foobarbaz" and rename "@attributes" to > "->attributes()" Having "->method()" might look a bit weird, at least I haven't seen something like this in other PHP extensions. Although it is more descriptive than "@attributes", because people may be led to believe you can do $simpleXML->{"@attributes"} or something like that. Adding a name and type in the output is also necessary I think. Making such changes probably requires an RFC and some bikeshedding to make sure everyone agrees on it. > > Regards, > Kind regards

« previous php.internals (#120910) next »