summaryrefslogtreecommitdiffstats
path: root/src/main/java/com
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2014-11-09 13:33:27 -0500
committerJames Moger <james.moger@gitblit.com>2014-11-10 21:46:26 -0500
commit4b9a61d0ef0fc2a9230a53a0ade45a20889aa9e3 (patch)
tree5b93bd2ad0425d46c0fea61c66ab56128e29b29c /src/main/java/com
parent97358035987e26ea801abac461bf5b5c9a406aa7 (diff)
downloadiciql-4b9a61d0ef0fc2a9230a53a0ade45a20889aa9e3.tar.gz
iciql-4b9a61d0ef0fc2a9230a53a0ade45a20889aa9e3.zip
Revise built-in type adapter class names again, add Yaml type adapter
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/iciql/adapter/GsonTypeAdapter.java15
-rw-r--r--src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.java55
-rw-r--r--src/main/java/com/iciql/adapter/XStreamTypeAdapter.java24
-rw-r--r--src/main/java/com/iciql/adapter/postgresql/JsonObjectAdapter.java (renamed from src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java)9
-rw-r--r--src/main/java/com/iciql/adapter/postgresql/JsonStringAdapter.java (renamed from src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java)62
-rw-r--r--src/main/java/com/iciql/adapter/postgresql/JsonbObjectAdapter.java (renamed from src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java)7
-rw-r--r--src/main/java/com/iciql/adapter/postgresql/JsonbStringAdapter.java (renamed from src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java)2
-rw-r--r--src/main/java/com/iciql/adapter/postgresql/XmlObjectAdapter.java (renamed from src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java)10
-rw-r--r--src/main/java/com/iciql/adapter/postgresql/XmlStringAdapter.java (renamed from src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java)2
9 files changed, 123 insertions, 63 deletions
diff --git a/src/main/java/com/iciql/adapter/GsonTypeAdapter.java b/src/main/java/com/iciql/adapter/GsonTypeAdapter.java
index a85f15e..b07b7b5 100644
--- a/src/main/java/com/iciql/adapter/GsonTypeAdapter.java
+++ b/src/main/java/com/iciql/adapter/GsonTypeAdapter.java
@@ -20,16 +20,21 @@ import com.google.gson.GsonBuilder;
import com.iciql.Iciql.DataTypeAdapter;
/**
- * Base class for inserting/retrieving a Java Object (de)serialized as JSON using Google GSON.
- * <p>You use this by creating a subclass which defines your object class.</p>
+ * Base class for inserting/retrieving a Java Object (de)serialized as JSON
+ * using Google GSON.
+ * <p>
+ * You use this by creating a subclass which defines your object class.
+ * </p>
+ *
* <pre>
* public class CustomObjectAdapter extends GsonTypeAdapter&lt;CustomObject&gt; {
*
- * public Class&lt;CustomObject&gt; getJavaType() {
- * return CustomObject.class;
- * }
+ * public Class&lt;CustomObject&gt; getJavaType() {
+ * return CustomObject.class;
+ * }
* }
* </pre>
+ *
* @param <T>
*/
public abstract class GsonTypeAdapter<T> implements DataTypeAdapter<T> {
diff --git a/src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.java b/src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.java
new file mode 100644
index 0000000..fe4541c
--- /dev/null
+++ b/src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2014 James Moger.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.iciql.adapter;
+
+import org.yaml.snakeyaml.DumperOptions.FlowStyle;
+import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.nodes.Tag;
+
+import com.iciql.Iciql.DataTypeAdapter;
+
+/**
+ * Base class for inserting/retrieving a Java Object (de)serialized as YAML using SnakeYaml.
+ */
+public abstract class SnakeYamlTypeAdapter<T> implements DataTypeAdapter<T> {
+
+ protected Yaml yaml() {
+ return new Yaml();
+ }
+
+ @Override
+ public String getDataType() {
+ return "TEXT";
+ }
+
+ @Override
+ public abstract Class<T> getJavaType();
+
+ @Override
+ public Object serialize(Object value) {
+ return yaml().dumpAs(value, Tag.MAP, FlowStyle.BLOCK);
+ }
+
+ @Override
+ public T deserialize(Object value) {
+ String yaml = value.toString();
+ Yaml processor = yaml();
+ T t = processor.loadAs(yaml, getJavaType());
+ return t;
+ }
+
+}
diff --git a/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java b/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java
index 3d1a9ce..5e152df 100644
--- a/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java
+++ b/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java
@@ -21,18 +21,8 @@ import com.thoughtworks.xstream.XStream;
/**
* Base class for inserting/retrieving a Java Object (de)serialized as XML using XStream.
- * <p>You use this by creating a subclass which defines your object class.</p>
- * <pre>
- * public class CustomObjectAdapter extends XStreamTypeAdapter&lt;CustomObject&gt; {
- *
- * public Class&lt;CustomObject&gt; getJavaType() {
- * return CustomObject.class;
- * }
- * }
- * </pre>
- * @param <T>
*/
-public abstract class XStreamTypeAdapter<T> implements DataTypeAdapter<T> {
+public class XStreamTypeAdapter implements DataTypeAdapter<Object> {
protected XStream xstream() {
return new XStream();
@@ -44,15 +34,21 @@ public abstract class XStreamTypeAdapter<T> implements DataTypeAdapter<T> {
}
@Override
- public Object serialize(T value) {
+ public Class<Object> getJavaType() {
+ return Object.class;
+ }
+
+ @Override
+ public Object serialize(Object value) {
return xstream().toXML(value);
}
@Override
- public T deserialize(Object value) {
+ public Object deserialize(Object value) {
String xml = value.toString();
XStream xstream = xstream();
- T t = (T) xstream.fromXML(xml);
+ Object t = xstream.fromXML(xml);
return t;
}
+
}
diff --git a/src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonObjectAdapter.java
index 64481bd..118cfc1 100644
--- a/src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java
+++ b/src/main/java/com/iciql/adapter/postgresql/JsonObjectAdapter.java
@@ -13,20 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.iciql.adapter.postgres;
+package com.iciql.adapter.postgresql;
import java.sql.SQLException;
import org.postgresql.util.PGobject;
+import com.iciql.adapter.GsonTypeAdapter;
+
/**
- * Postgres JSON data type adapter maps a JSON column to a domain object using Google GSON.
+ * Postgres JSON data type adapter maps a JSON column to a domain object using
+ * Google GSON.
*
* @author James Moger
*
* @param <T>
*/
-public abstract class GsonTypeAdapter<T> extends com.iciql.adapter.GsonTypeAdapter<T> {
+public abstract class JsonObjectAdapter<T> extends GsonTypeAdapter<T> {
@Override
public String getDataType() {
diff --git a/src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonStringAdapter.java
index 64ad073..c4fbd6a 100644
--- a/src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java
+++ b/src/main/java/com/iciql/adapter/postgresql/JsonStringAdapter.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.iciql.adapter.postgres;
+package com.iciql.adapter.postgresql;
import java.sql.SQLException;
@@ -22,34 +22,34 @@ import org.postgresql.util.PGobject;
import com.iciql.Iciql.DataTypeAdapter;
/**
- * Handles transforming raw strings to/from the Postgres JSON data type.
- */
- public class JsonStringAdapter implements DataTypeAdapter<String> {
-
- @Override
- public String getDataType() {
- return "json";
- }
-
- @Override
- public Class<String> getJavaType() {
- return String.class;
- }
-
- @Override
- public Object serialize(String value) {
- PGobject pg = new PGobject();
- pg.setType(getDataType());
- try {
- pg.setValue(value);
- } catch (SQLException e) {
- // not thrown on base PGobject
- }
- return pg;
- }
-
- @Override
- public String deserialize(Object value) {
- return value.toString();
+ * Handles transforming raw strings to/from the Postgres JSON data type.
+ */
+public class JsonStringAdapter implements DataTypeAdapter<String> {
+
+ @Override
+ public String getDataType() {
+ return "json";
+ }
+
+ @Override
+ public Class<String> getJavaType() {
+ return String.class;
+ }
+
+ @Override
+ public Object serialize(String value) {
+ PGobject pg = new PGobject();
+ pg.setType(getDataType());
+ try {
+ pg.setValue(value);
+ } catch (SQLException e) {
+ // not thrown on base PGobject
}
- } \ No newline at end of file
+ return pg;
+ }
+
+ @Override
+ public String deserialize(Object value) {
+ return value.toString();
+ }
+} \ No newline at end of file
diff --git a/src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonbObjectAdapter.java
index 6332742..6d8ad2c 100644
--- a/src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java
+++ b/src/main/java/com/iciql/adapter/postgresql/JsonbObjectAdapter.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.iciql.adapter.postgres;
+package com.iciql.adapter.postgresql;
import java.sql.SQLException;
@@ -22,13 +22,14 @@ import org.postgresql.util.PGobject;
import com.iciql.adapter.GsonTypeAdapter;
/**
- * Postgres JSONB data type adapter maps a JSONB column to a domain object using Google GSON.
+ * Postgres JSONB data type adapter maps a JSONB column to a domain object using
+ * Google GSON.
*
* @author James Moger
*
* @param <T>
*/
-public abstract class GsonBTypeAdapter<T> extends GsonTypeAdapter<T> {
+public abstract class JsonbObjectAdapter<T> extends GsonTypeAdapter<T> {
@Override
public String getDataType() {
diff --git a/src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonbStringAdapter.java
index 1619811..cc2d9c4 100644
--- a/src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java
+++ b/src/main/java/com/iciql/adapter/postgresql/JsonbStringAdapter.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.iciql.adapter.postgres;
+package com.iciql.adapter.postgresql;
import java.sql.SQLException;
diff --git a/src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java b/src/main/java/com/iciql/adapter/postgresql/XmlObjectAdapter.java
index 1bf6fc2..eba7c06 100644
--- a/src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java
+++ b/src/main/java/com/iciql/adapter/postgresql/XmlObjectAdapter.java
@@ -14,18 +14,18 @@
* limitations under the License.
*/
-package com.iciql.adapter.postgres;
-
+package com.iciql.adapter.postgresql;
+import com.iciql.adapter.XStreamTypeAdapter;
/**
- * Postgres XML data type adapter maps an XML column to a domain object using XStream.
+ * Postgres XML data type adapter maps an XML column to a domain object using
+ * XStream.
*
* @author James Moger
*
- * @param <T>
*/
-public abstract class XStreamTypeAdapter<T> extends com.iciql.adapter.XStreamTypeAdapter<T> {
+public abstract class XmlObjectAdapter extends XStreamTypeAdapter {
@Override
public String getDataType() {
diff --git a/src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java b/src/main/java/com/iciql/adapter/postgresql/XmlStringAdapter.java
index ded517e..defe9f9 100644
--- a/src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java
+++ b/src/main/java/com/iciql/adapter/postgresql/XmlStringAdapter.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.iciql.adapter.postgres;
+package com.iciql.adapter.postgresql;
import java.sql.SQLException;