General SubmodelComponents can now be saved to a json format
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / params / JsonHandler.java
1 package net.mograsim.logic.ui.model.components.params;
2
3 import java.io.FileReader;
4 import java.io.FileWriter;
5 import java.io.IOException;
6
7 import com.google.gson.Gson;
8 import com.google.gson.GsonBuilder;
9
10 public class JsonHandler
11 {
12         private static Gson parser = new GsonBuilder().setPrettyPrinting().create();
13
14         @SuppressWarnings("resource")
15         public static <T> T readJson(String path, Class<T> type) throws IOException
16         {
17                 FileReader reader = new FileReader(path);
18                 T params = parser.fromJson(new FileReader(path), type);
19                 reader.close();
20                 return params;
21         }
22
23         public static void writeJson(Object o, String path) throws IOException
24         {
25                 @SuppressWarnings("resource")
26                 FileWriter writer = new FileWriter(path);
27                 writer.write(parser.toJson(o));
28                 writer.close(); // TODO: Insure that writer is closed
29         }
30 }