Skip to content

Instantly share code, notes, and snippets.

@rburgosnavas
Last active August 29, 2015 13:57
Show Gist options
  • Save rburgosnavas/70099c8318f62c607dc1 to your computer and use it in GitHub Desktop.
Save rburgosnavas/70099c8318f62c607dc1 to your computer and use it in GitHub Desktop.
/**
* Testing Jackson with Groovy
**/
import org.codehaus.jackson.map.*
// sample JSON
json = """{
"name": {
"first": "Morgoth",
"last": "Melkor"
},
"job": "evil doer"
}"""
// name class:
// Groovy makes fields private and creates getters/setters automatically
class Name {
String first, last
}
// person class
class Person {
Name name
String job
}
// testing Person and Name
p1 = new Person()
p1.name = new Name( first: "bill", last: "ward" )
p1.job = "drummer"
// create an Jackson object mapper...
om = new ObjectMapper()
// create a new Person from JSON string
p2 = om.readValue(json, Person.class)
println(p2.job)
// write a JSON file
om.writeValue(new File("bill.json"), p1)
// load bill.json and open a stream
billJson = new File("bill.json")
fis = new FileInputStream(billJson)
// print JSON file's path
println(billJson.getAbsolutePath())
// iterate through stuff in the stream and print to console
for (j in fis) print((char)j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment