diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-06-05 15:37:51 +0200 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-06-05 15:37:51 +0200 |
commit | 0465829c25ef43ba9ba898dff01ebaa5106fd1f8 (patch) | |
tree | 4b6a15e99294c6f3c12376c0d280b73387cd9613 /src/eu/equalparts/cardbase/query | |
parent | 1f0159bcf903c0f422ab47b07cd296da1e816f87 (diff) |
It works! Reading in a file, writing to the same file or a different existing file, glancing, perusing, adding and removing cards by number
Diffstat (limited to 'src/eu/equalparts/cardbase/query')
-rw-r--r-- | src/eu/equalparts/cardbase/query/IO.java | 46 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/query/Test.java | 1 |
2 files changed, 41 insertions, 6 deletions
diff --git a/src/eu/equalparts/cardbase/query/IO.java b/src/eu/equalparts/cardbase/query/IO.java index 65a3a34..9b81b20 100644 --- a/src/eu/equalparts/cardbase/query/IO.java +++ b/src/eu/equalparts/cardbase/query/IO.java @@ -2,6 +2,7 @@ package eu.equalparts.cardbase.query; import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -21,21 +22,56 @@ public class IO { public static final String BASE_URL = "http://mtgjson.com/json/"; public static final String SETS_URL = BASE_URL + "SetList.json"; - private static final ObjectMapper mapper = new ObjectMapper(); + private static final ObjectMapper mapper = createMapper(); - public static CardSet getCardSet(String setId) { - return new CardSet(); + private static ObjectMapper createMapper() { + ObjectMapper om = new ObjectMapper(); + om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return om; } - public static ArrayList<MetaCardSet> getAllMetaSets() throws JsonParseException, JsonMappingException, IOException { - //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + /** + * @param code + * @return the actual cardset (containing cards). + * @throws IOException + * @throws MalformedURLException + * @throws JsonMappingException + * @throws JsonParseException + */ + public static CardSet getCardSet(String code) throws JsonParseException, JsonMappingException, MalformedURLException, IOException { + return mapper.readValue(new URL(BASE_URL + code + ".json"), CardSet.class); + } + + /** + * @return a list of metadata for every available set. + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ + public static ArrayList<MetaCardSet> getAllMetaSets() throws JsonParseException, JsonMappingException, IOException { return mapper.readValue(new URL(SETS_URL), new TypeReference<ArrayList<MetaCardSet>>() {}); } + /** + * @param file + * @return a CardBase object equivalent to the given file. + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ public static CardBase readCardBase(File file) throws JsonParseException, JsonMappingException, IOException { return mapper.readValue(file, CardBase.class); } + /** + * Writes the provided CardBase to the provided file in JSON format. + * + * @param file + * @param cardBase + * @throws JsonGenerationException + * @throws JsonMappingException + * @throws IOException + */ public static void writeCardBase(File file, CardBase cardBase) throws JsonGenerationException, JsonMappingException, IOException { mapper.writeValue(file, cardBase); } diff --git a/src/eu/equalparts/cardbase/query/Test.java b/src/eu/equalparts/cardbase/query/Test.java index 2050c76..e466c55 100644 --- a/src/eu/equalparts/cardbase/query/Test.java +++ b/src/eu/equalparts/cardbase/query/Test.java @@ -59,7 +59,6 @@ public class Test { for (CardSet set : sets.values()) { for (Card card : set.cards) { card.setCode = set.code; - card.setName = set.name; // System.out.println(set.getName() + ": " + card.getName()); allCards.add(card); |