diff --git a/src/main/java/no/nav/fpsak/nare/doc/BasicRuleDescription.java b/src/main/java/no/nav/fpsak/nare/doc/BasicRuleDescription.java index a426b66..4a17a63 100644 --- a/src/main/java/no/nav/fpsak/nare/doc/BasicRuleDescription.java +++ b/src/main/java/no/nav/fpsak/nare/doc/BasicRuleDescription.java @@ -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 { diff --git a/src/main/java/no/nav/fpsak/nare/doc/JsonOutput.java b/src/main/java/no/nav/fpsak/nare/doc/JsonOutput.java deleted file mode 100644 index fe1e145..0000000 --- a/src/main/java/no/nav/fpsak/nare/doc/JsonOutput.java +++ /dev/null @@ -1,56 +0,0 @@ -package no.nav.fpsak.nare.doc; - -import java.io.IOException; -import java.io.StringWriter; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; -import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; - -public class JsonOutput { - private static final ObjectMapper OM; - static { - OM = new ObjectMapper(); - - OM.registerModule(new JavaTimeModule()); - OM.registerModule(new Jdk8Module()); - SimpleModule module = new SimpleModule(); - - OM.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - OM.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - module.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE)); - module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); - - // TODO: Mangler Deserializer for LocalDateTimeline og LocalDateInterval - - OM.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); - OM.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); - OM.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); - - OM.registerModule(module); - - } - - public static String asJson(Object obj) { - - StringWriter sw = new StringWriter(1000); - - try { - OM.writerWithDefaultPrettyPrinter().writeValue(sw, obj); - } catch (IOException e) { - throw new IllegalArgumentException(String.format("Kunne ikke serialiseres til json: %s", obj), e); - } - return sw.toString(); - } -} diff --git a/src/main/java/no/nav/fpsak/nare/doc/RuleDescriptionDigraph.java b/src/main/java/no/nav/fpsak/nare/doc/RuleDescriptionDigraph.java index 20b8940..eb81645 100644 --- a/src/main/java/no/nav/fpsak/nare/doc/RuleDescriptionDigraph.java +++ b/src/main/java/no/nav/fpsak/nare/doc/RuleDescriptionDigraph.java @@ -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 { diff --git a/src/main/java/no/nav/fpsak/nare/json/JsonOutput.java b/src/main/java/no/nav/fpsak/nare/json/JsonOutput.java new file mode 100644 index 0000000..0a92c2f --- /dev/null +++ b/src/main/java/no/nav/fpsak/nare/json/JsonOutput.java @@ -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; + } +} diff --git a/src/main/java/no/nav/fpsak/nare/json/NareJsonException.java b/src/main/java/no/nav/fpsak/nare/json/NareJsonException.java new file mode 100644 index 0000000..4e6ed94 --- /dev/null +++ b/src/main/java/no/nav/fpsak/nare/json/NareJsonException.java @@ -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); + } + +} diff --git a/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java b/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java index 8188912..f26a50b 100644 --- a/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java +++ b/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java @@ -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 implements Specification { private String beskrivelse = ""; diff --git a/src/main/java/no/nav/fpsak/nare/specification/ConditionalOrSpecification.java b/src/main/java/no/nav/fpsak/nare/specification/ConditionalOrSpecification.java index 94b981b..bd5ff98 100644 --- a/src/main/java/no/nav/fpsak/nare/specification/ConditionalOrSpecification.java +++ b/src/main/java/no/nav/fpsak/nare/specification/ConditionalOrSpecification.java @@ -8,7 +8,6 @@ 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; @@ -16,6 +15,7 @@ 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