diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-06-11 18:12:24 +0200 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-06-11 18:12:24 +0200 |
commit | ff08a196fe98790d78ee7f5b26b9f367f9fa5a68 (patch) | |
tree | a2f6261fda4d69b685d83a5deb76f2d3b2069e0a /src/eu/equalparts/cardbase/utils | |
parent | 27be2dd797fc7087c5e362c22c1f30fc377ea0e9 (diff) |
Started work on the GUI, and added imageCode field to card, now need to update own collection
Diffstat (limited to 'src/eu/equalparts/cardbase/utils')
-rw-r--r-- | src/eu/equalparts/cardbase/utils/JSON.java | 2 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/utils/MTGUniverse.java | 46 |
2 files changed, 26 insertions, 22 deletions
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<ArrayList<CardSetInformation>>() {}); + cardSets = JSON.mapper.readValue(new URL(BASE_DATA_URL + "SetList.json"), new TypeReference<ArrayList<CardSetInformation>>() {}); } 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<Card> rawList = jsonTree.get("cards").traverse(JSON.mapper).readValueAs(new TypeReference<List<Card>>() {}); - // generate the map Map<String, Card> cardMap = new HashMap<String, Card>(); 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; } |