Skip to content

Commit 2f778a9

Browse files
refactor: replace ObjectMapper with JsonMapper for improved JSON handling
1 parent 7ce5683 commit 2f778a9

4 files changed

Lines changed: 21 additions & 29 deletions

File tree

app/src/main/java/tools/dynamia/app/controllers/CrudServiceRestController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tools.dynamia.app.controllers;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.json.JsonMapper;
55
import io.swagger.v3.oas.annotations.tags.Tag;
66
import org.springframework.http.ResponseEntity;
77
import org.springframework.web.bind.annotation.*;
@@ -43,7 +43,7 @@ public class CrudServiceRestController {
4343
/**
4444
* JSON object mapper for entity serialization/deserialization.
4545
*/
46-
private final ObjectMapper mapper = StringPojoParser.createJsonMapper();
46+
private final JsonMapper mapper = StringPojoParser.createJsonMapper();
4747

4848
/**
4949
* Constructs a new {@code CrudServiceRestController} with the given CRUD service.

commons/src/main/java/tools/dynamia/commons/StringPojoParser.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.fasterxml.jackson.core.JsonProcessingException;
2121
import com.fasterxml.jackson.core.type.TypeReference;
2222
import com.fasterxml.jackson.databind.JavaType;
23-
import com.fasterxml.jackson.databind.ObjectMapper;
2423
import com.fasterxml.jackson.databind.SerializationFeature;
2524
import com.fasterxml.jackson.databind.json.JsonMapper;
2625
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@@ -53,19 +52,19 @@ public static String convertMapToJson(Map map) {
5352
if (map == null || map.isEmpty()) {
5453
return "";
5554
}
56-
ObjectMapper jsonMapper = createJsonMapper();
55+
var jsonMapper = createJsonMapper();
5756
return jsonMapper.writeValueAsString(map);
5857
} catch (JsonProcessingException e) {
5958
throw new JsonParsingException(e);
6059
}
6160
}
6261

6362
/**
64-
* Creates a configured JSON {@link ObjectMapper} with indentation, disabled empty beans, and JavaTimeModule support.
63+
* Creates a configured JSON {@link JsonMapper} with indentation, disabled empty beans, and JavaTimeModule support.
6564
*
6665
* @return the configured JSON ObjectMapper
6766
*/
68-
public static ObjectMapper createJsonMapper() {
67+
public static JsonMapper createJsonMapper() {
6968
return JsonMapper.builder()
7069
.enable(SerializationFeature.INDENT_OUTPUT)
7170
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
@@ -85,7 +84,7 @@ public static String convertPojoToJson(Object pojo) {
8584
if (pojo == null) {
8685
return "";
8786
}
88-
ObjectMapper jsonMapper = createJsonMapper();
87+
var jsonMapper = createJsonMapper();
8988
return jsonMapper.writeValueAsString(pojo);
9089
} catch (JsonProcessingException e) {
9190
throw new JsonParsingException(e);
@@ -141,7 +140,7 @@ public static <T> T parseJsonToPojo(String json, Class<T> pojoType) {
141140
return null;
142141
}
143142

144-
ObjectMapper jsonMapper = createJsonMapper();
143+
var jsonMapper = createJsonMapper();
145144
return jsonMapper.readerFor(pojoType).readValue(json);
146145
} catch (IOException e) {
147146
throw new JsonParsingException(e);
@@ -161,7 +160,7 @@ public static <T> T parseJsonToPojo(Map map, Class<T> pojoType) {
161160
return null;
162161
}
163162

164-
ObjectMapper jsonMapper = createJsonMapper();
163+
var jsonMapper = createJsonMapper();
165164
return jsonMapper.convertValue(map, pojoType);
166165
} catch (IllegalArgumentException e) {
167166
throw new JsonParsingException(e);
@@ -184,12 +183,12 @@ public static String convertPojoToXml(Object pojo) {
184183
}
185184

186185
/**
187-
* Create a xml {@link ObjectMapper} with enable IDENT_OUTPUT and disabled FAIL_ON_EMPTY_BEANS. Also add support
186+
* Create a xml {@link XmlMapper} with enable IDENT_OUTPUT and disabled FAIL_ON_EMPTY_BEANS. Also add support
188187
* to {@link JavaTimeModule} from JSR310 dependency
189188
*
190-
* @return xml ObjectMapper
189+
* @return xml mapper
191190
*/
192-
public static ObjectMapper createXmlMapper() {
191+
public static XmlMapper createXmlMapper() {
193192
return XmlMapper.builder()
194193
.enable(SerializationFeature.INDENT_OUTPUT)
195194
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
@@ -244,7 +243,7 @@ public static <T> String convertListToJson(List<T> list) {
244243
if (list == null || list.isEmpty()) {
245244
return "";
246245
}
247-
ObjectMapper jsonMapper = createJsonMapper();
246+
var jsonMapper = createJsonMapper();
248247
return jsonMapper.writeValueAsString(list);
249248
} catch (JsonProcessingException e) {
250249
throw new JsonParsingException(e);

viewers/src/main/java/tools/dynamia/viewers/JsonView.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
package tools.dynamia.viewers;
1919

2020
import com.fasterxml.jackson.core.JsonProcessingException;
21-
import com.fasterxml.jackson.databind.ObjectMapper;
22-
import com.fasterxml.jackson.databind.SerializationFeature;
21+
import com.fasterxml.jackson.databind.json.JsonMapper;
2322
import com.fasterxml.jackson.databind.module.SimpleModule;
23+
import tools.dynamia.commons.StringPojoParser;
2424

2525
import java.io.IOException;
2626

@@ -31,7 +31,7 @@ public class JsonView<T> implements View<T> {
3131
private T value;
3232
private View parentView;
3333
private ViewDescriptor viewDescriptor;
34-
private ObjectMapper mapper;
34+
private JsonMapper mapper;
3535

3636
public JsonView() {
3737

@@ -80,7 +80,7 @@ public void setViewDescriptor(ViewDescriptor viewDescriptor) {
8080
public String renderJson() {
8181

8282
try {
83-
ObjectMapper mapper = getObjectMapper();
83+
var mapper = getJsonMapper();
8484
return mapper.writeValueAsString(value);
8585
} catch (JsonProcessingException e) {
8686
throw new ViewRendererException("Exception rendering json view of " + value + " with descriptor " + viewDescriptor, e);
@@ -94,18 +94,16 @@ public void parse(String json) {
9494
}
9595
try {
9696
//noinspection unchecked
97-
value = (T) getObjectMapper().readValue(json, viewDescriptor.getBeanClass());
97+
value = (T) getJsonMapper().readValue(json, viewDescriptor.getBeanClass());
9898
} catch (IOException e) {
9999
throw new ViewRendererException("Error parsing json to object", e);
100100
}
101101

102102
}
103103

104-
private ObjectMapper getObjectMapper() {
104+
private JsonMapper getJsonMapper() {
105105
if (mapper == null) {
106-
mapper = new ObjectMapper();
107-
mapper.enable(SerializationFeature.INDENT_OUTPUT);
108-
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
106+
mapper = StringPojoParser.createJsonMapper();
109107
SimpleModule module = new SimpleModule();
110108
module.addSerializer(viewDescriptor.getBeanClass(), new JsonViewDescriptorSerializer(viewDescriptor));
111109
//noinspection unchecked

web/src/main/java/tools/dynamia/web/navigation/RestNavigationController.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.fasterxml.jackson.annotation.JsonInclude;
2020
import com.fasterxml.jackson.core.JsonProcessingException;
2121
import com.fasterxml.jackson.databind.JsonNode;
22-
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import com.fasterxml.jackson.databind.SerializationFeature;
2422
import jakarta.servlet.http.HttpServletRequest;
2523
import org.springframework.core.annotation.Order;
2624
import org.springframework.http.HttpHeaders;
@@ -276,7 +274,7 @@ private ResponseEntity<String> update(String path, Long id, String jsonData, Htt
276274
}
277275
ViewDescriptor descriptor = getJsonFormDescriptor(entityClass, true);
278276

279-
ObjectMapper mapper = new ObjectMapper();
277+
var mapper = StringPojoParser.createJsonMapper();
280278
try {
281279
final ViewDescriptor desc = descriptor;
282280
JsonNode node = mapper.readTree(jsonData);
@@ -338,10 +336,7 @@ public static int getParameterNumber(HttpServletRequest request, String name) {
338336

339337
public static ResponseEntity<String> getMetadata(HttpServletRequest request, ViewDescriptor viewDescriptor) {
340338
if (viewDescriptor != null && request.getParameter("_metadata") != null) {
341-
ObjectMapper mapper = new ObjectMapper();
342-
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
343-
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
344-
339+
var mapper = StringPojoParser.createJsonMapper();
345340

346341
try {
347342
return new ResponseEntity<>(mapper.writeValueAsString(viewDescriptor), HttpStatus.OK);

0 commit comments

Comments
 (0)