Skip to content

Commit

Permalink
Standardiser JsonMapper for nare + bruk i regelrepos (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolarsen authored Feb 9, 2023
1 parent 5c45398 commit b580122
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import no.nav.fpsak.nare.evaluation.Operator;
import no.nav.fpsak.nare.json.JsonOutput;

@JsonInclude(Include.NON_NULL)
public class BasicRuleDescription implements RuleDescription {
Expand Down
56 changes: 0 additions & 56 deletions src/main/java/no/nav/fpsak/nare/doc/JsonOutput.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.Version;

import no.nav.fpsak.nare.evaluation.Operator;
import no.nav.fpsak.nare.json.JsonOutput;

@JsonIgnoreProperties(ignoreUnknown = true)
public class RuleDescriptionDigraph {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/no/nav/fpsak/nare/json/JsonOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package no.nav.fpsak.nare.json;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class JsonOutput {

private static final ObjectMapper MAPPER = createObjectMapper();


private JsonOutput() {
}

public static String asJson(Object obj) {
try {
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw mapException(obj, e);
}
}

public static String asCompactJson(Object obj) {
try {
return MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw mapException(obj, e);
}
}

private static NareJsonException mapException(Object obj, JsonProcessingException e) {
return new NareJsonException(String.format("Kunne ikke serialiseres til json: %s", obj), e);
}

private static ObjectMapper createObjectMapper() {
var om = new ObjectMapper();
om.registerModule(new JavaTimeModule());
om.registerModule(new Jdk8Module());
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
om.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
om.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
om.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
om.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
om.setVisibility(PropertyAccessor.CREATOR, Visibility.ANY);
return om;
}
}
13 changes: 13 additions & 0 deletions src/main/java/no/nav/fpsak/nare/json/NareJsonException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package no.nav.fpsak.nare.json;

public class NareJsonException extends RuntimeException {

public NareJsonException(String message) {
super(message);
}

public NareJsonException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.util.Map;
import java.util.Optional;

import no.nav.fpsak.nare.doc.JsonOutput;
import no.nav.fpsak.nare.doc.RuleDescription;
import no.nav.fpsak.nare.evaluation.Operator;
import no.nav.fpsak.nare.evaluation.Resultat;
import no.nav.fpsak.nare.evaluation.RuleReasonRef;
import no.nav.fpsak.nare.evaluation.node.SingleEvaluation;
import no.nav.fpsak.nare.json.JsonOutput;

public abstract class AbstractSpecification<T> implements Specification<T> {
private String beskrivelse = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import java.util.stream.Collectors;

import no.nav.fpsak.nare.doc.BasicRuleDescription;
import no.nav.fpsak.nare.doc.JsonOutput;
import no.nav.fpsak.nare.doc.RuleDescription;
import no.nav.fpsak.nare.evaluation.Evaluation;
import no.nav.fpsak.nare.evaluation.Operator;
import no.nav.fpsak.nare.evaluation.Resultat;
import no.nav.fpsak.nare.evaluation.RuleReasonRef;
import no.nav.fpsak.nare.evaluation.node.ConditionalElseEvaluation;
import no.nav.fpsak.nare.evaluation.node.ConditionalOrEvaluation;
import no.nav.fpsak.nare.json.JsonOutput;

/**
* Conditional Or specification, used to create a new specifcation that is the
Expand Down

0 comments on commit b580122

Please sign in to comment.