Skip to content

Instantly share code, notes, and snippets.

@danveloper
Created July 26, 2013 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danveloper/6086864 to your computer and use it in GitHub Desktop.
Save danveloper/6086864 to your computer and use it in GitHub Desktop.
This is a mixin class that is resolvable with the package location and class naming convention resolution strategy.
package groovy.runtime.metaclass.java.lang
import groovy.json.JsonSlurper
class StringMixins {
// Must be static & must take an instance of the receiver as the first vararg
static def toJson(String input) {
new JsonSlurper().parseText(input)
}
}
package groovy.runtime.metaclass.java.lang
import com.objectpartners.groovy.ext.CustomMetaClassCreationHandle
import org.junit.BeforeClass
import org.junit.Test
class StringMixinsTest {
@BeforeClass
static void init() {
// We need to tell the GroovySystem to use our CustomMetaClassCreationHandle
GroovySystem.getMetaClassRegistry().setMetaClassCreationHandle(new CustomMetaClassCreationHandle())
}
@Test
void testToJson() {
def json = '{ "id": "1", "name": "Dan Woods", "twitter": "@danveloper" }'.toJson()
assert json instanceof Map
assert json.id == "1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment