Skip to content

Commit

Permalink
fix: force BigDecimal serialization to NUMBER format
Browse files Browse the repository at this point in the history
  • Loading branch information
mcook42 committed Jan 22, 2025
1 parent a9ed9bb commit 67d3322
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package us.dot.its.jpo.ode.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -27,15 +28,13 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.LogicalType;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;


import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

@Slf4j
public class JsonUtils {
Expand Down Expand Up @@ -65,6 +64,10 @@ private JsonUtils() {
mapper_noNulls = new ObjectMapper();
mapper_noNulls.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper_noNulls.setSerializationInclusion(Include.NON_NULL);

// Ensure BigDecimals are serialized constistently as numbers not strings
mapper.configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
mapper_noNulls.configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
}

public static String toJson(Object o, boolean verbose) {
Expand Down

0 comments on commit 67d3322

Please sign in to comment.