Created
November 29, 2013 20:25
-
-
Save bcfurtado/7711460 to your computer and use it in GitHub Desktop.
GSON Serialization & Deserialization
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 test; | |
import java.util.ArrayList; | |
public class A { | |
public String nome; | |
public String idade; | |
public ArrayList<B> telefones = new ArrayList<B>(); | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getIdade() { | |
return idade; | |
} | |
public void setIdade(String idade) { | |
this.idade = idade; | |
} | |
public ArrayList<B> getTelefones() { | |
return telefones; | |
} | |
public void setTelefones(ArrayList<B> telefones) { | |
this.telefones = telefones; | |
} | |
public void addB(B b){ | |
this.telefones.add(b); | |
} | |
} |
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 testt; | |
public class B { | |
private String telefone; | |
private String endereco; | |
public B() { | |
} | |
public String getTelefone() { | |
return telefone; | |
} | |
public void setTelefone(String telefone) { | |
this.telefone = telefone; | |
} | |
public String getEndereco() { | |
return endereco; | |
} | |
public void setEndereco(String endereco) { | |
this.endereco = endereco; | |
} | |
} |
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 test; | |
import java.lang.reflect.Type; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
public class Main { | |
public static void main(String args[]){ | |
A a1 = new A(); | |
a1.setIdade("10"); | |
a1.setNome("User 1"); | |
A a2 = new A(); | |
a2.setIdade("11"); | |
a2.setNome("User 2"); | |
B b1 = new B(); | |
b1.setEndereco("address 1"); | |
b1.setTelefone("tel 1"); | |
B b2 = new B(); | |
b2.setEndereco("address 2"); | |
b2.setTelefone("tel 2"); | |
B b3 = new B(); | |
b3.setEndereco("address 3"); | |
b3.setTelefone("tel 4"); | |
a1.addB(b1); | |
a1.addB(b2); | |
a2.addB(b3); | |
ArrayList<A> as = new ArrayList<A>(); | |
as.add(a1); | |
as.add(a2); | |
Gson gson = new Gson(); | |
String json = gson.toJson(as); | |
System.out.println("Json: " + json); | |
Type collectionType = new TypeToken<Collection<A>>(){}.getType(); | |
Collection<A> list = gson.fromJson(json, collectionType); | |
for (A a : list) { | |
System.out.println("A:" + a.getNome()); | |
for (B b : a.getTelefones()) { | |
System.out.println("B: " + b.getTelefone()); | |
} | |
} | |
ArrayList<A> originalObject = (ArrayList<A>)list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment