Skip to content

Commit

Permalink
fix: Safely serialize JSON map [DHIS2-18253] [2.40] (#18875)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Oct 22, 2024
1 parent a420343 commit 15457f5
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -58,16 +59,18 @@ public String convertObjectToJson(Object object) {
Map<String, AttributeValue> attrValueMap = new HashMap<>();

for (AttributeValue attributeValue : attributeValues) {
if (attributeValue.getAttribute() != null) {
attributeValue.setAttribute(new Attribute(attributeValue.getAttribute().getUid()));
attrValueMap.put(attributeValue.getAttribute().getUid(), attributeValue);
if (attributeValue.getAttribute() != null
&& attributeValue.getAttribute().getUid() != null) {
String uid = attributeValue.getAttribute().getUid();
attributeValue.setAttribute(new Attribute(uid));
attrValueMap.put(uid, attributeValue);
}
}

return writer.writeValueAsString(attrValueMap);

} catch (IOException e) {
throw new RuntimeException(e);
} catch (IOException ex) {
throw new UncheckedIOException("Failed to serialize JSON", ex);
}
}

Expand All @@ -83,8 +86,8 @@ public Object convertJsonToObject(String content) {
Map<String, AttributeValue> data = reader.readValue(content);

return convertAttributeValueMapIntoSet(data);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (IOException ex) {
throw new UncheckedIOException("Failed to deserialize JSON", ex);
}
}

Expand Down

0 comments on commit 15457f5

Please sign in to comment.