ArgumentError: Error #2004 when implementing iExternalizable

I’ve written a trivial class in attempting to better understand iExternalizable’s behavior. I can’t see that I’ve done anything wrong, but my code throws an error. Here’s my class:

package {
    import flash.utils.IDataInput;
    import flash.utils.IDataOutput;    
    import flash.utils.IExternalizable;

    public class Foo implements IExternalizable {
    
        private var A:String;
        public var B:String;
         
        function C(){
        }
         
        public function D(val:String):void {
            A = val;
        }
         
        public function E(val:String):void {
            B = val;
        }
         
        public function F():String {
            return A + " " + B;
        }
        
        public function writeExternal(output:IDataOutput): void {
            //output.writeInt(2);
            output.writeUTF(A);
            output.writeUTF(B);
        }
 
        public function readExternal(input:IDataInput): void {
            A = input.readUTF();
            B = input.readUTF();
        }
    }
}

This is the code on the first frame of my FLA:

var obj:Foo = new Foo();
obj.D("a");
obj.E("b");
var bytes: ByteArray = new ByteArray();
bytes.writeObject(obj);

and here’s the error that gets thrown:

ArgumentError: Error #2004: One of the parameters is invalid.
    at flash.utils::ByteArray/writeObject()

Are you using registerClassAlias before that?