aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/json
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/equalparts/cardbase/json')
-rw-r--r--src/eu/equalparts/cardbase/json/CardDeserializer.java60
-rw-r--r--src/eu/equalparts/cardbase/json/CardSerializer.java126
-rw-r--r--src/eu/equalparts/cardbase/json/JSON.java50
3 files changed, 236 insertions, 0 deletions
diff --git a/src/eu/equalparts/cardbase/json/CardDeserializer.java b/src/eu/equalparts/cardbase/json/CardDeserializer.java
new file mode 100644
index 0000000..c7ca0a1
--- /dev/null
+++ b/src/eu/equalparts/cardbase/json/CardDeserializer.java
@@ -0,0 +1,60 @@
+package eu.equalparts.cardbase.json;
+
+
+import java.io.IOException;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+
+import eu.equalparts.cardbase.card.Card;
+import eu.equalparts.cardbase.cardfield.IntegerCardField;
+import eu.equalparts.cardbase.cardfield.StringCardField;
+
+public class CardDeserializer extends StdDeserializer<Card> {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1894617997342518472L;
+
+ protected CardDeserializer(Class<?> vc) {
+ super(vc);
+ }
+
+ public CardDeserializer() {
+ this(null);
+ }
+
+ @Override
+ public Card deserialize(JsonParser jp, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ JsonNode node = jp.getCodec().readTree(jp);
+
+ Card newCard = new Card();
+
+ newCard.name = new StringCardField("name", node.hasNonNull("name") ? node.get("name").asText() : null);
+ newCard.layout = new StringCardField("layout", node.hasNonNull("layout") ? node.get("layout").asText() : null);
+ newCard.manaCost = new StringCardField("manaCost", node.hasNonNull("manaCost") ? node.get("manaCost").asText() : null);
+ newCard.cmc = new IntegerCardField("cmc", node.hasNonNull("cmc") ? node.get("cmc").asInt() : null);
+ newCard.type = new StringCardField("type", node.hasNonNull("type") ? node.get("type").asText() : null);
+ newCard.rarity = new StringCardField("rarity", node.hasNonNull("rarity") ? node.get("rarity").asText() : null);
+ newCard.text = new StringCardField("text", node.hasNonNull("text") ? node.get("text").asText() : null);
+ newCard.flavor = new StringCardField("flavor", node.hasNonNull("flavor") ? node.get("flavor").asText() : null);
+ newCard.artist = new StringCardField("artist", node.hasNonNull("artist") ? node.get("artist").asText() : null);
+ newCard.number = new StringCardField("number", node.hasNonNull("number") ? node.get("number").asText() : null);
+ newCard.power = new StringCardField("power", node.hasNonNull("power") ? node.get("power").asText() : null);
+ newCard.toughness = new StringCardField("toughness", node.hasNonNull("toughness") ? node.get("toughness").asText() : null);
+ newCard.loyalty = new IntegerCardField("loyalty", node.hasNonNull("loyalty") ? node.get("loyalty").asInt() : null);
+ newCard.multiverseid = new IntegerCardField("multiverseid", node.hasNonNull("multiverseid") ? node.get("multiverseid").asInt() : null);
+ newCard.imageName = new StringCardField("imageName", node.hasNonNull("imageName") ? node.get("imageName").asText() : null);
+ newCard.watermark = new StringCardField("watermark", node.hasNonNull("watermark") ? node.get("watermark").asText() : null);
+ newCard.setCode = new StringCardField("setCode", node.hasNonNull("setCode") ? node.get("setCode").asText() : null);
+ newCard.imageCode = new StringCardField("imageCode", node.hasNonNull("imageCode") ? node.get("imageCode").asText() : null);
+
+ return newCard;
+ }
+
+}
diff --git a/src/eu/equalparts/cardbase/json/CardSerializer.java b/src/eu/equalparts/cardbase/json/CardSerializer.java
new file mode 100644
index 0000000..05b41c0
--- /dev/null
+++ b/src/eu/equalparts/cardbase/json/CardSerializer.java
@@ -0,0 +1,126 @@
+package eu.equalparts.cardbase.json;
+
+import java.io.IOException;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+
+import eu.equalparts.cardbase.card.Card;
+
+public class CardSerializer extends JsonSerializer<Card> {
+ @Override
+ public void serialize(Card card, JsonGenerator jGen, SerializerProvider serializerProvider) throws IOException {
+ jGen.writeStartObject();
+
+ if (card.name.get() != null) {
+ jGen.writeStringField("name", card.name.get());
+ } else {
+ jGen.writeNullField("name");
+ }
+
+ if (card.layout.get() != null) {
+ jGen.writeStringField("layout", card.layout.get());
+ } else {
+ jGen.writeNullField("layout");
+ }
+
+ if (card.manaCost.get() != null) {
+ jGen.writeStringField("manaCost", card.manaCost.get());
+ } else {
+ jGen.writeNullField("manaCost");
+ }
+
+ if (card.cmc.get() != null) {
+ jGen.writeNumberField("cmc", card.cmc.get());
+ } else {
+ jGen.writeNullField("cmc");
+ }
+
+ if (card.type.get() != null) {
+ jGen.writeStringField("type", card.type.get());
+ } else {
+ jGen.writeNullField("type");
+ }
+
+ if (card.rarity.get() != null) {
+ jGen.writeStringField("rarity", card.rarity.get());
+ } else {
+ jGen.writeNullField("rarity");
+ }
+
+ if (card.text.get() != null) {
+ jGen.writeStringField("text", card.text.get());
+ } else {
+ jGen.writeNullField("text");
+ }
+
+ if (card.flavor.get() != null) {
+ jGen.writeStringField("flavor", card.flavor.get());
+ } else {
+ jGen.writeNullField("flavor");
+ }
+
+ if (card.artist.get() != null) {
+ jGen.writeStringField("artist", card.artist.get());
+ } else {
+ jGen.writeNullField("artist");
+ }
+
+ if (card.number.get() != null) {
+ jGen.writeStringField("number", card.number.get());
+ } else {
+ jGen.writeNullField("number");
+ }
+
+ if (card.power.get() != null) {
+ jGen.writeStringField("power", card.power.get());
+ } else {
+ jGen.writeNullField("power");
+ }
+
+ if (card.toughness.get() != null) {
+ jGen.writeStringField("toughness", card.toughness.get());
+ } else {
+ jGen.writeNullField("toughness");
+ }
+
+ if (card.loyalty.get() != null) {
+ jGen.writeNumberField("loyalty", card.loyalty.get());
+ } else {
+ jGen.writeNullField("loyalty");
+ }
+
+ if (card.multiverseid.get() != null) {
+ jGen.writeNumberField("multiverseid", card.multiverseid.get());
+ } else {
+ jGen.writeNullField("multiverseid");
+ }
+
+ if (card.imageName.get() != null) {
+ jGen.writeStringField("imageName", card.imageName.get());
+ } else {
+ jGen.writeNullField("imageName");
+ }
+
+ if (card.watermark.get() != null) {
+ jGen.writeStringField("watermark", card.watermark.get());
+ } else {
+ jGen.writeNullField("watermark");
+ }
+
+ if (card.setCode.get() != null) {
+ jGen.writeStringField("setCode", card.setCode.get());
+ } else {
+ jGen.writeNullField("setCode");
+ }
+
+ if (card.imageCode.get() != null) {
+ jGen.writeStringField("imageCode", card.imageCode.get());
+ } else {
+ jGen.writeNullField("imageCode");
+ }
+
+ jGen.writeEndObject();
+ }
+}
diff --git a/src/eu/equalparts/cardbase/json/JSON.java b/src/eu/equalparts/cardbase/json/JSON.java
new file mode 100644
index 0000000..b06f205
--- /dev/null
+++ b/src/eu/equalparts/cardbase/json/JSON.java
@@ -0,0 +1,50 @@
+package eu.equalparts.cardbase.json;
+
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+import eu.equalparts.cardbase.card.Card;
+
+/**
+ * This class simply holds an {@code ObjectMapper} to be used whenever JSON must be parsed.
+ * In the future it may be removed in favour of individual mappers for each function.
+ *
+ * @author Eduardo Pedroni
+ */
+public final class JSON {
+
+ /**
+ * The Jackson {@code ObjectMapper} which parses fetched JSON files.
+ */
+ public static final ObjectMapper mapper = createMapper();
+
+ /**
+ * Private constructor, this class is not to be instantiated.
+ */
+ private JSON() {}
+
+ /**
+ * Instantiate and configure Jackson mapper statically.
+ *
+ * @return the {@code ObjectMapper}, ready to use.
+ */
+ private static ObjectMapper createMapper() {
+ ObjectMapper objectMapper = new ObjectMapper();
+ // classes don't necessarily use all json fields
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ // disable auto detection
+ objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS,
+ MapperFeature.AUTO_DETECT_FIELDS,
+ MapperFeature.AUTO_DETECT_GETTERS,
+ MapperFeature.AUTO_DETECT_IS_GETTERS);
+
+ SimpleModule sm = new SimpleModule();
+ sm.addDeserializer(Card.class, new CardDeserializer());
+ sm.addSerializer(Card.class, new CardSerializer());
+
+ objectMapper.registerModule(sm);
+ return objectMapper;
+ }
+}