Migration from xml to json

517 views
Skip to first unread message

Cong Huan Nguyen

unread,
Dec 23, 2022, 6:33:10 AM12/23/22
to Blockly
Hello guys,
we recently updated to blockly 9.1.1 and we used to save our workspaces/blocks in xml format.
Now, we want to change our database from xml format to json format. Is there any way convert the existing xml code to json format?
I'd use a script which runs through our databases but I could not find any function which converts xml to json in the blockly documentation.
Did someone went through this migration?

Thanks and kind regards
Nguyen

Beka Westberg

unread,
Jan 3, 2023, 12:17:36 PM1/3/23
to blo...@googlegroups.com
Hello!

You can convert XML to JSON by running it through a headless workspace.

Eg:
```
const workspace = new Blockly.Workspace();
Blockly.Xml.domToWorkspace(getMyXml(), workspace);
const json = Blockly.serialization.workspaces.save(workspace);
storeMyJson(json);
```

I hope that helps! If you have any further questions please reply =)
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/1508357d-2ab4-4bd2-9640-d6d58bef3b4an%40googlegroups.com.

Dummy PG

unread,
Nov 20, 2023, 4:49:40 PM11/20/23
to Blockly
@OP/@Beka,

Is JSON faster than XML in terms of de/serialization?  I wonder if JSON takes less overhead than XML?
Reason for asking is that we currently use XML and its quite slow to load xml -> textToDom -> domToWorkspace
Takes about ~15s to load ~4K blocks, this is on i7/3060 gaming laptop btw.


ewpa...@gmail.com

unread,
Nov 21, 2023, 2:12:57 PM11/21/23
to Blockly
App Inventor is also interested in this process and I expect we will collect some data before/after to see how it goes. My understanding at least is that DOM interactions are fairly slow whereas manipulating JSON objects is pretty well optimized. The JSON payload should also in theory be smaller due to not having to repeat the closing tags, so there is less data to download and process as a result.

Hopefully that helps.

Cheers,
Evan

Christopher Allen

unread,
Nov 22, 2023, 3:01:34 PM11/22/23
to blo...@googlegroups.com
 Hello,

Is JSON faster than XML in terms of de/serialization?  I wonder if JSON takes less overhead than XML?
Reason for asking is that we currently use XML and its quite slow to load xml -> textToDom -> domToWorkspace
Takes about ~15s to load ~4K blocks, this is on i7/3060 gaming laptop btw.

I'm not sure we've actually benchmarked this (and the colleague who would know is on holiday at the moment), and of course it's going to be highly dependent on the speed of the implementation of the underlying JS APIs (which will vary from platform to platform) but I would be surprised if the JSON implementation was not at least somewhat faster than XML in most cases.

App Inventor is also interested in this process and I expect we will collect some data before/after to see how it goes.

I hope you will share this; we'd be most interested as I am sure would many other developers.
 
My understanding at least is that DOM interactions are fairly slow whereas manipulating JSON objects is pretty well optimized. The JSON payload should also in theory be smaller due to not having to repeat the closing tags, so there is less data to download and process as a result.

It is also worth considering that what we describe as the JSON serializer is actually only a converter that encodes Blockly data in the form of Plain Old JavaScript Objects in a way that is compatible with JSON.serialize, so if  ultimate performance is the goal you may be able to get faster (de)serialisation—and smaller data size—by substituting a different object-to-bytestream serializer, such as MessagePack, for JSON.serialize / .parse.


Best wishes,

Christopehr

Reply all
Reply to author
Forward
0 new messages