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.java1
-rw-r--r--src/eu/equalparts/cardbase/json/CardSerializer.java6
-rw-r--r--src/eu/equalparts/cardbase/json/FullCardSetDeserializer.java91
-rw-r--r--src/eu/equalparts/cardbase/json/JSON.java2
4 files changed, 93 insertions, 7 deletions
diff --git a/src/eu/equalparts/cardbase/json/CardDeserializer.java b/src/eu/equalparts/cardbase/json/CardDeserializer.java
index c7ca0a1..7193c53 100644
--- a/src/eu/equalparts/cardbase/json/CardDeserializer.java
+++ b/src/eu/equalparts/cardbase/json/CardDeserializer.java
@@ -52,7 +52,6 @@ public class CardDeserializer extends StdDeserializer<Card> {
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
index 05b41c0..5699061 100644
--- a/src/eu/equalparts/cardbase/json/CardSerializer.java
+++ b/src/eu/equalparts/cardbase/json/CardSerializer.java
@@ -115,12 +115,6 @@ public class CardSerializer extends JsonSerializer<Card> {
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/FullCardSetDeserializer.java b/src/eu/equalparts/cardbase/json/FullCardSetDeserializer.java
new file mode 100644
index 0000000..73034a0
--- /dev/null
+++ b/src/eu/equalparts/cardbase/json/FullCardSetDeserializer.java
@@ -0,0 +1,91 @@
+package eu.equalparts.cardbase.json;
+
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+
+import eu.equalparts.cardbase.card.Card;
+import eu.equalparts.cardbase.card.FullCardSet;
+
+public class FullCardSetDeserializer extends StdDeserializer<FullCardSet> {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1894617997342518472L;
+
+ protected FullCardSetDeserializer(Class<?> vc) {
+ super(vc);
+ }
+
+ public FullCardSetDeserializer() {
+ this(null);
+ }
+
+ @Override
+ public FullCardSet deserialize(JsonParser jp, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ JsonNode jsonTree = jp.getCodec().readTree(jp);
+
+ FullCardSet fcs = new FullCardSet();
+
+ /*
+ * These fields are critical, if any of them is not present an exception is thrown.
+ */
+ if (jsonTree.hasNonNull("name")) {
+ fcs.name = jsonTree.get("name").asText();
+ } else {
+ throw new JsonMappingException("Field \"name\" not found.");
+ }
+
+ String setCode;
+ if (jsonTree.hasNonNull("code")) {
+ setCode = jsonTree.get("code").asText();
+ fcs.code = setCode;
+ } else {
+ throw new JsonMappingException("Field \"code\" not found.");
+ }
+
+ if (jsonTree.hasNonNull("releaseDate")) {
+ fcs.releaseDate = jsonTree.get("releaseDate").asText();
+ } else {
+ throw new JsonMappingException("Field \"releaseDate\" not found.");
+ }
+
+ if (jsonTree.hasNonNull("cards")) {
+ // 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.set(setCode);
+ cardMap.put(card.number.get(), card);
+ }
+ fcs.cards = cardMap;
+ } else {
+ throw new JsonMappingException("Field \"cards\" not found.");
+ }
+
+ /*
+ * These fields are optional and are set to null if not present.
+ */
+ 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;
+ fcs.magicCardsInfoCode = jsonTree.hasNonNull("magicCardsInfoCode") ? jsonTree.get("magicCardsInfoCode").asText() : null;
+
+ return fcs;
+ }
+
+}
diff --git a/src/eu/equalparts/cardbase/json/JSON.java b/src/eu/equalparts/cardbase/json/JSON.java
index b06f205..8204be6 100644
--- a/src/eu/equalparts/cardbase/json/JSON.java
+++ b/src/eu/equalparts/cardbase/json/JSON.java
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import eu.equalparts.cardbase.card.Card;
+import eu.equalparts.cardbase.card.FullCardSet;
/**
* This class simply holds an {@code ObjectMapper} to be used whenever JSON must be parsed.
@@ -43,6 +44,7 @@ public final class JSON {
SimpleModule sm = new SimpleModule();
sm.addDeserializer(Card.class, new CardDeserializer());
sm.addSerializer(Card.class, new CardSerializer());
+ sm.addDeserializer(FullCardSet.class, new FullCardSetDeserializer());
objectMapper.registerModule(sm);
return objectMapper;