serialization order of map in fiddle

Sunday, 29 December 2024 · 2 messages
MH
Martin Honnen29 Dec 2024 17:44 #
After some recent announcement of ordered maps in the BaseX fiddle I played around with some JSON serialization, to my surprise the result is different whether I use the serialize function directly on a single map <https://fiddle.basex.org/?share=%28%27query%21%27%2803.*2%2C%5Cn%28.30*24%21%27%27%7Emode%21%27XQuer7%7BBaseX%7D4Type%21%27xml%27%29*%29+%3D%3E+serializ9%28%22method6json%223%22indent-tru6+%3A+.%22data-arra7%28+1+to+580%22foo6bar%2229%7D8%7D3%2C+4%27%7Econtext6-%227y+8+%299e%7B%0198764320.-*_>: {'foo' : 'bar', 'data' : array { 1 to 5 }} => serialize({'method' : 'json', 'indent' : true() }), {'data' : array { 1 to 5 }, 'foo' : 'bar'} => serialize({'method' : 'json', 'indent' : true() }) gives { "foo": "bar", "data": [ 1, 2, 3, 4, 5 ] } { "data": [ 1, 2, 3, 4, 5 ], "foo": "bar" } or in a for .. return <https://fiddle.basex.org/?share=%28%27query%21%27for+%24map+in+%7B%280.-%29.%28-.0%29%7D+return+serialize%7B%24map.%28%22method3json%22.%22indent*true%7B%7D62%21%27%27%7Emode%21%27XQuer4%7BBaseX2Type%21%27xml%27%293+%3A+-%22data*arra4%28+1+to+56.%2C+0%22foo3bar%222%7D%27%7Econtext3*%224y+6+%29%0164320.-*_>:
for $map in ({'foo' : 'bar', 'data' : array { 1 to 5 }}, {'data' : array
{ 1 to 5 }, 'foo' : 'bar'}) return serialize($map, {'method' : 'json',
'indent' : true() })
gives { "foo": "bar", "data": [ 1, 2, 3, 4, 5 ] } { "foo": "bar", "data": [ 1, 2, 3, 4, 5 ] } Should the JSON serialization reflect the map as created or is that still random/arbitrary order for the properties?
CG
Christian Grün29 Dec 2024 21:24 #
Hi Martin, Thanks for the observation. It was the query optimizer that rewrote (map1, map2) to replicate(map1, 2). The order is now taken into consideration, and the two maps are treated as different items.
Note that, for backward compatibility, we can expect fn:deep-equal($map1,
$map2) to continue to return true, even if maps have a different order.
Best, Christian
Quoted text · Martin Honnen · 74 lines
On Sun, Dec 29, 2024 at 5:44 PM Martin Honnen <martin.honnen at gmx.de> wrote: > > After some recent announcement of ordered maps in the BaseX fiddle I > played around with some JSON serialization, to my surprise the result is > different whether I use the serialize function directly on a single map > <https://fiddle.basex.org/?share=%28%27query%21%27%2803.*2%2C%5Cn%28.30*24%21%27%27%7Emode%21%27XQuer7%7BBaseX%7D4Type%21%27xml%27%29*%29+%3D%3E+serializ9%28%22method6json%223%22indent-tru6+%3A+.%22data-arra7%28+1+to+580%22foo6bar%2229%7D8%7D3%2C+4%27%7Econtext6-%227y+8+%299e%7B%0198764320.-*_> > : > > {'foo' : 'bar', 'data' : array { 1 to 5 }} => serialize({'method' : > 'json', 'indent' : true() }), > {'data' : array { 1 to 5 }, 'foo' : 'bar'} => serialize({'method' : > 'json', 'indent' : true() }) > > gives > > { > "foo": "bar", > "data": [ > 1, > 2, > 3, > 4, > 5 > ] > } > { > "data": [ > 1, > 2, > 3, > 4, > 5 > ], > "foo": "bar" > } > > or in a for .. return > <https://fiddle.basex.org/?share=%28%27query%21%27for+%24map+in+%7B%280.-%29.%28-.0%29%7D+return+serialize%7B%24map.%28%22method3json%22.%22indent*true%7B%7D62%21%27%27%7Emode%21%27XQuer4%7BBaseX2Type%21%27xml%27%293+%3A+-%22data*arra4%28+1+to+56.%2C+0%22foo3bar%222%7D%27%7Econtext3*%224y+6+%29%0164320.-*_> > : > > for $map in ({'foo' : 'bar', 'data' : array { 1 to 5 }}, {'data' : array { > 1 to 5 }, 'foo' : 'bar'}) return serialize($map, {'method' : 'json', > 'indent' : true() }) > > gives > > { > "foo": "bar", > "data": [ > 1, > 2, > 3, > 4, > 5 > ] > } > { > "foo": "bar", > "data": [ > 1, > 2, > 3, > 4, > 5 > ] > } > > Should the JSON serialization reflect the map as created or is that still > random/arbitrary order for the properties? > > > > >

⚡Generated with XQuery