Marcono1234 / serial-builder

Library for manually creating Java serialization data.
MIT License
28 stars 1 forks source link

Non-fluent builder API #2

Open Marcono1234 opened 1 year ago

Marcono1234 commented 1 year ago

Currently the builder only provides a fluent API. While this has some advantages it also have some disadvantages:

Therefore it might be useful to also offer a non-fluent builder API, for example similar to this:

byte[] serialData = serializableObject(
    classData(
        SerializableClass.class,
        List.of(
            intField("i", 6),
            objectField(
                "array",
                int[].class,
                array(new int[] {1, 2, 3})
            ),
            objectField(
                "s",
                String.class,
                string("nested-test")
            )
        )
    )
).createSerialData();

This could be realized in a rather user friendly way by exposing these static factory methods all on one class for which the user can then use a wildcard import (e.g. import static marcono1234...FactoryMethods.*;).

As part of this, it might be reasonable to rename the existing builders (and their packages) to:

The new builder API could be for now mostly be implemented on top of the existing fluent API. But eventually, especially for handle support, it might be easier to implement the fluent API on top of that new API. That would however also require a low level non-fluent API.

Marcono1234 commented 6 months ago

The new API could for example be named CompositeSerialBuilder (?).