Update api json mapping to include null & empty values

This commit is contained in:
2023-05-25 10:22:04 -05:00
parent 76b102b811
commit 515e04ecfe
4 changed files with 72 additions and 12 deletions

View File

@ -28,6 +28,7 @@ import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
@ -62,11 +63,29 @@ public class JsonUtils
**
*******************************************************************************/
public static String toJson(Object object)
{
return (toJson(object, null));
}
/*******************************************************************************
** Serialize any object into a JSON String - with customizations on the Jackson
** ObjectMapper.
**
** Internally using jackson - so jackson annotations apply!
**
*******************************************************************************/
public static String toJson(Object object, Consumer<ObjectMapper> objectMapperCustomizer)
{
try
{
ObjectMapper mapper = newObjectMapper();
String jsonResult = mapper.writeValueAsString(object);
ObjectMapper mapper = newObjectMapper();
if(objectMapperCustomizer != null)
{
objectMapperCustomizer.accept(mapper);
}
String jsonResult = mapper.writeValueAsString(object);
return (jsonResult);
}
catch(JsonProcessingException e)