Google��Gson�̃����B
|
Gson�́AGoogle��JSON���C�u�����[�B
Java�̃I�u�W�F�N�g��JSON�`���ŕۑ��E�����i�V���A���C�Y�E�f�V���A���C�Y�Ɠ��l�j����̂ɕ֗��B
�K�v�ȃ��C�u�����[��Gson��jar�t�@�C���̂݁i�o�[�W����2.2.2�̏ꍇ��gson-2.2.2.jar�j�ŁA���Ɉˑ�����jar�t�@�C���������̂����_�B
ExampleBean�N���X��ۑ��E���������B
public class ExampleBean { private String text; private int value; public ExampleBean(String s, int n) { this.text = s; this.value = n; } @Override public String toString() { return String.format("Example(%s, %d)", text, value); } }
Gson�ł́Aprivate�t�B�[���h�������I�ɕۑ����Ă����B�i�Z�b�^�[�E�Q�b�^�[���\�b�h�͕s�v�j
import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter;
�ۑ� | ���� |
---|---|
void write(File file) throws IOException { ExampleBean bean = new ExampleBean("text1", 123); try (JsonWriter writer = new JsonWriter(new FileWriter(file))) { writer.setIndent(" "); Gson gson = new Gson(); gson.toJson(bean, ExampleBean.class, writer); } } |
void read(File file) throws IOException { ExampleBean bean; try (JsonReader reader = new JsonReader(new FileReader(file))) { Gson gson = new Gson(); bean = gson.fromJson(reader, ExampleBean.class); } System.out.println(bean); } |
setIndent()���w�肵�Ȃ��ƁA���s�����ň�s��JSON�Ƃ��ďo�͂����B
{"text":"text1","value":123}
setIndent()�ŃC���f���g���w�肷��ƁA�ȉ��̗l�ɉ��s����A�e�s�̍s���ɃC���f���g���t����ꂽ��Ԃŏo�͂����B
{ "text": "text1", "value": 123 }
�ۑ��������N���X�̃t�B�[���h��SerializedName�A�m�e�[�V������t����ƁA���̖��O��JSON�������B
import com.google.gson.annotations.SerializedName;
public class ExampleBean { @SerializedName("string") private String text; @SerializedName("integer") private int value; �` }
��
{ "string": "text1", "integer": 123 }
�W����Serializable�ł�transient��t�����t�B�[���h�̓V���A���C�Y����Ȃ��i�ۑ�����Ȃ��j�B
Gson�ł́AExpose�A�m�e�[�V������t�����t�B�[���h�����V���A���C�Y�Ώۂɂ���Ƃ��������o����B
import com.google.gson.annotations.Expose;
public class ExampleBean { @Expose private String text; //�ۑ����� private int value; //�ۑ����Ȃ� �` }
Expose�A�m�e�[�V������L���ɂ���ɂ́AGson�I�u�W�F�N�g�����ۂ�excludeFieldsWithoutExposeAnnotation���w�肷��K�v������B
import com.google.gson.GsonBuilder;
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); gson.toJson(bean, ExampleBean.class, writer);
��
{ "text": "text1" }
JSON�ɂ������I�u�W�F�N�g���t�B�[���h�ɕʂ̃I�u�W�F�N�g��ێ����Ă���ꍇ�A���̃I�u�W�F�N�g��JSON�ɕϊ������B
�ijava.util.List�͔z��Ƃ��Ĉ�����j
���̍ہA�Q�Ƃ��z���Ă���Ɩ������[�v�ɂȂ�A�X�^�b�N�I�[�o�[�t���[�ɂȂ�B
�Q�Ƃ�JSON�ɏo�͂���K�v�������Ȃ�AExpose���g���ďo�͗L���𐧌䂷����@���g����B
Gson�I�u�W�F�N�g���g�킸�ɁA����JsonWriter/JsonReader�Ŗ��O��l���w�肵�ĕۑ��E�������邱�Ƃ��o����B
import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter;
�ۑ� | ���� |
---|---|
void write(File file) throws IOException { ExampleBean bean = new ExampleBean("text1", 123); try (JsonWriter writer = new JsonWriter(new FileWriter(file))) { writer.setIndent(" "); writer.beginObject(); writer.name("text").value(bean.getText()); writer.name("value").value(bean.getValue()); writer.endObject(); } } |
void read(File file) throws IOException { ExampleBean bean; try (JsonReader reader = new JsonReader(new FileReader(file))) { String text = null; int value = 0; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "text": text = reader.nextString(); break; case "value": value = reader.nextInt(); break; default: reader.skipValue(); break; } } reader.endObject(); bean = new ExampleBean(text, value); } System.out.println(bean); } |
beginObject()�EendObject()�ŃI�u�W�F�N�g�̊J�n�E�I���������B
�ǂݍ��ލۂ́AbeginObject()�̌��hasNext()���Ăяo�����ƂŁA�I�u�W�F�N�g�̏I��肪����܂Ń��[�v�����邱�Ƃ��o����B
�f�[�^�̕��я�������Ă��Ă��Ή��ł���悤�ɂ��悤�Ǝv������A���[�v���Ė��O�Ŕ��肷��̂��ǂ��̂��낤�B
�z��O�̖��O�������ꍇ��skipValue()�Œl���X�L�b�v����B�l���X�L�b�v���Ȃ��ƁA�㑱�����Ŗ��O���~�����̂ɒl������̂ŃG���[�ɂȂ�B
�z���ۑ��E���������B
beginArray()�EendArray()�Ŕz��̊J�n�E�I���������B
�ǂݍ��ލۂ́AbeginArray()�̌��hasNext()���Ăяo�����ƂŁA�z��̏I��肪����܂Ń��[�v�����邱�Ƃ��o����B
�ۑ� | ���� |
---|---|
void write(File file) throws IOException { try (JsonWriter writer = new JsonWriter(new FileWriter(file))) { writer.setIndent(" "); writer.beginObject(); writer.name("beans"); writer.beginArray(); for (int i = 1; i <= 3; i++) { writer.beginObject(); writer.name("text").value("text" + i); writer.name("value").value(100 + i); writer.endObject(); } writer.endArray(); writer.endObject(); } } |
void read(File file) throws IOException { try (JsonReader reader = new JsonReader(new FileReader(file))) { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "beans": reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "text": System.out.println(reader.nextString()); break; case "value": System.out.println(reader.nextInt()); break; default: reader.skipValue(); break; } } reader.endObject(); } reader.endArray(); break; default: reader.skipValue(); break; } } reader.endObject(); } } |
��
{ "beans": [ { "text": "text1", "value": 101 }, { "text": "text2", "value": 102 }, { "text": "text3", "value": 103 } ] }
�F�X�Ȏ�ނ̎q�N���X�������Ă��郊�X�g��ۑ��E���������B[2013-03-20]
�Q�l�F stackoverflow��Polymorphism with gson
MyClass0�Ƃ����e�N���X�������āA���̎q�N���X�Ƃ���MyClass1��MyClass2��������̂Ƃ���B
������ێ�����MyRoot�Ƃ����N���X��ۑ��E�������Ă݂�B
public class MyClass0 { public String name; }
public class MyClass1 extends MyClass0 { public String value1; @Override public String toString() { return String.format("MyClass1(%s, %s)", name, value1); } }
public class MyClass2 extends MyClass0 { public String value2; @Override public String toString() { return String.format("MyClass2(%s, %s)", name, value2); } }
public class MyRoot { private List<MyClass0> list = new ArrayList<>(); public void add1(String name, String value) { MyClass1 m = new MyClass1(); m.name = name; m.value1 = value; list.add(m); } public void add2(String name, String value) { MyClass2 m = new MyClass2(); m.name = name; m.value2 = value; list.add(m); } @Override public String toString() { return String.format("MyRoot%s", list); } }
import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter;
public void write(File file) throws IOException { MyRoot root = new MyRoot(); root.add1("name1a", "abc"); root.add1("name1b", "def"); root.add2("name2", "zzz"); try (JsonWriter writer = new JsonWriter(new FileWriter(file))) { writer.setIndent(" "); Gson gson = new GsonBuilder().registerTypeAdapter(MyClass0.class, new MyTypeAdapter()).create(); gson.toJson(root, MyRoot.class, writer); } }
public void read(File file) throws IOException { MyRoot root; try (JsonReader reader = new JsonReader(new FileReader(file))) { Gson gson = new GsonBuilder().registerTypeAdapter(MyClass0.class, new MyTypeAdapter()).create(); root = gson.fromJson(reader, MyRoot.class); } System.out.println(root); }
TypeAdaper��p�ӂ��AGson�ɓo�^����̂��|�C���g�B
TypeAdapter�ł́A�N���X����\���v���p�e�B�[�ƃC���X�^���X�{�̂�ʁX�ɓo�^�E�擾����B
static class MyTypeAdapter implements JsonSerializer<MyClass0>, JsonDeserializer<MyClass0> { private static final String CLASSNAME = "CLASSNAME"; private static final String INSTANCE = "INSTANCE";
@Override public JsonElement serialize(MyClass0 src, Type typeOfSrc, JsonSerializationContext context) { JsonObject retValue = new JsonObject(); String className = src.getClass().getSimpleName(); retValue.addProperty(CLASSNAME, className); JsonElement elem = context.serialize(src); retValue.add(INSTANCE, elem); return retValue; }
@Override public MyClass0 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME); String className = prim.getAsString(); Class<?> klass; switch (className) { case "MyClass1": klass = MyClass1.class; break; case "MyClass2": klass = MyClass2.class; break; default: throw new JsonParseException("class not found. className=" + className); } return context.deserialize(jsonObject.get(INSTANCE), klass); } }
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@