             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);
         }
     }
 

            return;
        }

        var denseEnd :int = 0;
        while (denseEnd < arr.size && arr[denseEnd] === undefined) {
            denseEnd ++;
        }
        encodeInteger(1 | (denseEnd << 1));

        for (var prop :Object in arr) {
            if ((prop is int || prop is uint) && prop >= 0 && prop < denseEnd) {
                continue;
            }
            encodeString(String(prop));
            encodeValue(context, arr[prop]);
        }
        encodeString("");

        for (var ii :int = 0; ii < denseEnd; ii ++) {
            encodeValue(context, arr[ii]);
        }

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

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