Skip to content

Instantly share code, notes, and snippets.

@yutax77
Created June 13, 2011 13:52
Show Gist options
  • Save yutax77/1022797 to your computer and use it in GitHub Desktop.
Save yutax77/1022797 to your computer and use it in GitHub Desktop.
Collectionメンバー変数もJSON含めてシリアライズする
import java.util.List;
import flexjson.JSONSerializer;
public class Human {
private String name;
private List<String> hobbies; //このフィールドをJSONに含めたい
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public String toJson(){
return new JSONSerializer().exclude("*.class")
.include("hobbies")//明示的にincludeする!
.serialize(this);
}
}
import java.util.List;
import flexjson.JSON;
import flexjson.JSONSerializer;
public class Human2 {
private String name;
private List<String> hobbies; //このフィールドをJSONに含めたい
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JSON //アノテーションを使えば、include()は不要
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public String toJson(){
return new JSONSerializer().exclude("*.class")
.serialize(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment