Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialize-json() bug - extra commas #7

Closed
joewiz opened this issue Dec 20, 2013 · 4 comments
Closed

serialize-json() bug - extra commas #7

joewiz opened this issue Dec 20, 2013 · 4 comments

Comments

@joewiz
Copy link
Owner

joewiz commented Dec 20, 2013

The serialize-json() function appears to be inserting extra commas in the result. Here's a reproducible test, using eXide in the latest revision from the eXist-db develop branch:

xquery version "3.0";

import module namespace xqjson="http://xqilla.sourceforge.net/lib/xqjson";

let $json := '[1,[2,3]]'
let $parsed := xqjson:parse-json($json)
let $re-serialized := xqjson:serialize-json($parsed)
return
    $re-serialized

This returns '[1,[,2,3]]' instead of the expected original input. Notice the extra comma preceding 2.

The parse-json() function doesn't seem to be the cause of the problem, since $parsed is correct here:

<json type="array">
  <item type="number">1</item>
  <item type="array">
    <item type="number">2</item>
    <item type="number">3</item>
  </item>
</json>

So the problem as I see it is with the serialize-json() function.

@Albicocca
Copy link
Contributor

This problem is caused by incorrect using of position() in function xqjson:serializeJSONObject.
To fix it, you can change the function from:

declare %private function xqjson:serializeJSONObject($e as element())
  as xs:string*
{
  "{",
  $e/*/(
    if(position() = 1) then () else ",",
    '"', xqjson:escape-json-string(@name), '":',
    xqjson:serializeJSONElement(.)
  ),
  "}"
}; 

to this:

declare %private function xqjson:serializeJSONObject($e as element())
  as xs:string*
{
  "{",
  for $el at $pos in $e/* return
 (
    if($pos = 1) then () else ",",
    '"', xqjson:escape-json-string($el/@name), '":',
    xqjson:serializeJSONElement($el)
  ),
  "}"
}; 

@joewiz
Copy link
Owner Author

joewiz commented May 6, 2014

Wonderful! I'd be happy to accept a pull request with this fix.

@joewiz
Copy link
Owner Author

joewiz commented May 7, 2014

Thanks for the pull request, @Albicocca. After further testing with the test example in this issue, I realized that I need to apply your logic to xqjson:serializeJSONArray() too. Now works.

@joewiz
Copy link
Owner Author

joewiz commented May 7, 2014

Fixed by d193107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants