private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();

        final int size = dataSet.size();
        out.writeInt(size);

        if (size > 0) {
            DataOutputViewStreamWrapper wrapper = new DataOutputViewStreamWrapper(out);
            for (T element : dataSet) {
                serializer.serialize(element, wrapper);
            }
        }
    }
@Test
    void testSerializationFailure() {
        try (ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(buffer)) {
            // a mock serializer that fails when writing
            CollectionInputFormat<ElementType> inFormat =
                    new CollectionInputFormat<>(
                            Collections.singleton(new ElementType()),
                            new TestSerializer(false, true));

            try {
                out.writeObject(inFormat);
                fail("should throw an exception");
            } catch (TestException e) {
                // expected
            } catch (Exception e) {
                fail("Exception not properly forwarded");
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }