Groovy Object Notation using ConfigSlurper

In an old post in 2010 I was exploring the idea of using Groovy syntax as a data interchange format, GrON, just as JSON is a “subset” of Javascript.

Just noticed that Groovy has, since version 1.5, the ConfigSlurper class. “ConfigSlurper is a utility class for reading configuration files defined in the form of Groovy scripts. Configuration settings can be defined using dot notation or scoped using closures.”

Example use of ConfigSlurper

We can store a configuration found on the ConfigSlurper API page into a section of an Inix file:

    grails.webflow.stateless = true
    smtp { = ''
        mail.auth.user = 'server'
    resources.URL = "http://localhost:80/resources"

Then to read it we just load that section and parse with a ConfigSlurper. As shown below.

import static org.junit.Assert.*;
import org.junit.Test;
import com.octodecillion.util.inix.Inix

class ExampleSlurpInix {
  public final void test() {		
  	def scriptString = new Inix('resources\\test-data.inix')

  	def config = new ConfigSlurper().parse(scriptString)
  	assert config.toString() == 
  	'[grails:[webflow:[stateless:true]], smtp:[mail:[, auth:[user:server]]], resources:[URL:http://localhost:80/resources]]'

That’s one of the application types I was envisioning GrON to be used for: An easier format to store and transfer configuration, test, and other types of data. The issue is that as noted in the original post, this is insecure for use as data interchange unless a GrON parser that does not execute Groovy is used.


Similar Posts:

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

One thought on “Groovy Object Notation using ConfigSlurper”

Leave a Reply

Your email address will not be published. Required fields are marked *