Created
July 26, 2013 07:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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