I have a class that looks something like this
private byte[] profilePicture;
private ProfileType profileType;
private String profileHandle;
private String personalEmail;
private Address address;
JsonObject payloadObject = Json.createObjectBuilder()
.add("profilePicture", profilePictureEncoded)
.add("profileType", profileType.ordinal())
.add("profileHandle", profileHandle)
.add("personalEmail", personalEmail)
.add("webAppEmail", webappEmail)
.add("address", address.produceJsonPayload())
.build();
JsonObject payloadObject = Json.createObjectBuilder().build();
if(profilePicture != null)
{
final String profilePictureEncoded = ..... //encode the image
payloadObject.put("profilePicture",profilePictureEncoded);
}
You can just get a reference to the JsonObjectBuilder
.
JsonObjectBuilder payloadObjectBuilder = Json.createObjectBuilder();
wait until you have everything while constructing
if(profilePicture != null)
{
final String profilePictureEncoded = ..... //encode the image
payloadObject.add("profilePicture",profilePictureEncoded);
}
and then build()
JsonObject payloadObject = payloadObjectBuilder.build();