Updated request body for form data

This commit is contained in:
nisha
2024-10-07 20:11:30 +05:30
parent fa46349720
commit 543963f22d
5 changed files with 98 additions and 16 deletions

View File

@@ -227,4 +227,47 @@ public class Utils {
Pattern pattern = Pattern.compile(EMAIL_REGEX);
return pattern.matcher(email).matches();
}
public static String convertObjectToJsonString(Object object) {
try {
// Check if the object is a string
if (object instanceof String) {
String str = (String) object;
// Return null if the string is null or empty
if (str != null && !str.trim().isEmpty()) {
return str; // Return the non-empty string
} else {
return null; // Return null for null or empty string
}
} else if (object != null) {
// Convert non-string objects (arrays, objects) to JSON strings
return mapper.writeValueAsString(object);
}
return null; // Return null if the object is null
} catch (JsonProcessingException e) {
log.error("Error while converting object to string: {}", e.getMessage(), e);
return null; // Return null in case of exception
}
}
public static Object getFieldValueAsObject(String fieldValue) {
ObjectMapper mapper = new ObjectMapper();
try {
// Check if the string is a valid JSON object, array, or simple string
if (fieldValue.startsWith("{")) {
// Convert to a Map (representing an object)
return mapper.readValue(fieldValue, Map.class);
} else if (fieldValue.startsWith("[")) {
// Convert to a List (representing an array)
return mapper.readValue(fieldValue, List.class);
} else {
// Return the raw string (it's a simple value)
return fieldValue;
}
} catch (JsonProcessingException e) {
log.error("Error while converting string to object: {}", e.getMessage(), e);
return fieldValue; // If there's an error, return the raw string
}
}
}