Class PayloadBuilder

java.lang.Object
dev.adlin.vts4j.request.PayloadBuilder

public final class PayloadBuilder extends Object
A fluent builder utility designed for constructing GSON JsonObject instances.

This class provides a typesafe, chainable API to easily append properties and handle nullable nested JSON objects without boilerplate checks.

Example Usage:


 JsonObject payload = PayloadBuilder.builder()
         .addField("pluginName", "MyPlugin")
         .addField("enabled", true)
         .addField("version", 1)
         .addOfNullable("config", optionalConfigJson)
         .build();
 
  • Constructor Details

    • PayloadBuilder

      public PayloadBuilder()
  • Method Details

    • builder

      public static @NonNull PayloadBuilder builder()
      Creates a new fluent instance of PayloadBuilder.
      Returns:
      a fresh builder instance
    • addField

      public @NonNull PayloadBuilder addField(@NonNull String field, @NonNull String value)
      Appends a text property to the JSON object.
      Parameters:
      field - the JSON property key name
      value - the string value to assign
      Returns:
      this builder instance for method chaining
    • addField

      public @NonNull PayloadBuilder addField(@NonNull String field, @NonNull Character value)
      Appends a character property to the JSON object.
      Parameters:
      field - the JSON property key name
      value - the character value to assign
      Returns:
      this builder instance for method chaining
    • addField

      public @NonNull PayloadBuilder addField(@NonNull String field, @NonNull Number value)
      Appends a numeric property (Integer, Double, Long, etc.) to the JSON object.
      Parameters:
      field - the JSON property key name
      value - the numeric value to assign
      Returns:
      this builder instance for method chaining
    • addField

      public @NonNull PayloadBuilder addField(@NonNull String field, @NonNull Boolean value)
      Appends a boolean property to the JSON object.
      Parameters:
      field - the JSON property key name
      value - the boolean value to assign
      Returns:
      this builder instance for method chaining
    • addOfNullable

      public @NonNull PayloadBuilder addOfNullable(@NonNull String field, @Nullable com.google.gson.JsonObject value)
      Conditionally appends a nested JsonObject if the provided value is not null.
      Parameters:
      field - the JSON property key name for the nested object
      value - the nullable JSON object payload
      Returns:
      this builder instance for method chaining
    • build

      public @NonNull com.google.gson.JsonObject build()
      Concludes the construction process and returns the fully assembled JSON structure.
      Returns:
      the constructed JsonObject instance