JSON block definitions!! when did that happen???

826 views
Skip to first unread message

phil cleaver

unread,
Jun 18, 2015, 4:54:40 AM6/18/15
to blo...@googlegroups.com
I've just been making some blocks in the block factory and see that there is a dropdown next to the block definition code that lets me see the definition in JavaScript or in JSON format.

I've never come across JSON format blocks before and couldn't see anything in the docs.  How do we use it?  Does this also mean I can use JSON instead of XML for the workspace too?

Phil

Neil Fraser

unread,
Jun 18, 2015, 12:06:18 PM6/18/15
to blo...@googlegroups.com

Shh....

Send (awkwardly) from an Android phone.

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

Neil Fraser

unread,
Jun 29, 2015, 2:57:45 PM6/29/15
to blo...@googlegroups.com
On 18 June 2015 at 01:54, phil cleaver <phil_c...@hotmail.com> wrote:
I've just been making some blocks in the block factory and see that there is a dropdown next to the block definition code that lets me see the definition in JavaScript or in JSON format.

I've never come across JSON format blocks before and couldn't see anything in the docs.  How do we use it?  Does this also mean I can use JSON instead of XML for the workspace too?

The advantages of using JSON over the regular JavaScript commands are:
1) Easier and more flexible translation of blocks into other languages.
2) Better portability of non-dynamic blocks to non-web versions of Blockly.

Feedback on this format is welcome.  There is still time to change it, but it will become fixed soon.

[Note that the old (undocumented) interpolateMsg init function has been removed in favour of the more powerful JSON format.]
--

Carlos Pereira

unread,
Jun 29, 2015, 4:30:12 PM6/29/15
to blo...@googlegroups.com, ro...@neil.fraser.name
>> [Note that the old (undocumented) interpolateMsg init function has been removed in favour of the more powerful JSON format.]

Indeed it has, perhaps it's a good idea to throw an exception with a log message to indicate the new JSON implementation. Otherwise new blocks just crash with a "TypeError not a function".

Andrew Stratton

unread,
Jul 1, 2015, 5:36:59 AM7/1/15
to blo...@googlegroups.com, ro...@neil.fraser.name
Possibly this is heading off topic - but could the generator be added tot he block definition using json as well. This is something I have been doing for a museums project (which is just ending for me) - and some examples might help to see how this works. I don't expect Blockly to change to this format - but perhaps some of the concepts would be useful:

The blocks look like this

And here's the JSON based definition (actually it's javascript - but most of it's JSON :) ) for the Say block:

        var ID_GREETING = "Greeting";
        defineAction
({
            name
: 'Say',
           
interface: [ {name:ID_GREETING, title: '', text:'...'} ],
            javascript
: function (block) {
               
return 'console.log("' + getText(block, ID_GREETING) + '");\n';
           
}
       
});


And the Every Block:

        var DURATION = 'DURATION';
        defineRule
({
            name
: 'Every', next:false, previous:false,
           
interface: [
               
{name:DURATION, title:'', number:1},
               
{menu:['Seconds','Minutes','Hours'], name:UNITS_MENU, title:''},
               
{statement:STATEMENT}
           
],
            javascript
: function(block) {
               
var duration = getNumber(block, DURATION);
               
switch (getMenu(block, UNITS_MENU)) {
                   
case 'Minutes':
                        duration
*= 60;
                       
break;
                   
case 'Hours':
                        duration
*= 60*60;
                       
break;
               
};
               
var statement = getStatement(block, STATEMENT);
               
var result = "quando.every("
                   
+ duration
                   
+ ", function() {\n"
                   
+ statement
                   
+ "});\n";
               
return result;
           
}
       
});




Here's the generated code (for reference), where quando is my runtime library to simplify event handling on node js/browser:

quando = require("./quando.js");
quando
.every(1, function() {
 console
.log("hi");
});

Hope this helps - let me know if you would like any more information.

Best wishes
  Andy Stratton

toe...@extremenetworks.com

unread,
Jul 2, 2015, 4:02:57 PM7/2/15
to blo...@googlegroups.com, ro...@neil.fraser.name
I really like what you have done with the new jsonInit function.  One request to add a case to Blockly.Block.prototype.interpolate_ for those cases where the template doesn't quite do it:

          case 'field':
            field = element['field'];
            break;

This allows you to use it many more cases, particularly when you have to modify the created field.  You can see an example of using it in our current version of procedures at:
   https://github.com/toebes-extreme/blockly/blob/master/blocks/procedures.js (lines 124-160).

-- John

Neil Fraser

unread,
Jul 2, 2015, 7:23:06 PM7/2/15
to blo...@googlegroups.com
Interesting.  This proposal does have the disadvantage of the input no longer being JSON.  That means that the code is no longer compatible cross-platform (for (currently non-existent) non-JS ports of Blockly).

I encountered a similar issue with a few blocks and have just made Block.getField public (previously it was private).  Check out:
  https://github.com/google/blockly/blob/master/blocks/loops.js#L60
Here we use JSON to define the static aspects of he field, then use JS to add the dynamic stuff.  Would this be a valid solution for you?

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

toe...@extremenetworks.com

unread,
Jul 6, 2015, 7:15:04 AM7/6/15
to blo...@googlegroups.com, ro...@neil.fraser.name
It is a good idea, and it works in this case, but it intruduces a problem in that if you name a field, it also gets output into the XML.  We have addressed that partially by adding a SERIALIZABLE bit to blocks to handle that case, but that isn't in your main code base (https://github.com/toebes-extreme/blockly/blob/master/core/field.js line 89).  Another option would be to enable more attributes of the block definitions in the JSON such as "spellcheck: false".

Thank you!
  -- John

toe...@extremenetworks.com

unread,
Jul 6, 2015, 2:25:56 PM7/6/15
to blo...@googlegroups.com, ro...@neil.fraser.name
I've gone ahead and updated my code to take advantage of the new getField() functionality and eliminate the kludge for "field" to be in line with what you suggest.  I still have to have the SERIALIZABLE flag to prevent these fields from getting into the generated DOM however.  I am open to other suggestions there...
  -- John

Neil Fraser

unread,
Jul 11, 2015, 4:09:46 AM7/11/15
to blo...@googlegroups.com
Your spellcheck property for field_textinput is good.  Added.
(Documentation will update next week.)

Carlos Pereira

unread,
Jul 14, 2015, 7:55:13 PM7/14/15
to blo...@googlegroups.com, ro...@neil.fraser.name
So Neil, now that we have JSON blocks, does that mean we could expected a JSON toolbox as well?

Neil Fraser

unread,
Jul 14, 2015, 7:57:56 PM7/14/15
to blo...@googlegroups.com
On 14 July 2015 at 16:55, Carlos Pereira <carlos...@gmail.com> wrote:
So Neil, now that we have JSON blocks, does that mean we could expected a JSON toolbox as well?

Not planning on it.  Is there a problem with XML?

Carlos P.A.

unread,
Jul 14, 2015, 8:00:58 PM7/14/15
to blo...@googlegroups.com
Not really a problem, but I was thinking about changing blocks and categories in the toolbox dynamically, and editing a JSON object is nicer than an XML DOM.

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/pMsZS3P7c0c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages