5 ms·
“returning a single object without an array to represent one item.” I saw the same thing when working with PHP consuming data from a SOAP endpoint. First I tho
by Ididntdothis 7y ago
“returning a single object without an array to represent one item.”
I saw the same thing when working with PHP consuming data from a SOAP endpoint. First I thought PHP is stupid. But then I realized that XML can’t model single item arrays versus single objects. They look the same. JSON is better that way. You can model empty arrays and single item arrays.
- benoitg 7y agoYou can force PHP to handle sngle-element arrays consistently, with the SOAP_SINGLE_ELEMENT_ARRAYS option, the behaviour is much saner when enabled.
- Ididntdothis 7y agoGood to know! Unfortunately I have a call to a “FixSoapArray” all over the code already :(
- thaumasiotes 7y ago> XML can’t model single item arrays versus single objects. They look the same. <results> <result> <property>value</property> ... </result> </results> I don't really see the problem.
- Ididntdothis 7y agoYou need to know that “results” is an array so you need the schema. In JSON an array is an array without doubt.
- thaumasiotes 7y agoIf you don't know that "results" is an array... how do you handle the uncontroversial case of getting back an array of two results?
- Ididntdothis 7y agoI 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.
- chipotle_coyote 7y agoI mean, sure, but if you're creating an XML API, you've almost certainly defined the schema. If I take JSON like [ { "fruit": "banana" }, { "color": "yellow" }, [ 1 ] ] ...and turn it into XML like... <document> <fruit>banana</fruit> <color>yellow</color> <ids> <id>1</id> </ids> </document> It's going to be fairly clear what's going on. I'd generally prefer to work with JSON, too. But the silver lining of XML is that you can define virtually any schema you want, and there are times that's conceivably going to be clearer or even more concise than the equivalent JSON.
- bouke 7y agoA sample output (either XML or JSON) is not a substitute for a specification. Looking at the XML when a list has only a single element, you wouldn't know if there could be more. You need an XSD to accompany the XML, which is what is typically provided. When looking at a JSON document you might run into the same situation where the document you received doesn't contain all possible elements, and you wouldn't be the wiser. You'd still need a proper JSON schema to accompany the document.
- perl4ever 7y agoWhat would we do without ten different ways to represent nothing? if ismissing(x) then x = empty end if if isempty(x) then x = "" end if if x is nothing then x = new foo end if if right(typename(x), 2) <> "()" then x = Array(x) end if