4 ms·
I know that but the PHP deserializer doesn’t know and gives me back an object instead of an array of one. If it’s two it detects an array.
by Ididntdothis 7y ago
I know that but the PHP deserializer doesn’t know and gives me back an object instead of an array of one. If it’s two it detects an array.
- thaumasiotes 7y agoBut it's always a single object. If it has two parallel nodes at the document root, it isn't valid XML. So an array of two results looks like this: <results> <result></result> <result></result> </results> and PHP automatically treats results as a two-element array, but for the one-element array <results> <result></result> </results> you just get a parse error?
- Ididntdothis 7y agoNot a parse error but a single “result” object, not an array of length 1. With two elements it recognizes an array.
- thaumasiotes 7y agoIf you can parse <results> <result></result> </results> and you end up with a single "result" object, you must have identified "results" as being an array of one element so that you could discard it. (You didn't get a single "results" object.) That's not a problem in what XML is able to model, it's a problem with you throwing the information you got away and then pretending you didn't receive it. On the other hand, if you meant that you get a single "results" object, then you're saying that your code is written to require invalid XML and fall down when valid XML is provided. Again, that sounds more like a problem with your code than with the modeling capabilities of XML. There's an argument to be made for accepting invalid input; rejecting valid input is ridiculous.
- vaidhy 7y agoYou do understand that php and quite a few other languages provide pre-built serialization and de-serialization from XML, right? The OP was talking about the out of box behaviour for those. Your answer seems to predicate on having written a custom deserializer.
- thaumasiotes 7y agoMy answer is predicated on the idea that you should be using a deserializer that doesn't rely on receiving invalid input, regardless of whether that deserializer comes from a standard library.