Converting XML toolbox to JSON

132 views
Skip to first unread message

Paul Jarvis

unread,
Sep 21, 2023, 6:11:59 PM9/21/23
to Blockly
I have the following section of XML in my toolbox and so far have not found how to map it to JSON.

            <category name="If">
                <block type="controls_if"></block>
                <block type="controls_if">
                    <mutation else="1"></mutation>
                </block>
                <block type="controls_if">
                    <mutation elseif="1" else="1"></mutation>
                </block>
            </category>

It is setting the mutations that has me stumped.

Paul

Maribeth Moffatt

unread,
Sep 23, 2023, 12:12:40 PM9/23/23
to Blockly
Hello,

Mutations work a bit differently in JSON. In XML, extra block data is serialized into the <mutation> elements like you showed above. In JSON, this data is saved as part of the extraState property on the block data:

    {
        "type": "controls_if",
        "extraState": {
          "hasElse": true
        }
      }

What the property is called depends on the block definition. This will be controlled by the `saveExtraState` and `loadExtraState` methods of the block definition. For learning how the built-in blocks work, you can also try using the advanced playground. You can drag blocks out into the workspace and then switch between the XML and JSON tabs to compare.

I hope that helps! Best,
Maribeth

Paul Jarvis

unread,
Sep 23, 2023, 5:20:31 PM9/23/23
to Blockly
Hi Maribeth,

Thanks for that - and the 'advanced playground' link is very useful.

Cheers
Paul
Reply all
Reply to author
Forward
0 new messages