Generate Nested JSON with Blockly

924 views
Skip to first unread message

br...@sltwtr.com

unread,
Feb 13, 2018, 3:54:21 PM2/13/18
to Blockly
Has anyone used Blockly to generate a nested JSON object?

I looked at the code for if/else and thing it can be modified into a node object:

Blockly.JavaScript['controls_if'] = function(block) {
  // If/elseif/else condition.
  var n = 0;
  var code = '', branchCode, conditionCode;
  do {
    conditionCode = Blockly.JavaScript.valueToCode(block, 'IF' + n,
      Blockly.JavaScript.ORDER_NONE) || 'false';
    branchCode = Blockly.JavaScript.statementToCode(block, 'DO' + n);
    code += (n > 0 ? ' else ' : '') +
        'if (' + conditionCode + ') {\n' + branchCode + '}';

    ++n;
  } while (block.getInput('IF' + n));

  if (block.getInput('ELSE')) {
    branchCode = Blockly.JavaScript.statementToCode(block, 'ELSE');
    code += ' else {\n' + branchCode + '}';
  }
  return code + '\n';
};



Then I would like to be able to insert preconfigured JSON nodes as new types of blocks.

Andrew n marshall

unread,
Feb 14, 2018, 4:07:15 AM2/14/18
to blo...@googlegroups.com
I haven't tried this, but it's certainly possible. We already allow nested arrays through the list blocks.

The controls_if block is a good basis for creating a block for a JSON object. Not only does it have a variable length, with the mutator to manage the length, but it also has a parameter (boolean, in the if case) for each input. In the JSON object case, these would be string and number keys.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages