resolved conflicts
This commit is contained in:
@@ -233,4 +233,47 @@ public class Utils {
|
||||
return data.substring(data.length() - range);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user