分析了BasicSerializer和JavaSerializer,剩下的
序列化类就比较简单了,今天我们分析下CollectionSerializer和MapSerializer。
1.CollectionSerializer
public void writeObject(Object obj, AbstractHessianOutput out)
throws IOException
{
if (out.addRef(obj))
return;
Collection list = (Collection) obj;
Class cl = obj.getClass();
boolean hasEnd;
// 输出集合头,格式:'V'['l'集合大小] or 'Vt'[类型名]['l'集合大小]总是返回true
if (cl.equals(ArrayList.class) || ! _sendJavaType)
hasEnd = out.writeListBegin(list.size(), null);
else
hasEnd = out.writeListBegin(list.size(), obj.getClass().getName());
// 循环序列化每一个集合元素
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object value = iter.next();
// 若不清楚这里面做了什么,请看Hessian源码学习(三)
out.writeObject(value);
}
// 输出结束标记'z'
if (hasEnd)
out.writeListEnd();
}
2.MapSerializer
public void writeObject(Object obj, AbstractHessianOutput out)
throws IOException
{
if (out.addRef(obj))
return;
Map map = (Map) obj;
Class cl = obj.getClass();
// 输出Map头,格式:'Mt' or 'Mt'[类型名]
if (cl.equals(HashMap.class))
out.writeMapBegin(null);
else
out.writeMapBegin(obj.getClass().getName());
// 循环序列化每一个键值对
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
// 分别序列化键、值 (若不清楚这里面做了什么,请看Hessian源码学习(三))
out.writeObject(entry.getKey());
out.writeObject(entry.getValue());
}
// 输出结束标记'z'
out.writeMapEnd();
}