From 7b3b465c520f615cbf88a3cc816cdec522a85d8e Mon Sep 17 00:00:00 2001 From: virmon Date: Tue, 14 Apr 2020 22:50:09 -0500 Subject: [PATCH] setup json deserializer --- src/smith/tukahirwa/storage/Factory.java | 76 ++++++++++++++++++------ 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/src/smith/tukahirwa/storage/Factory.java b/src/smith/tukahirwa/storage/Factory.java index 6fda80d..0319e8e 100644 --- a/src/smith/tukahirwa/storage/Factory.java +++ b/src/smith/tukahirwa/storage/Factory.java @@ -1,44 +1,82 @@ package smith.tukahirwa.storage; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import smith.tukahirwa.*; import java.io.FileNotFoundException; import java.io.FileReader; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class Factory { public static void main(String[] args) throws FileNotFoundException { + Gson gson = new Gson(); - // Accounts + // Accounts // Librarians - // Fines - // Authors // Array of Dates - // List of Members - // Racks // Address - - // Librarians - - //BookItems - - // using the Gson - var g = new Gson(); - List list = g.fromJson(new FileReader("src/smith/tukahirwa/docs/data-member.json"), - ArrayList.class); - System.out.println(list.get(0).getStatus()); - - + FileReader address = new FileReader("src/smith/tukahirwa/json/address.json"); + + Type addressType = new TypeToken>(){}.getType(); + + List
addressList = gson.fromJson(address, addressType); + System.out.println(addressList.get(0).getCity()); + + // BOOK ITEMS + // Date could not be parsed +// FileReader bookItem = new FileReader("src/smith/tukahirwa/json/book_item.json"); +// +// Type bookItemType = new TypeToken>(){}.getType(); +// +// List bookItemList = gson.fromJson(bookItem, bookItemType); +// System.out.println(bookItemList.get(0).getBarcode()); + + // BOOK LENDING + // Date could not be parsed +// FileReader bookLending = new FileReader("src/smith/tukahirwa/json/book_lending.json"); +// +// Type bookLendingType = new TypeToken>(){}.getType(); +// +// List bookReservationList = gson.fromJson(bookLending, bookLendingType); +// System.out.println(bookReservationList.get(0).getBookItemBarcode()); + + // BOOK RESERVATION + // Date could not be parsed +// FileReader bookReservation = new FileReader("src/smith/tukahirwa/json/book_reservation.json"); +// +// Type bookReservationType = new TypeToken>(){}.getType(); +// +// List bookReservationList = gson.fromJson(bookReservation, bookReservationType); +// System.out.println(bookReservationList.get(0).getBookItemBarcode()); + + // FINES + // JSON date could not be parsed +// FileReader fine = new FileReader("src/smith/tukahirwa/json/fine.json"); +// +// Type fineType = new TypeToken>(){}.getType(); +// +// List fineList = gson.fromJson(fine, fineType); +// System.out.println(fineList.get(0).getMemberId()); + + // MEMBERS + // Date could not be parsed +// FileReader members = new FileReader("src/smith/tukahirwa/json/members.json"); +// +// Type memberType = new TypeToken>(){}.getType(); +// +// List memberList = gson.fromJson(members, memberType); +// System.out.println(memberList.get(0).getId()); } // Addresses @@ -59,10 +97,10 @@ public static void main(String[] args) throws FileNotFoundException { // Members - static List memberList = List.of( +// static List memberList = List.of( // new Member() - ); +// );