Summary
Foregoing the use of JSON as a data interchange when Groovy language applications must interact internally or with other Groovy applications would be, well, groovy.
Introduction
JavaScript Object Notation (JSON) is considered a language-independent data interchange format. However, since it is based on a subset of the JavaScript (ECMA-262 3rd Edition) language, this is not fully correct. In fact, many languages must marshal to and from JSON using external libraries or extensions. This complicates applications since they must rely on more subsystems and there may be a performance penalty to parse or generate an external object notation.
If an application must only interact within a specific language or environment, such as the Java Virtual Machine (JVM), perhaps using the host language’s data structures and syntax will be a simpler approach. Since Groovy (a compiled dynamic language) has built-in script evaluation capabilities, high-level builders (for Domain Specific Language (DSL) creation) , and meta-programming capabilities, it should be possible to parse, create, transmit, or store data structures using the native Groovy data interchange format (GDIF), i.e., based on the native Groovy data structures.
Syntax example
Below is an example JSON data payload.
JSON (JavaScript) syntax:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
Below is the same data payload; this time using Groovy syntax. Note that there are not too many differences, the most striking is that maps are created using brackets instead of braces. It looks simpler too.
Groovy syntax:
[menu: [
id: "file",
value: "File",
popup: [
menuitem : [
[ value: "New", onclick: "CreateNewDoc()" ],
[ value: "Open", onclick: "OpenDoc()" ],
[ value: "Close", onclick: "CloseDoc()" ]
]
]
]]
Code Example
/**
* File: GrON.groovy
* Example class to show use of Groovy data interchange format.
* This is just to show use of Groovy data structure.
* Actual use of "evaluate()" can introduce a security risk.
* @sample
* @author Josef Betancourt
* @run groovy GrON.groovy
*
* Code below is sample only and is on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied.
* =================================================
*/
*/
class GrON {
static def message =
'''[menu:[id:"file", value:"File",
popup:[menuitem:[[value:"New", onclick:"CreateNewDoc()"],
[value:"Open", onclick:"OpenDoc()"], [value:"Close",
onclick:"CloseDoc()"]]]]]'''
/** script entry point */
static main(args) {
def gron = new GrON()
// dynamically create object using a String.
def payload = gron.slurp(this, message)
// manually create the same POGO.
def obj = [menu:
[ id: "file",
value: "File",
popup: [
menuitem : [
[ value: "New", onclick: "CreateNewDoc()" ],
[ value: "Open", onclick: "OpenDoc()" ],
[ value: "Close", onclick: "CloseDoc()" ]
]
]
]]
// they should have the same String representation.
assert(gron.burp(payload) == obj.toString())
}
/**
*
* @param object context
* @param data payload
* @return data object
*/
def slurp(object, data){
def code = "{->${data}}" // a closure
def received = new GroovyShell().evaluate(code)
if(object){
received.delegate=object
}
return received()
}
/**
*
* @param data the payload
* @return data object
*/
def slurp(data){
def code = "{->${data}}"
def received = new GroovyShell().evaluate(code)
return received()
}
/**
* @param an object
* @return it's string rep
*/
def burp(data){
return data ? data.toString() : ""
}
} // end class GrON
Possible IANA Considerations
MIME media type: application/gron.
Type name: application
Subtype name: gron
Encoding considerations: 8bit if UTF-8; binary if UTF-16 or UTF-32
Additional information:
Magic number(s): n/a
File extension: gron.
Macintosh file type code(s): TEXT
API
To be determined.
Security
Would GrON be a security hole? Yes, if it is implemented using a simple evaluation of the payload as if it were a script. The example shown above used evaluate() as an example of ingestion of a data object. For real world use, some kind of parser and generator for object graphs would be needed. The advantage would accrue if the underlying language parser could be reused for this.
Now this begs the question, if Groovy must now support a data parser, why not just use JSON with the existing libraries, like JSON-lib?
Is using the Java security system an alternative as one commenter mentioned?
Notes
The idea for GrON was formulated about a year ago. Delayed posting it since I wanted to create direct support for it. However, the task required more time and expertise then I have available at this time.
I was debating what to call it, if anything. Another name I considered was Groovy Data Interchange Format (GDIF), but I decided to follow the JSON name format by just changing the “J” to “G” and the “S” to “r” (emphasizing that Groovy is more then a Scripting language, its an extension of Java).
Updates
10Sept2011: See also this post: “JSON Configuration file format“.
9Feb2011: Looks like Groovy will get built in support for JSON: GEP 7 – JSON Support
I found (May 18, 2010, 11:53 PM) that I’m not the first to suggest this approach. See Groovy Interchange Format? by DeniseH.
Recently (Oct 3, 2010) found this blog post:
Groovy Object Notation ? GrON?
Mar 22, 2011: Groovy 1.8 will have JSON support built in.
Further Reading
- “All markup ends up looking like XML“, http://quoderat.megginson.com/2007/01/03/all-markup-ends-up-looking-like-xml/
- Groovy, http://groovy.codehaus.org/
- Groovy, http://en.wikipedia.org/wiki/Groovy_(programming_language)
- JSON, http://json.org/
- JSON, http://en.wikipedia.org/wiki/JSON
- Groovy JSON Builder parser, Google search
- RFC 4627, RFC 4627 application/json.
- “Evaluating code dynamically in Groovy (differences with Ruby)”, http://jorgemanrubia.net/2009/10/10/evaluating-code-dynamically-in-groovy/
- JSON Example, http://www.json.org/example.html
- JSON-lib Snippets, http://json-lib.sourceforge.net/snippets.html#Using%20the%20JsonGroovyBuilder
- “JSON and other data serialization languages”, http://www.simple-talk.com/content/print.aspx?article=534
- “Groovy Interchange Format?”, DeniseH, http://tinyurl.com/22qgae2
- “Distributed Extensibility”, http://bitworking.org/news/2010/06/distributed-extensibility
Groovy Object Notation ? GrON?

Groovy Object Notation (GrON) for Data Interchange
by Josef Betancourt is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Based on a work at wp.me.
Posted by josefB 