XML fragments in JSON serialization

80 views
Skip to first unread message

Chris

unread,
Apr 26, 2022, 3:41:26 PM4/26/22
to Blockly
Hi,

In our application we generate a serialised Blockly workspace that we subsequently load using the Blockly deserialization functions. I am a bit confused about the requirement for an XML 'extraState' in the JSON serialisation of the 'lists_getIndex' block, i.e.:

      {
        "type": "lists_getIndex",
        "id": "PgTtxza3@08!tgP(kAEJ",
        "x": -12,
        "y": 13,
        "extraState": "<mutation statement=\"true\" at=\"false\"></mutation>",
        "fields": {
          "MODE": "REMOVE",
          "WHERE": "FIRST"
        }
      }

Line 449 of https://github.com/google/blockly/blob/master/blocks/lists.js says that this is for backwards compatibility, which might suggest that it isn't necessary for restoring blocks using the new JSON serialization system, however this does not appear to be true for all cases.

If I do the following:
1. Open the Blockly Playground
2. Drag a 'lists_getIndex' block from the 'Lists' section into the workspace
3. Click the 'get' dropdown and change to 'remove'
4. Drag a 'controls_forEach' block from the 'Loops' section into the workspace
5. Attach the 'lists_getIndex' block into the body of the loop
6. Click 'Save JSON'
7. Remove the 'extraState' property from the generated JSON
8. Click 'Load'

I get the exception 'Error: The block "lists_getIndex" block (id="PgTtxza3@08!tgP(kAEJ") is missing a(n) previous connection' from line 485 of https://github.com/google/blockly/blob/master/core/serialization/blocks.js

I do not get the error after removing the 'extraState' if the 'lists_getIndex' block is at its default values or is not contained within another block.

I can easily just create the required XML fragments in my generated blocks, however I am curious as to why this 'extraState' is stored as XML instead of JSON, as it does seem necessary for successful JSON deserialization in some cases. Am I missing something? I did look through the documentation, but have not yet been able find anything.

Thanks,

Chris

Beka Westberg

unread,
May 2, 2022, 12:08:34 PM5/2/22
to Blockly
Thank you for reporting this issue! When I upgraded the block definitions to support the JSON serializer, I didn't realize that this block had a validator that modified it next/output connections. In that case it *does* need some extraState, while it does not if it only modifies its input connections.

I've filed an issue to track fixing this =) No action is needed from you, just leave the `extraState` in there because it *is* necessary.

Thank you again! Best wishes,
--Beka

Chris

unread,
May 3, 2022, 10:35:23 AM5/3/22
to Blockly
Thanks very much for the clear explanation and adding the issue, that's very helpful.

Best wishes,

Chris

Gregory Dyke

unread,
May 9, 2022, 8:40:13 PM5/9/22
to Blockly
Great coincidence! My heavily-inspired-by-lists_getIndex block does not have the xml hooks (as I uses only json serialisation) - and of course, it does not work when it is shape-of-a-statement.

I'm so glad this was such an easy find for me and thanks for the issue!

Cheers
Greg
Reply all
Reply to author
Forward
0 new messages