Created
August 28, 2012 15:49
-
-
Save toby55kij/3499330 to your computer and use it in GitHub Desktop.
POJO<->JSONの相互変換にGroovyを使った場合のサンプル
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
import groovy.lang.Binding; | |
import groovy.lang.GroovyShell; | |
import groovy.lang.Script; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* POJO <-> JSON Sample Using Groovy | |
*/ | |
public class GroovyPojoJsonSample { | |
/** | |
* Name class | |
*/ | |
public static class Name { | |
private String firstName; | |
private String lastName; | |
public Name() { | |
} | |
public Name(String firstName, String lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String toString() { | |
return lastName + " " + firstName; | |
} | |
} | |
/** | |
* Profile class | |
*/ | |
public static class Profile { | |
private Name name; | |
private int age; | |
public Profile() { | |
super(); | |
} | |
public Profile(Name name, int age) { | |
this(); | |
this.name = name; | |
this.age = age; | |
} | |
public Name getName() { | |
return name; | |
} | |
public void setName(Name name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String toString() { | |
return "name: " + name.toString() + ", age: " + age; | |
} | |
} | |
/** | |
* SampleData class | |
*/ | |
public static class SampleData { | |
private List<Profile> profiles; | |
public SampleData() { | |
profiles = new ArrayList<>(); | |
} | |
public SampleData(List<Profile> profiles) { | |
this.profiles = profiles; | |
} | |
public List<Profile> getProfiles() { | |
return profiles; | |
} | |
public void setProfiles(List<Profile> profiles) { | |
this.profiles = profiles; | |
} | |
public String toString() { | |
StringBuilder builder = new StringBuilder(); | |
for (Profile profile : profiles) { | |
builder.append(builder.length() > 0 ? "," : "profiles:"); | |
builder.append(" [").append(profile).append("]"); | |
} | |
return builder.toString(); | |
} | |
} | |
/** | |
* Category Class for Groovy asType() | |
*/ | |
public static class SampleDataCategory { | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
public static Object asType(Map self, Class clazz) { | |
if (clazz == SampleData.class) { | |
SampleData result = new SampleData(); | |
List<Map> profiles = (List<Map>)self.get("profiles"); | |
for (Map profile : profiles) { | |
result.getProfiles().add((Profile)asType(profile, Profile.class)); | |
} | |
return result; | |
} | |
if (clazz == Profile.class) { | |
Map name = (Map)self.get("name"); | |
return new Profile((Name)asType(name, Name.class) , (int)self.get("age")); | |
} | |
if (clazz == Name.class) { | |
return new Name((String)self.get("firstName"), (String)self.get("lastName")); | |
} | |
return self; | |
} | |
} | |
/** | |
* main | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
GroovyPojoJsonSample sample = new GroovyPojoJsonSample(); | |
sample.init(); | |
sample.convertPojoToJson(); | |
sample.convertJsonToPojo(); | |
} | |
/** | |
* GroovyShell | |
*/ | |
private GroovyShell shell = null; | |
/** | |
* initialize GroovyShell | |
*/ | |
public void init() { | |
shell = new GroovyShell(getClass().getClassLoader(), new Binding()); | |
} | |
/** | |
* POJO -> JSON | |
*/ | |
public void convertPojoToJson() { | |
List<Profile> profiles = new ArrayList<>(); | |
profiles.add(new Profile(new Name("Yasuharu", "Hayami"), 40)); | |
profiles.add(new Profile(new Name("康晴", "速水"), 40)); | |
SampleData data = new SampleData(profiles); | |
File file = new File("profile.json"); | |
Script script = shell.parse("use(groovy.json.JsonOutput){file.text = data.toJson()}"); | |
Binding binding = new Binding(); | |
binding.setVariable("data", data); | |
binding.setVariable("file", file); | |
script.setBinding(binding); | |
script.run(); | |
} | |
/** | |
* JSON -> POJO | |
*/ | |
public void convertJsonToPojo() { | |
File file = new File("profile.json"); | |
String s = "use(" + SampleDataCategory.class.getName() + | |
"){new groovy.json.JsonSlurper().parseText(file.text) as " + | |
SampleData.class.getName() + "}"; | |
Script script = shell.parse(s); | |
Binding binding = new Binding(); | |
binding.setVariable("file", file); | |
script.setBinding(binding); | |
Object result = script.run(); | |
System.out.println(result.getClass().getName() + ", " + result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment