json 2.0 bug: json:serialize(json:parse(VALID_JSON))

Bug #920717 reported by Sorin Marian Nasoi
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Zorba
Fix Released
Medium
Paul J. Lucas

Bug Description

Should be possible to call json:serialize(json:parse(VALID_JSON))
where VALID_JSON is any valid JSON string
see added failing test test/rbkt/zorba/json/json-snelson-serialize-parse:

import module namespace json="http://www.zorba-xquery.com/modules/converters/json";
declare variable $json-value := '{"html":"<b>bold</b>"}';
json:serialize(json:parse($json-value))

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

I think this is happening because of the was json:parse() is declared to return an element(*,xs:untyped)*. I need help to (1) confirm and (2) how to fix it. I don't think this bug has anything to do with my JSON parsing/serializing code per se.

Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

I investigated this issue a little more. The problem seems to be in the optimizer.

zorba -q tmp.xq -f --optimization-level O0

returns the correct result.

<?xml version="1.0" encoding="UTF-8"?>
{"html":"&lt;b&gt;bold&lt;/b&gt;"}

whereas

zorba -q tmp.xq -f --optimization-level O1

raises the error.

Tracing this further, it seems as if the json:parse function returns the options element created in json:serialize (if the optimizer is used). It clearly returns the correct value if the query is used without json:serialize(...).

I'm assigning this to Markos to help looking into this. If I made a guess, I would say that the problem is in the no-copy rule.

Changed in zorba:
assignee: Paul J. Lucas (paul-lucas) → Markos Zaharioudakis (markos-za)
Revision history for this message
Markos Zaharioudakis (markos-za) wrote :

On which branch does the bug show up? I ran the query in the jsoniq branch, and I get the following result (same for all optimization levels):

<?xml version="1.0" encoding="UTF-8"?>
&lt;json type="object" xmlns="http://www.zorba-xquery.com/modules/converters/json"&gt;&lt;pair name="html" type="string"&gt;&lt;b&gt;bold&lt;/b&gt;&lt;/pair&gt;&lt;/json&gt;

Is this result correct?

If the bug shows up in another branch, maybe that branch is not up-to-date w.r.t. the trunk? Recently, I have fixed a couple of bugs related to the no-copy rule, so if the problem is really with the no-copy rule, then the branch must be updated.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

The bug shows up on the feature-json_parser branch. The test that fails is json-snelson-serialize-parse.xq. The test still fails even after a merge from the trunk.

Revision history for this message
Markos Zaharioudakis (markos-za) wrote :

Ok, I was able to reproduce it. But it's not due to the no-copy rule: I get the same error if I disable that rule. I will investigate further.

Revision history for this message
Markos Zaharioudakis (markos-za) wrote :

I fixed the bug. It was in JSONParseInternal::nextImpl()

Changed in zorba:
status: New → Fix Committed
Revision history for this message
William Candillon (wcandillon) wrote :

The following query:
import module namespace json = "http://www.zorba-xquery.com/modules/converters/json";

let $json := '{ "action": "GET /html/index", "public": true, "opt": [1, 2, {"foo": "bar"}]}'
let $json := json:parse($json)
return json:serialize($json)

Returns:
URI_PATH/com/zorba-xquery/www/modules/converters/json.xq>:196,3: JSON serialization error [zerr:ZJSE0006]: JSON type "number" can not have a child element node
=================================================
json:serialize#1 <http://www.zorba-xquery.com/modules/converters/json>

I think that this result is not correct.

Changed in zorba:
status: Fix Committed → In Progress
assignee: Markos Zaharioudakis (markos-za) → Paul J. Lucas (paul-lucas)
milestone: none → 2.2
Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

It was an unrelated bug.

Changed in zorba:
status: In Progress → Fix Committed
Revision history for this message
William Candillon (wcandillon) wrote :

The following query:
import module namespace json = "http://www.zorba-xquery.com/modules/converters/json";

let $json := '{ "action": "GET /html/index", "public": true, "opt": [1, 2, {"foo": "bar"}, null, false, true]}'
let $json := json:parse($json)
return json:serialize($json)

Returns:
JSON serialization error [zerr:ZJSE0005]: "item": illegal child element of "object" type; must be "pair"

Changed in zorba:
status: Fix Committed → In Progress
Revision history for this message
William Candillon (wcandillon) wrote :

Please don't close this bug, assign it back to me if you think that it is fixed.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

These are DIFFERENT bugs. They have NOTHING to do with serialization. When you have a query that fails, you need to try to get it to the SMALLEST possible query that fails. In this and the previous case, the call to json:serialize() is not necessary to show the bug.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

OK, I've fixed your latest bug. For the record, the smallest possible query that showed the bug was:

import module namespace json = "http://www.zorba-xquery.com/modules/converters/json";

let $json := '{ "k1" : [ { "k2" : "v2" }, null ] }'
return json:parse( $json )

It would help if, instead of reporting bugs one-at-a-time, having me fix them, me thinking the bug is fixed, changing the status, updating the bug comment, etc, if you reported all your bugs at once.

If you find any more, please open a NEW bug since the previous 2 bugs you've reported have nothing to do with this bug.

Changed in zorba:
status: In Progress → Fix Committed
assignee: Paul J. Lucas (paul-lucas) → William Candillon (wcandillon)
Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

Bug owner changed to you per your request.

Changed in zorba:
status: Fix Committed → In Progress
assignee: William Candillon (wcandillon) → Paul J. Lucas (paul-lucas)
Revision history for this message
William Candillon (wcandillon) wrote :

The following query:
import module namespace json = "http://www.zorba-xquery.com/modules/converters/json";

let $json := doc("tweet.json")
return json:serialize($json)

Where tweet.json is the result from json:parse() (the file is attached).

Returns:
json.xq>:196,3: JSON serialization error [zerr:ZJSE0007]: JSON type "object" can not have a child text node
=================================================
json:serialize#1 <http://www.zorba-xquery.com/modules/converters/json>

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

I'm closing this bug for hopefully the last time. As we discussed, your recent bugs are UNRELATED to this bug. When you get another JSON example, open a NEW bug. Thanks.

Changed in zorba:
status: In Progress → Fix Committed
Changed in zorba:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.