From 27be2dd797fc7087c5e362c22c1f30fc377ea0e9 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 11 Jun 2015 16:57:42 +0200 Subject: Streamlined Card class, cards are now stored in a hashmap, had to write a method to parse full card set json --- src/eu/equalparts/cardbase/utils/MTGUniverse.java | 80 +++++++++++++++++++++-- 1 file changed, 74 insertions(+), 6 deletions(-) (limited to 'src/eu/equalparts/cardbase/utils') diff --git a/src/eu/equalparts/cardbase/utils/MTGUniverse.java b/src/eu/equalparts/cardbase/utils/MTGUniverse.java index 0bcda5c..8c77032 100644 --- a/src/eu/equalparts/cardbase/utils/MTGUniverse.java +++ b/src/eu/equalparts/cardbase/utils/MTGUniverse.java @@ -4,10 +4,13 @@ import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; import eu.equalparts.cardbase.data.Card; import eu.equalparts.cardbase.data.CardSetInformation; @@ -97,11 +100,7 @@ public final class MTGUniverse { } // not cached; fetch, cache and return it else { - requestedSet = JSON.mapper.readValue(new URL(BASE_URL + validCode + ".json"), FullCardSet.class); - // MTG JSON does not include set code in the card information, but it is useful for sorting - for (Card card : requestedSet.getCards()) { - card.setCode = validCode; - } + requestedSet = parseFullSet(JSON.mapper.readValue(new URL(BASE_URL + validCode + ".json"), JsonNode.class)); cardSetCache.put(validCode, requestedSet); } } @@ -152,4 +151,73 @@ public final class MTGUniverse { } return null; } -} + + /** + * This method is necessary to adapt the list of cards in the json to + * the map format used in cardbase. + * + * @param jsonTree the tree-representation of the json to be parsed. + * @return the parsed full card set. + * + * @throws JsonMappingException if the upstream JSON does not map to {@code FullCardSet}. + * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs. + */ + private static FullCardSet parseFullSet(JsonNode jsonTree) throws JsonMappingException, IOException { + + FullCardSet fcs = new FullCardSet(); + + /* + * 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()); + } else { + throw new JsonMappingException("Field \"name\" not found."); + } + + String setCode; + if (jsonTree.hasNonNull("code")) { + setCode = jsonTree.get("code").asText(); + fcs.setCode(setCode); + } else { + throw new JsonMappingException("Field \"code\" not found."); + } + + if (jsonTree.hasNonNull("releaseDate")) { + fcs.setReleaseDate(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 + 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; + cardMap.put(card.number, card); + } + fcs.setCards(cardMap); + } else { + throw new JsonMappingException("Field \"cards\" not found."); + } + + return fcs; + + } +} \ No newline at end of file -- cgit v1.2.3