summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/provider/JsonEncodedProvider.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/p4square/grow/provider/JsonEncodedProvider.java')
-rw-r--r--src/com/p4square/grow/provider/JsonEncodedProvider.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/com/p4square/grow/provider/JsonEncodedProvider.java b/src/com/p4square/grow/provider/JsonEncodedProvider.java
new file mode 100644
index 0000000..605b18c
--- /dev/null
+++ b/src/com/p4square/grow/provider/JsonEncodedProvider.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.provider;
+
+import java.io.IOException;
+
+import org.codehaus.jackson.map.DeserializationConfig;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.SerializationConfig;
+
+/**
+ * Provider provides a simple interface for loading and persisting
+ * objects.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public abstract class JsonEncodedProvider<K, V> implements Provider<K, V> {
+ public static final ObjectMapper MAPPER = new ObjectMapper();
+ static {
+ MAPPER.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
+ MAPPER.configure(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING, true);
+ MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ }
+
+ private final Class<V> mClazz;
+
+ public JsonEncodedProvider(Class<V> clazz) {
+ mClazz = clazz;
+ }
+
+ /**
+ * Encode the object as JSON.
+ *
+ * @param obj The object to encode.
+ * @return The JSON encoding of obj.
+ * @throws IOException if the object cannot be encoded.
+ */
+ protected String encode(V obj) throws IOException {
+ return MAPPER.writeValueAsString(obj);
+ }
+
+ /**
+ * Decode the JSON string as an object.
+ *
+ * @param blob The JSON data to decode.
+ * @return The decoded object or null if blob is null.
+ * @throws IOException If an object cannot be decoded.
+ */
+ protected V decode(String blob) throws IOException {
+ if (blob == null) {
+ return null;
+ }
+
+ V obj = MAPPER.readValue(blob, mClazz);
+ return obj;
+ }
+}
+