From ff08a196fe98790d78ee7f5b26b9f367f9fa5a68 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 11 Jun 2015 18:12:24 +0200 Subject: Started work on the GUI, and added imageCode field to card, now need to update own collection --- src/eu/equalparts/cardbase/utils/JSON.java | 2 +- src/eu/equalparts/cardbase/utils/MTGUniverse.java | 46 ++++++++++++----------- 2 files changed, 26 insertions(+), 22 deletions(-) (limited to 'src/eu/equalparts/cardbase/utils') diff --git a/src/eu/equalparts/cardbase/utils/JSON.java b/src/eu/equalparts/cardbase/utils/JSON.java index a5992c7..17e8392 100644 --- a/src/eu/equalparts/cardbase/utils/JSON.java +++ b/src/eu/equalparts/cardbase/utils/JSON.java @@ -28,7 +28,7 @@ public final class JSON { */ private static ObjectMapper createMapper() { ObjectMapper objectMapper = new ObjectMapper(); - // TODO decide what to do about this + // classes don't necessarily use all json fields objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return objectMapper; } diff --git a/src/eu/equalparts/cardbase/utils/MTGUniverse.java b/src/eu/equalparts/cardbase/utils/MTGUniverse.java index 8c77032..51959aa 100644 --- a/src/eu/equalparts/cardbase/utils/MTGUniverse.java +++ b/src/eu/equalparts/cardbase/utils/MTGUniverse.java @@ -32,7 +32,7 @@ public final class MTGUniverse { /** * The base URL from where the information is fetched. */ - private static final String BASE_URL = "http://mtgjson.com/json/"; + private static final String BASE_DATA_URL = "http://mtgjson.com/json/"; /** * If the upstream set code list can't be loaded, this is loaded instead. */ @@ -100,7 +100,7 @@ public final class MTGUniverse { } // not cached; fetch, cache and return it else { - requestedSet = parseFullSet(JSON.mapper.readValue(new URL(BASE_URL + validCode + ".json"), JsonNode.class)); + requestedSet = parseFullSet(JSON.mapper.readValue(new URL(BASE_DATA_URL + validCode + ".json"), JsonNode.class)); cardSetCache.put(validCode, requestedSet); } } @@ -114,7 +114,7 @@ public final class MTGUniverse { // if the list isn't cached, fetch and cache it if (cardSets == null) { try { - cardSets = JSON.mapper.readValue(new URL(BASE_URL + "SetList.json"), new TypeReference>() {}); + cardSets = JSON.mapper.readValue(new URL(BASE_DATA_URL + "SetList.json"), new TypeReference>() {}); } catch (Exception e) { System.out.println("Error: could not fetch/parse set code list from upstream, loading fallback json..."); e.printStackTrace(); @@ -170,7 +170,7 @@ public final class MTGUniverse { * These fields are critical, if any of them is not present an exception is thrown. */ if (jsonTree.hasNonNull("name")) { - fcs.setName(jsonTree.get("name").asText()); + fcs.name = jsonTree.get("name").asText(); } else { throw new JsonMappingException("Field \"name\" not found."); } @@ -178,45 +178,49 @@ public final class MTGUniverse { String setCode; if (jsonTree.hasNonNull("code")) { setCode = jsonTree.get("code").asText(); - fcs.setCode(setCode); + fcs.code = setCode; } else { throw new JsonMappingException("Field \"code\" not found."); } + String imageCode; + if (jsonTree.hasNonNull("magicCardsInfoCode")) { + imageCode = jsonTree.get("magicCardsInfoCode").asText(); + fcs.magicCardsInfoCode = imageCode; + } else { + throw new JsonMappingException("Field \"magicCardsInfoCode\" not found."); + } + if (jsonTree.hasNonNull("releaseDate")) { - fcs.setReleaseDate(jsonTree.get("releaseDate").asText()); + fcs.releaseDate = jsonTree.get("releaseDate").asText(); } else { throw new JsonMappingException("Field \"releaseDate\" not found."); } - /* - * These fields are optional and are set to null if not present. - */ - fcs.setGathererCode(jsonTree.hasNonNull("gathererCode") ? jsonTree.get("gathererCode").asText() : null); - fcs.setBorder(jsonTree.hasNonNull("border") ? jsonTree.get("border").asText() : null); - fcs.setType(jsonTree.hasNonNull("type") ? jsonTree.get("type").asText() : null); - fcs.setMagicCardsInfoCode(jsonTree.hasNonNull("magicCardsInfoCode") ? jsonTree.get("magicCardsInfoCode").asText() : null); - fcs.setBlock(jsonTree.hasNonNull("block") ? jsonTree.get("block").asText() : null); - - /* - * This is a critical field which must be present, if not an exception is thrown. - */ if (jsonTree.hasNonNull("cards")) { - // attempt to card list as POJO + // attempt to read card list as a POJO using the standard mapper List rawList = jsonTree.get("cards").traverse(JSON.mapper).readValueAs(new TypeReference>() {}); - // generate the map Map cardMap = new HashMap(); for (Card card : rawList) { // add set code for convenience card.setCode = setCode; + card.imageCode = imageCode; cardMap.put(card.number, card); } - fcs.setCards(cardMap); + fcs.cards = cardMap; } else { throw new JsonMappingException("Field \"cards\" not found."); } + /* + * These fields are optional and are set to null if not present. + */ + fcs.gathererCode = jsonTree.hasNonNull("gathererCode") ? jsonTree.get("gathererCode").asText() : null; + fcs.border = jsonTree.hasNonNull("border") ? jsonTree.get("border").asText() : null; + fcs.type = jsonTree.hasNonNull("type") ? jsonTree.get("type").asText() : null; + fcs.block = jsonTree.hasNonNull("block") ? jsonTree.get("block").asText() : null; + return fcs; } -- cgit v1.2.3