RestXQ returning JSON

Monday, 23 January 2017 · 4 messages · last reply 24 Jan 2017
ML
Marco Lettere23 Jan 2017 09:31 #
Hello all, this morning just stumbled into this issue and I'm wondering whether it could be somehow smoothed up ... I have the following RestXQ function: declare %rest:path("/f") %rest:GET %output:method("json") function a:f() { (:code ...:) }; 1) Replacing comment with the following code correctly returns an empty object:
   let $out := (<json type="object"></json>)
   return $out
2) Replacing comment with the following code correctly returns an expected exception (" [SERE0023] Only one item can be serialized with JSON." :
let $out := (<json type="object"></json>, <json type="object"></json>)
   return $out
3) Replacing comment with the following code returns a somewhat less expected Null-pointer exception :
let $out := (<json type="object"></json>, <json type="object"></json>)
return array{$out}
4) In order to get the expected Json array I need to write the code like the following:
  let $out := (<json type="object"></json>, <json type="object"></json>)
  return array{ $out ! json:parse(json:serialize(.), map{ "format" :
"map"}) }
I'm not sure whether the array could be inferred from the sequence directly in a pattern like 2. But I would expect that 3) should work. Isn't it? Thanks for any explanation. Regards, Marco.
FE
Fabrice ETANCHAUD23 Jan 2017 09:59 #
Hi Marco, What about using a root element like :
<json type='array'>
...
</json>
Best regards, Fabrice ETANCHAUD -----Message d'origine-----
Quoted text · Marco Lettere · 32 lines
De : basex-talk-bounces at mailman.uni-konstanz.de [mailto:basex-talk-bounces at mailman.uni-konstanz.de] De la part de Marco Lettere Envoyé : lundi 23 janvier 2017 09:32 À : BaseX <basex-talk at mailman.uni-konstanz.de> Objet : [basex-talk] RestXQ returning JSON Hello all, this morning just stumbled into this issue and I'm wondering whether it could be somehow smoothed up ... I have the following RestXQ function: declare %rest:path("/f") %rest:GET %output:method("json") function a:f() { (:code ...:) }; 1) Replacing comment with the following code correctly returns an empty object: let $out := (<json type="object"></json>) return $out 2) Replacing comment with the following code correctly returns an expected exception (" [SERE0023] Only one item can be serialized with JSON." : let $out := (<json type="object"></json>, <json type="object"></json>) return $out 3) Replacing comment with the following code returns a somewhat less expected Null-pointer exception : let $out := (<json type="object"></json>, <json type="object"></json>) return array{$out} 4) In order to get the expected Json array I need to write the code like the following: let $out := (<json type="object"></json>, <json type="object"></json>) return array{ $out ! json:parse(json:serialize(.), map{ "format" : "map"}) } I'm not sure whether the array could be inferred from the sequence directly in a pattern like 2. But I would expect that 3) should work. Isn't it? Thanks for any explanation. Regards, Marco.
CG
Christian Grünin reply to Marco Lettere, 23 Jan 2017, 09:3123 Jan 2017 17:24 #
Hi Marco, Fabrice has already given you the correct hint: Wrapping your json elements with <json <json type='array'> should do the trick. Anyway, thanks for the hint to the NullPointerException, this issue has been fixed. Cheers, Christian
Quoted text · Marco Lettere · 50 lines
On Mon, Jan 23, 2017 at 9:31 AM, Marco Lettere <m.lettere at gmail.com> wrote: > Hello all, > > this morning just stumbled into this issue and I'm wondering whether it > could be somehow smoothed up ... > > I have the following RestXQ function: > > declare > %rest:path("/f") > %rest:GET > %output:method("json") > function a:f() { > (:code ...:) > }; > > 1) Replacing comment with the following code correctly returns an empty > object: > > let $out := (<json type="object"></json>) > return $out > > 2) Replacing comment with the following code correctly returns an expected > exception (" [SERE0023] Only one item can be serialized with JSON." : > > let $out := (<json type="object"></json>, <json type="object"></json>) > return $out > > 3) Replacing comment with the following code returns a somewhat less > expected Null-pointer exception : > > let $out := (<json type="object"></json>, <json type="object"></json>) > return array{$out} > > 4) In order to get the expected Json array I need to write the code like the > following: > > let $out := (<json type="object"></json>, <json type="object"></json>) > return array{ $out ! json:parse(json:serialize(.), map{ "format" : "map"}) > } > > I'm not sure whether the array could be inferred from the sequence directly > in a pattern like 2. But I would expect that 3) should work. Isn't it? > > Thanks for any explanation. > > Regards, > > Marco. >
ML
Marco Lettere24 Jan 2017 09:51 #
Hi Christian and Fabrice, thanks for your suggestion which indeed makes sense! :D Happy that at least my message has been useful for something else. Regards, Marco.
Quoted text · Christian Grün · 65 lines
On 23/01/2017 17:24, Christian Grün wrote: > Hi Marco, > > Fabrice has already given you the correct hint: Wrapping your json > elements with <json <json type='array'> should do the trick. > > Anyway, thanks for the hint to the NullPointerException, this issue > has been fixed. > > Cheers, > Christian > > > > > On Mon, Jan 23, 2017 at 9:31 AM, Marco Lettere <m.lettere at gmail.com> wrote: >> Hello all, >> >> this morning just stumbled into this issue and I'm wondering whether it >> could be somehow smoothed up ... >> >> I have the following RestXQ function: >> >> declare >> %rest:path("/f") >> %rest:GET >> %output:method("json") >> function a:f() { >> (:code ...:) >> }; >> >> 1) Replacing comment with the following code correctly returns an empty >> object: >> >> let $out := (<json type="object"></json>) >> return $out >> >> 2) Replacing comment with the following code correctly returns an expected >> exception (" [SERE0023] Only one item can be serialized with JSON." : >> >> let $out := (<json type="object"></json>, <json type="object"></json>) >> return $out >> >> 3) Replacing comment with the following code returns a somewhat less >> expected Null-pointer exception : >> >> let $out := (<json type="object"></json>, <json type="object"></json>) >> return array{$out} >> >> 4) In order to get the expected Json array I need to write the code like the >> following: >> >> let $out := (<json type="object"></json>, <json type="object"></json>) >> return array{ $out ! json:parse(json:serialize(.), map{ "format" : "map"}) >> } >> >> I'm not sure whether the array could be inferred from the sequence directly >> in a pattern like 2. But I would expect that 3) should work. Isn't it? >> >> Thanks for any explanation. >> >> Regards, >> >> Marco. >>

⚡Generated with XQuery