Skip to content

Commit

Permalink
avniproject/avni-webapp#1263 | Use object mapper to serialize ValueUn…
Browse files Browse the repository at this point in the history
…it to JSON

Propagate error if occurs while (de)serializing ValueUnit
  • Loading branch information
1t5j0y committed Jul 2, 2024
1 parent 76329ca commit 51707e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.avni.server.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.avni.server.dao.*;
import org.avni.server.domain.*;
import org.avni.server.util.BadRequestError;
Expand Down Expand Up @@ -134,11 +133,11 @@ private void buildCard(ReportCardContract reportCardRequest, ReportCard card) {
public ValueUnit buildDurationForRecentTypeCards(String recentDurationString) {
if (recentDurationString == null) return null;
try {
ObjectMapper objectMapper = ObjectMapperSingleton.getObjectMapper();
return objectMapper.readValue(recentDurationString, ValueUnit.class);
return ObjectMapperSingleton.getObjectMapper().readValue(recentDurationString, ValueUnit.class);
} catch (JsonProcessingException e) {
return null;
throw new RuntimeException(e);
}

}

private void assertNewNameIsUnique(String newName, String oldName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.avni.server.web.contract;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.avni.server.util.ObjectMapperSingleton;

import java.io.Serializable;
import java.util.LinkedHashMap;

public class ValueUnit implements Serializable {
private String value;
Expand All @@ -16,11 +18,6 @@ public ValueUnit(String value, String unit) {
this.unit = unit;
}

public ValueUnit(LinkedHashMap<String, String> valueUnit) {
this.value = valueUnit.get("value");
this.unit = valueUnit.get("unit");
}

public String getValue() {
return value;
}
Expand All @@ -38,6 +35,10 @@ public void setUnit(String unit) {
}

public String toJSONString() {
return String.format("{\"%s\":\"%s\", \"%s\":\"%s\"}", "value", value, "unit", unit);
try {
return ObjectMapperSingleton.getObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 51707e5

Please sign in to comment.