1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
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;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.equalparts.cardbase.data.Cardbase;
import eu.equalparts.cardbase.data.CardSet;
import eu.equalparts.cardbase.data.MetaCardSet;
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 = createMapper();
private static ObjectMapper createMapper() {
ObjectMapper om = new ObjectMapper();
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return om;
}
/**
* @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);
}
}
|