List of usage examples for com.google.gson Gson newJsonReader
public JsonReader newJsonReader(Reader reader)
From source file:com.aliakseipilko.flightdutytracker.utils.BackupUtils.java
License:Open Source License
public static void deserialiseFlightsFileToRealm(Realm realm, final File srcFile, final BackupRestoreBaseFragment callingView) { realm.executeTransactionAsync(new Realm.Transaction() { @Override/*from w w w .jav a 2 s .c o m*/ public void execute(Realm realm) { try { GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.registerTypeAdapter(Flight.class, new FlightSerialiser()) .serializeNulls().setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY) .setDateFormat(DateFormat.FULL, DateFormat.FULL).create(); JsonReader reader = gson.newJsonReader(new FileReader(srcFile)); List<Flight> flights; Type type = new TypeToken<List<Flight>>() { }.getType(); flights = gson.fromJson(reader, type); for (Flight f : flights) { //Check for ID duplication if (realm.where(Flight.class).equalTo("id", f.getId()).findFirst() == null) { realm.copyToRealm(f); } else { long newId; Number idNum = realm.where(Flight.class).max("id"); if (idNum == null) { newId = 1; } else { long id = idNum.longValue(); newId = id + 1; } f.setId(newId); realm.copyToRealm(f); } } } catch (IOException e) { e.printStackTrace(); } } }, new Realm.Transaction.OnSuccess() { @Override public void onSuccess() { callingView.showSuccess("Restore completed successfully!"); } }, new Realm.Transaction.OnError() { @Override public void onError(Throwable error) { callingView.showError("That didn't work. Try again"); } }); }
From source file:guru.qas.martini.report.Main.java
License:Apache License
protected void createReport(ApplicationContext context, Gson gson, TraceabilityMatrix matrix) throws Exception { try (Reader reader = getReader(context); JsonReader jsonReader = gson.newJsonReader(reader); OutputStream outputStream = getOutputStream(context)) { jsonReader.setLenient(true);//from w w w. j a va 2 s. co m matrix.createReport(jsonReader, outputStream); } }
From source file:persistance.JSONHandler.java
public static ArrayList<Colony> loadColonies(String filePath) throws IOException { ArrayList<Colony> colonies = new ArrayList<>(); try (Reader reader = new InputStreamReader(new FileInputStream(filePath))) { Gson gson = new GsonBuilder().create(); JsonReader jsonReader = gson.newJsonReader(reader); jsonReader.beginArray();//from ww w . j a va 2s . co m while (jsonReader.hasNext()) { colonies.add(parseColony(jsonReader, colonies)); } jsonReader.endArray(); return colonies; } }