Support serializing non-HashMap implementations of Map.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6678 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -77,7 +77,7 @@ public class ObjectOutputStream
|
||||
}
|
||||
|
||||
writeShort(-cmap.code);
|
||||
writeUTF(streamer.getJavaClassName());
|
||||
writeUTF(streamer.getUpstreamJavaClassName());
|
||||
|
||||
} else {
|
||||
writeShort(cmap.code);
|
||||
|
||||
@@ -37,6 +37,7 @@ import com.threerings.io.streamers.MapStreamer;
|
||||
import com.threerings.io.streamers.NumberStreamer;
|
||||
import com.threerings.io.streamers.SetStreamer;
|
||||
import com.threerings.io.streamers.StringStreamer;
|
||||
import com.threerings.io.streamers.DelegatingStreamer;
|
||||
|
||||
public class Streamer
|
||||
{
|
||||
@@ -94,7 +95,17 @@ public class Streamer
|
||||
streamer = new Streamer(clazz, jname);
|
||||
|
||||
} else {
|
||||
return null;
|
||||
// if we already support streaming a superclass of this object, use it instead
|
||||
// (e.g. a DictionaryMap gets deserialized on the server as a HashMap)
|
||||
for each (var upcast :Streamer in _byJName) {
|
||||
if (ClassUtil.isAssignableAs(upcast._target, clazz)) {
|
||||
streamer = new DelegatingStreamer(upcast, clazz, jname);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (streamer == null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,13 +123,26 @@ public class Streamer
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the String to use to identify the class that we're streaming.
|
||||
* Returns the canonical class name used to identify this streamer. This is known as the java
|
||||
* class name and is typically the class sent to the server, but may in some cases be different.
|
||||
* @see com.threerings.io.streamers.DelegatingStreamer
|
||||
*/
|
||||
public function getJavaClassName () :String
|
||||
{
|
||||
return _jname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the class name used to deserialize the class represented by this streamer on the
|
||||
* server. This is typically the same as the java class name, but may in some cases be
|
||||
* different.
|
||||
* @see com.threerings.io.streamers.DelegatingStreamer
|
||||
*/
|
||||
public function getUpstreamJavaClassName () :String
|
||||
{
|
||||
return _jname;
|
||||
}
|
||||
|
||||
public function writeObject (obj :Object, out :ObjectOutputStream) :void
|
||||
//throws IOError
|
||||
{
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://code.google.com/p/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.io.streamers {
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.Streamer;
|
||||
|
||||
/**
|
||||
* A streamer that allows subclasses or implementations of the classes or interfaces supported by
|
||||
* other streamers to be included in upstream messages. All serialization support is impelemented
|
||||
* using the parent streamer, but the java class name is different. This is so that the streamer
|
||||
* caching mechanism and the upstream class mappings both work.
|
||||
*/
|
||||
public class DelegatingStreamer extends Streamer
|
||||
{
|
||||
public function DelegatingStreamer (parent :Streamer, targ:Class, jname:String=null)
|
||||
{
|
||||
super(targ, jname);
|
||||
_parent = parent;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
override public function getUpstreamJavaClassName () :String
|
||||
{
|
||||
return _parent.getUpstreamJavaClassName();
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
|
||||
//throws IOError
|
||||
{
|
||||
_parent.writeObject(obj, out);
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
override public function createObject (ins :ObjectInputStream) :Object
|
||||
//throws IOError
|
||||
{
|
||||
return _parent.createObject(ins);
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
override public function readObject (obj :Object, ins :ObjectInputStream) :void
|
||||
//throws IOError
|
||||
{
|
||||
_parent.readObject(obj, ins);
|
||||
}
|
||||
|
||||
protected var _parent :Streamer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user