// @SuppressWarnings("unchecked")
public static String encodeString(Object object) throws Exception {
if (object instanceof SuperCodable) {
((SuperCodable) object).preEncode();
}
KVPairs kv = new KVPairs();
CodableClassInfo fieldMap = Fields.getClassFieldMap(object.getClass());
if (fieldMap.size() == 0) {
return object.toString();
}
String stype = fieldMap.getClassName(object);
if (stype != null) {
kv.putValue(fieldMap.getClassField(), stype);
}
for (Iterator<CodableFieldInfo> fields = fieldMap.values().iterator(); fields.hasNext(); ) {
CodableFieldInfo field = fields.next();
Object value = field.get(object);
if (value == null) {
continue;
}
String name = field.getName();
if (field.isArray()) {
int len = Array.getLength(value);
if (field.getType() == byte.class) {
kv.putValue(name, new String(Base64.encode((byte[]) value)));
} else {
for (int i = 0; i < len; i++) {
Object av = Array.get(value, i);
kv.putValue(name + i, encodeString(av));
}
}
} else if (field.isCollection()) {
Collection<?> coll = (Collection<?>) value;
int idx = 0;
for (Iterator<?> iter = coll.iterator(); iter.hasNext(); idx++) {
kv.putValue(name + idx, encodeString(iter.next()));
}
} else if (field.isMap()) {
Map<?, ?> map = (Map<?, ?>) value;
KVPairs nv = new KVPairs();
for (Entry<?, ?> entry : map.entrySet()) {
nv.putValue(entry.getKey().toString(), encodeString(entry.getValue()));
}
kv.putValue(field.getName(), nv.toString());
} else if (field.isNative()) {
kv.putValue(name, value.toString());
} else if (field.isEnum()) {
kv.putValue(name, value.toString());
} else if (field.isCodable()) {