@SuppressWarnings("unchecked") @Test public void persist() { // Serialize our test object. Map<String, Object> serial = test.serialize(); assertTrue(serial.get("items") instanceof List<?>); // We need to simulate Bukkit's automatic deserializing of objects. List<Map<String, Object>> itemdata = (List<Map<String, Object>>) serial.get("items"); Iterator<Map<String, Object>> i = itemdata.iterator(); List<ItemStack> newlist = new ArrayList<ItemStack>(); while (i.hasNext()) { newlist.add(ItemStack.deserialize(i.next())); } serial.put("items", newlist); // Deserialize as a new object. ItemBid test2; try { test2 = new ItemBid(serial); } catch (InstantiationException e) { fail("Deserialization failed"); return; } assertEquals("test and test2 have different hashCodes", test.hashCode(), test2.hashCode()); // Check if they match. assertTrue( "ItemBid.equals() says the deserialized object doesn't equal the original.", test.equals(test2)); }
private ItemStack[] deserial(Object o) throws SerializationException { try { if (o instanceof List) { final List<?> data = (List) o; List<ItemStack> items = new ArrayList<ItemStack>(data.size()); for (Object t : data) { if (t instanceof Map) { final Map<?, ?> mdata = (Map) t; final Map<String, Object> conv = new HashMap<String, Object>(mdata.size()); for (Map.Entry<?, ?> e : mdata.entrySet()) { conv.put(String.valueOf(e.getKey()), convert(e.getValue())); } LoggingManager.getInstance() .log(LoggingManager.Level.DEBUG, "Serializing Data: " + conv.entrySet().toString()); items.add(ItemStack.deserialize(conv)); } else { throw new IllegalArgumentException("Not a Map"); } } return items.toArray(new ItemStack[items.size()]); } throw new IllegalArgumentException("Not a List"); } catch (IllegalArgumentException ex) { throw new SerializationException(o, ex); } }
@SuppressWarnings("unchecked") public static ShopInventory getShopInventory(String s) { try { JSONObject j = JSONLoader.get(s); assert j.get("items") instanceof Map; Map<Integer, Map<String, Object>> items = (Map) j.get("items"); assert j.get("actions") instanceof Map; Map<Integer, String> actions = (Map) j.get("actions"); assert j.get("name") instanceof String; String title = (String) j.get("name"); Inventory e = Bukkit.createInventory(null, INVENTORY_SIZE, title); for (int i = 0; i < INVENTORY_SIZE; i++) { if (items.get(i) == null) continue; e.setItem(i, ItemStack.deserialize(items.get(i))); } return new ShopInventory(title, e, actions); } catch (InventoryParseException | IOException | ParseException e) { e.printStackTrace(); return null; } }