blockly json serialization and merging

494 views
Skip to first unread message

Koen Van Wijk

unread,
Jan 6, 2022, 5:51:42 PM1/6/22
to Blockly
Hi, 

I know quite some thoughts went into the json format. I read the document. I am referring to the Sequential Stack Format. With the current json merging of the json file breaks quite fast if any reordering is done. Did anyone consider or even make a seperate json serialization that keeps a sorted list of blocks with references to other blocks using their ids? 

Regards,
Koen
 

Beka Westberg

unread,
Jan 6, 2022, 7:13:04 PM1/6/22
to blo...@googlegroups.com
Hello! Thanks for giving that document a look :D I really appreciate your interest in the new serializer. Sadly I don't think we considered any alternatives that aren't documented there :/

However, if you just want to serialize individual blocks and not the whole workspace (so that you can keep your own sorted list), the Blockly.serialization.blocks.save function is public and allows you to do that =)

Best wishes,
--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/1106633a-b37a-44b7-93eb-4f14b0767fb9n%40googlegroups.com.

Koen Van Wijk

unread,
Jan 7, 2022, 6:16:10 PM1/7/22
to Blockly
The code is indeed kind of easy... For anyone who like to save a flat file...

Regards,
Koen


function remove_blocks(obj)
{
        var properties = Object.getOwnPropertyNames(obj)
        for (var j=0; j<properties.length;j++)
        {
        if (properties[j]=='block')
        {
                        // remove the block but keep the id
                        var id = obj['block'].id
                        delete obj['block']
                        obj.block = { "id":id}
        } else if (typeof(obj[properties[j]])=='object')
        {
                remove_blocks(obj[properties[j]])
        }
        }
}

function inject_blocks(obj, saved_blocks)
{
    var properties = Object.getOwnPropertyNames(obj)
    for (var j=0; j<properties.length;j++)
          {
        if (properties[j]=='block')
        {
                obj.block = saved_blocks[obj.block.id]
        } else if (typeof(obj[properties[j]])=='object')
        {
                inject_blocks(obj[properties[j]], saved_blocks)
        }
          }
}

function save_mergeable(workspace)
{
    var blocks = workspace.getAllBlocks();
    var save_blocks = {};
    for (var i=0; i<blocks.length;i++)
    {
            var json_obj = Blockly.serialization.blocks.save(blocks[i], {addCoordinates: true,
                                                      addInputBlocks: true,
                                                      addNextBlocks: true,
                                                      doFullSerialization: true})

        remove_blocks(json_obj)
        save_blocks[blocks[i].id] = json_obj

    }
    save_blocks['top_blocks'] = workspace.getTopBlocks().map(block => block.id);
   

    return save_blocks
}

function load_mergeable(saved_blocks, workspace)
{
        var keys = Object.keys(saved_blocks)
        for (var i=0; i<keys.length;i++)
        {
        inject_blocks(saved_blocks[keys[i]],saved_blocks)
        }
        workspace.clear()
        for (var i=0; i<saved_blocks['top_blocks'].length;i++)
        {
                var id = saved_blocks['top_blocks'][i]
                Blockly.serialization.blocks.append(saved_blocks[id], workspace)

Beka Westberg

unread,
Jan 8, 2022, 10:24:55 AM1/8/22
to blo...@googlegroups.com
Thanks for posting the code you ended up using! It's always nice for future people to be able to come back to an old post and get a full solution =)

Best wishes,
--Beka

Reply all
Reply to author
Forward
0 new messages