Package dev.adlin.vts4j.request
Class PayloadBuilder
java.lang.Object
dev.adlin.vts4j.request.PayloadBuilder
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@NonNull PayloadBuilderAppends a boolean property to the JSON object.@NonNull PayloadBuilderAppends a character property to the JSON object.@NonNull PayloadBuilderAppends a numeric property (Integer, Double, Long, etc.) to the JSON object.@NonNull PayloadBuilderAppends a text property to the JSON object.@NonNull PayloadBuilderaddOfNullable(@NonNull String field, @Nullable com.google.gson.JsonObject value) Conditionally appends a nestedJsonObjectif the provided value is not null.@NonNull com.google.gson.JsonObjectbuild()Concludes the construction process and returns the fully assembled JSON structure.static @NonNull PayloadBuilderbuilder()Creates a new fluent instance ofPayloadBuilder.
-
Constructor Details
-
PayloadBuilder
public PayloadBuilder()
-
-
Method Details
-
builder
Creates a new fluent instance ofPayloadBuilder.- Returns:
- a fresh builder instance
-
addField
Appends a text property to the JSON object.- Parameters:
field- the JSON property key namevalue- the string value to assign- Returns:
- this builder instance for method chaining
-
addField
Appends a character property to the JSON object.- Parameters:
field- the JSON property key namevalue- the character value to assign- Returns:
- this builder instance for method chaining
-
addField
Appends a numeric property (Integer, Double, Long, etc.) to the JSON object.- Parameters:
field- the JSON property key namevalue- the numeric value to assign- Returns:
- this builder instance for method chaining
-
addField
Appends a boolean property to the JSON object.- Parameters:
field- the JSON property key namevalue- 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 nestedJsonObjectif the provided value is not null.- Parameters:
field- the JSON property key name for the nested objectvalue- 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
JsonObjectinstance
-