             context.bytes.writeByte(0x80 | ((n >> 7) & 0x7F));
             context.bytes.writeByte(0x00 | (n & 0x7F));
 
-        } else {
+        } else if (n < 0x3FFFFFFF) {
             context.bytes.writeByte(0x80 | (n >> 21));
             context.bytes.writeByte(0x80 | ((n >> 14) & 0x7F));
             context.bytes.writeByte(0x80 | ((n >> 7) & 0x7F));
             context.bytes.writeByte(0x00 | (n & 0x7F));
 
+        } else {
+            throw new Error("Internal error - numerical overflow: " + n);
         }
     }
 

{
    public static function encode (bytes :ByteArray, value :*) :void
    {
        encodeValue(new Context(bytes), bytes, value);
    }

    private static function encodeValue (context :Context, value :*) :void


        } else if (value is Date) {
            // 0x08
            throw new Error("Date serialization not supported");

        } else if (value is Array) {
            context.bytes.writeByte(0x09);
