Quantcast
Channel: T. C. Mits 108 » java
Viewing all articles
Browse latest Browse all 36

Groovy Object Notation using ConfigSlurper

$
0
0

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:

[>configuration/finance]
    grails.webflow.stateless = true
    smtp {
        mail.host = 'smtp.myisp.com'
        mail.auth.user = 'server'
    }
    resources.URL = "http://localhost:80/resources"
[<configuration/finance]

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 {
  @Test
  public final void test() {		
  	def scriptString = new Inix('resources\\test-data.inix')
              .load("configuration/finance")

  	def config = new ConfigSlurper().parse(scriptString)
		
  	assert config.toString() == 
  	'[grails:[webflow:[stateless:true]], smtp:[mail:[host:smtp.myisp.com, 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.

Links

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

Viewing all articles
Browse latest Browse all 36

Trending Articles