fa03f02e141c5056df9ad8bc89f9d59937500a74
1 package be
.nikiroo
.utils
.serial
;
3 public abstract class CustomSerializer
{
5 protected abstract String
toString(Object value
);
7 protected abstract Object
fromString(String content
);
9 protected abstract String
getType();
12 * Encode the object into the given builder if possible (if supported).
15 * the builder to append to
17 * the object to encode
18 * @return TRUE if success, FALSE if not (the content of the builder won't
19 * be changed in case of failure)
21 public boolean encode(StringBuilder builder
, Object value
) {
22 int prev
= builder
.length();
23 String customString
= toString(value
);
24 builder
.append("custom:").append(getType()).append(":");
25 if (!SerialUtils
.encode(builder
, customString
)) {
26 builder
.delete(prev
, builder
.length());
33 public Object
decode(String encodedValue
) {
34 return fromString((String
) SerialUtils
.decode(contentOf(encodedValue
)));
37 public static boolean isCustom(String encodedValue
) {
38 int pos1
= encodedValue
.indexOf(':');
39 int pos2
= encodedValue
.indexOf(':', pos1
+ 1);
41 return pos1
>= 0 && pos2
>= 0 && encodedValue
.startsWith("custom:");
44 public static String
typeOf(String encodedValue
) {
45 int pos1
= encodedValue
.indexOf(':');
46 int pos2
= encodedValue
.indexOf(':', pos1
+ 1);
47 String type
= encodedValue
.substring(pos1
+ 1, pos2
);
52 public static String
contentOf(String encodedValue
) {
53 int pos1
= encodedValue
.indexOf(':');
54 int pos2
= encodedValue
.indexOf(':', pos1
+ 1);
55 String encodedContent
= encodedValue
.substring(pos2
+ 1);
57 return encodedContent
;