Created
July 19, 2016 13:24
-
-
Save sunmeat/861807399dea570091459da8707697e4 to your computer and use it in GitHub Desktop.
gson example
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 serialization; | |
import com.google.gson.Gson; | |
class Student { | |
private String name; | |
private int age = (int) (Math.random() * 20 + 20); | |
public Student(String name) { | |
setName(name); | |
} | |
@Override | |
public String toString() { | |
return "Name: " + name + ", age: " + age; | |
} | |
} | |
class AdmittedGroup { | |
public Student first = new Student("Natalie"); | |
public Student second = new Student("Olga"); | |
public Student third = new Student("Stanislav"); | |
public Student fourth = new Student("Valentine"); | |
public Student fifth = new Student("Artem"); | |
@Override | |
public String toString() { | |
return first + "\n" | |
+ second + "\n" | |
+ third + "\n" | |
+ fourth + "\n" | |
+ fifth + "\n"; | |
} | |
} | |
class Program { | |
public static void main(String[] args) { | |
AdmittedGroup group = new AdmittedGroup(); | |
String jsonGroup = new Gson().toJson(group); | |
System.out.println("Json:\n" + jsonGroup); | |
AdmittedGroup returnTo = new Gson().fromJson(jsonGroup, AdmittedGroup.class); | |
System.out.println("\nObject:\n" + returnTo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment