Untitled
unknown
plain_text
a year ago
2.1 kB
8
Indexable
import org.json.JSONObject;
import java.util.Optional;
public class JsonCreator {
public static String createJson(String endpointUri, String targetName, JSONObject input, Optional<JSONObject> headers) {
// Create the main JSON object
JSONObject mainJson = new JSONObject();
// Add endpointUri and targetName
mainJson.put("endpointUri", endpointUri);
mainJson.put("targetName", targetName);
// Add the input JSON object
mainJson.put("input", input);
// Check if headers are present and add them if they are
headers.ifPresent(h -> mainJson.put("headers", h));
// Return the string representation of the JSON object
return mainJson.toString(4); // Pretty printing with indentation
}
public static void main(String[] args) {
// Example usage
// Creating input JSON object
JSONObject input = new JSONObject();
input.put("piezoDocument", "<<redacted>>");
// Creating headers JSON object
JSONObject headers = new JSONObject();
headers.put("x-signature", "<<redacted>>");
headers.put("x-signature-key-id", "<<redacted>>");
headers.put("x-signature-public-key", "<<redacted>>");
// Use Optional.of() when headers are present
String jsonOutputWithHeaders = createJson(
"execute",
"xx",
input,
Optional.of(headers) // Headers are provided
);
// Use Optional.empty() when headers are not provided
String jsonOutputWithoutHeaders = createJson(
"execute",
"xx",
input,
Optional.empty() // No headers
);
// Output the generated JSONs
System.out.println("With Headers:");
System.out.println(jsonOutputWithHeaders);
System.out.println("Without Headers:");
System.out.println(jsonOutputWithoutHeaders);
}
}
Editor is loading...
Leave a Comment