Skip to content

Commit

Permalink
Merge pull request #165 from CDOT-CV/mcook42/hotfix/set-big-decimal-s…
Browse files Browse the repository at this point in the history
…erialization-2

fix: force BigDecimal serialization to NUMBER format
  • Loading branch information
drewjj authored Jan 22, 2025
2 parents a9ed9bb + e089177 commit d200673
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/Release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Enhancements in this release:
- [CDOT PR 154](https://github.com/CDOT-CV/jpo-ode/pull/154): Fix: Use odeKafkaProperties env vars to drive producer retries
- [CDOT PR 158](https://github.com/CDOT-CV/jpo-ode/pull/158): Release/PSM schema fix
- [CDOT PR 160](https://github.com/CDOT-CV/jpo-ode/pull/160): Support Renamed Fields in J2735 2024 BSM Structures
- [CDOT PR 165](https://github.com/CDOT-CV/jpo-ode/pull/165): fix: force BigDecimal serialization to NUMBER format
- [USDOT PR 559](https://github.com/usdot-jpo-ode/jpo-ode/pull/559): Update GitHub Actions Third-Party Action Versions
- [USDOT PR 561](https://github.com/usdot-jpo-ode/jpo-ode/pull/561): Bump ch.qos.logback:logback-core from 1.4.14 to 1.5.13 in /jpo-ode-plugins

Expand Down
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 consistently 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
2 changes: 1 addition & 1 deletion jpo-ode-core/src/main/resources/schemas/schema-map.json
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@
"properties": {
"elevation": {
"type": [
"string"
"number"
]
},
"latitude": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package us.dot.its.jpo.ode.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -9,6 +10,7 @@
import com.fasterxml.jackson.databind.type.LogicalType;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper.Builder;
import java.math.BigDecimal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
Expand All @@ -33,6 +35,8 @@ public ObjectMapper objectMapper() {
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.coercionConfigFor(LogicalType.Enum)
.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
// Ensure BigDecimals are serialized consistently as numbers not strings
mapper.configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
return mapper;
}

Expand Down

0 comments on commit d200673

Please sign in to comment.