Ken Webb 2010-05-04T18:58:18Z
JavaScript Object Notation (JSON) is "a lightweight data-interchange format". JSON is an increasingly popular format for representing structured (hierarchical) data.
Xholon can read JSON-formatted files, in certain circumstances, as an alternative to XML-formatted files. It uses the Jettison JSON StAX library to parse the JSON-formatted text.
As an example, the Swing GUI in the Xholon MeTTTa application, can be expressed declaratively in JSON as follows.
{
JFrame:{
Title:'Xholon meTTTa',
DefaultCloseOperation:3,
JPanel:{
JPanel:{
Background:'0xF0F0F0',
GridLayout:{
Rows:3,
Columns:3
},
JButton:[
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select0',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select1',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select2',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select3',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select4',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select5',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select6',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select7',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
},
{
MinimumSize:'Dimension,50,50',
PreferredSize:'Dimension,50,50',
ActionCommand:'select8',
SetFont:'Monospaced,1,24',
Background:'0xEEF8E0'
}
]
},
VBox:{
JButton:[
{
Text:'New Game',
ActionCommand:'newgame'
},
{
Enabled:false
},
{
Text:'Computer First',
ActionCommand:'computerfirst'
}
]
}
}
}
}
The same Swing GUI can be expressed much more concisely. This depends on using the Java class SwingButtonStylist2, a Xholon stylist script that initializes all nine JButton nodes that are children of the JPanel node.
{
JFrame:{Title:'Xholon meTTTa',DefaultCloseOperation:3,
JPanel:{
JPanel:{Background:'0xF0F0F0',
GridLayout:{Rows:3,Columns:3},
JButton:[{},{},{},{},{},{},{},{},{}],
SwingButtonStylist2:{}
},
VBox:{
JButton:[
{Text:'New Game',ActionCommand:'newgame'},
{Enabled:false},
{Text:'Computer First',ActionCommand:'computerfirst'}
]
}
}
}
}
Jettison supports the convention that attributes start with the @ symbol. So the following is probably the best way to represent the example GUI. It's also the most compatible with the original XML-formatted file.
{
JFrame:{@Title:'Xholon meTTTa',@DefaultCloseOperation:'3', // 2 3
JPanel:{
JPanel:{@Background:'0xF0F0F0',
GridLayout:{@Rows:'3',@Columns:'3'},
JButton:{@multiplicity:'9'},
SwingStylist:{
@implName:'org.primordion.user.app.MeTTTa.SwingButtonStylist2'}
},
VBox:{
JButton:[
{@Text:'New Game',@ActionCommand:'newgame'},
{@Enabled:'false'},
{@Text:'Computer First',@ActionCommand:'computerfirst'}
]
}
}
}
}
The XML equivalent is:
<?xml version="1.0" encoding="UTF-8"?>
<JFrame Title="Xholon meTTTa" DefaultCloseOperation="3"> <!-- 2 3 -->
<JPanel>
<JPanel Background="0xF0F0F0">
<GridLayout Rows="3" Columns="3"/>
<JButton multiplicity="9"/>
<SwingStylist
implName="org.primordion.user.app.MeTTTa.SwingButtonStylist2"/>
</JPanel>
<VBox>
<JButton Text="New Game" ActionCommand="newgame"/>
<Strut Height="10"/>
<JButton Text="Computer First" ActionCommand="computerfirst"/>
</VBox>
</JPanel>
</JFrame>
All three of the above JSON versions (and the XML version) have been successfully tested with Xholon, as alternatives to the XML-formatted files.
The main composite structure hierarchy file (MeTTTa_CompositeStructureHierarchy.xml) for the MeTTTa application, allows for the choice of different versions of either XML or JSON.
<?xml version="1.0" encoding="UTF-8"?>
<!--
MeTTTa application - Composite Structure Hierarchy
Xholon 0.7.1 http://www.primordion.com/Xholon
-->
<TheSystem xmlns:xi="http://www.w3.org/2001/XInclude">
<AppClientMVC>
<AppClientMVC multiplicity="1">
<MeTTTaModel/>
<MeTTTaView>
<!-- meTTTa GUI -->
<!--<xi:include href="./config/user/MeTTTa/MeTTTa_JFrame_Original.xml"/>-->
<!--<xi:include href="./config/user/MeTTTa/MeTTTa_JFrame_New.xml"/>-->
<!--<xi:include href="./config/user/MeTTTa/MeTTTa_JFrame_WithStylist.xml"/>-->
<!--<xi:include href="./config/user/MeTTTa/MeTTTa_JFrame_Tiny.xml"/>-->
<!--<xi:include href="./config/user/MeTTTa/MeTTTa_JFrame_JSON1.json"/>-->
<xi:include href="./config/user/MeTTTa/MeTTTa_JFrame_JSON2.json"/>
</MeTTTaView>
<MeTTTaController/>
</AppClientMVC>
<MeTTTaWorker/>
<MeTTTaBusinessDelegate/>
</AppClientMVC>
</TheSystem>