-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standardiser JsonMapper for nare + bruk i regelrepos (#138)
- Loading branch information
Showing
7 changed files
with
70 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/no/nav/fpsak/nare/json/NareJsonException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters