X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FSerialUtils.java;h=61bca4f6d1a9b0e238c44b72c4589327d53d09e8;hb=72648e757f648cd152bc00dfb83f895260f037a0;hp=c222bc3e3d6334a76dcf03ad133a846609aed0b0;hpb=cd0c27d2e457ea19fcd9def879e1534a528292c2;p=nikiroo-utils.git
diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java
index c222bc3..61bca4f 100644
--- a/src/be/nikiroo/utils/serial/SerialUtils.java
+++ b/src/be/nikiroo/utils/serial/SerialUtils.java
@@ -1,23 +1,46 @@
package be.nikiroo.utils.serial;
-import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.NotSerializableException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.net.URL;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.UnknownFormatConversionException;
-import be.nikiroo.utils.ImageUtils;
+import be.nikiroo.utils.Image;
/**
* Small class to help with serialisation.
*
* Note that we do not support inner classes (but we do support nested classes)
* and all objects require an empty constructor to be deserialised.
+ *
+ * It is possible to add support to custom types (both the encoder and the
+ * decoder will require the custom classes) -- see {@link CustomSerializer}.
+ *
+ * Default supported types are:
+ *
+ *
NULL (as a null value)
+ *
String
+ *
Boolean
+ *
Byte
+ *
Character
+ *
Short
+ *
Long
+ *
Float
+ *
Double
+ *
Integer
+ *
Enum (any enum whose name and value is known by the caller)
+ *
java.awt.image.BufferedImage (as a {@link CustomSerializer})
+ *
An array of the above (as a {@link CustomSerializer})
+ *
URL
+ *
*
* @author niki
*/
@@ -65,7 +88,7 @@ public class SerialUtils {
}
@Override
- protected Object fromString(String content) {
+ protected Object fromString(String content) throws IOException {
String[] tab = content.split("\n");
try {
@@ -78,31 +101,54 @@ public class SerialUtils {
return array;
} catch (Exception e) {
- throw new UnknownFormatConversionException(e.getMessage());
+ if (e instanceof IOException) {
+ throw (IOException) e;
+ }
+ throw new IOException(e.getMessage());
}
}
});
- // Images (this is currently the only supported image type by default)
- customTypes.put("java.awt.image.BufferedImage", new CustomSerializer() {
+ // URL:
+ customTypes.put("java.net.URL", new CustomSerializer() {
@Override
protected String toString(Object value) {
- try {
- return ImageUtils.toBase64((BufferedImage) value);
- } catch (IOException e) {
- throw new UnknownFormatConversionException(e.getMessage());
+ if (value != null) {
+ return ((URL) value).toString();
+ }
+ return null;
+ }
+
+ @Override
+ protected Object fromString(String content) throws IOException {
+ if (content != null) {
+ return new URL(content);
}
+ return null;
}
@Override
protected String getType() {
- return "java.awt.image.BufferedImage";
+ return "java.net.URL";
+ }
+ });
+
+ // Images (this is currently the only supported image type by default)
+ customTypes.put("be.nikiroo.utils.Image", new CustomSerializer() {
+ @Override
+ protected String toString(Object value) {
+ return ((Image) value).toBase64();
+ }
+
+ @Override
+ protected String getType() {
+ return "be.nikiroo.utils.Image";
}
@Override
protected Object fromString(String content) {
try {
- return ImageUtils.fromBase64(content);
+ return new Image(content);
} catch (IOException e) {
throw new UnknownFormatConversionException(e.getMessage());
}
@@ -126,27 +172,59 @@ public class SerialUtils {
public static Object createObject(String type)
throws ClassNotFoundException, NoSuchMethodException {
+ String desc = null;
+
try {
Class> clazz = getClass(type);
String className = clazz.getName();
- Object[] args = null;
+ List