com.verisec.frejaeid.client.util.JsonService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of FrejaEidClient Show documentation
Show all versions of FrejaEidClient Show documentation
Freja eID Client is a client library aimed to ease integration of relying party back-end systems with Freja eID Relying Party API.
package com.verisec.frejaeid.client.util;
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.verisec.frejaeid.client.exceptions.FrejaEidClientInternalException;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
public class JsonService implements Serializable {
private final ObjectMapper mapper = new ObjectMapper();
public JsonService() {
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public String serializeToJson(T jsonSerializable) throws FrejaEidClientInternalException {
return serialize(jsonSerializable);
}
public V deserializeFromJson(byte[] bodyBytes, Class responseType) throws FrejaEidClientInternalException {
return deserialize(bodyBytes, responseType, mapper);
}
private String serialize(T jsonSerializable) throws FrejaEidClientInternalException {
try {
return mapper.writeValueAsString(jsonSerializable);
} catch (JsonProcessingException ex) {
throw new FrejaEidClientInternalException(String.format("Error while serializing %s. ", jsonSerializable), ex);
}
}
private T deserialize(byte[] value, Class extends T> type, ObjectMapper mapper) throws FrejaEidClientInternalException {
try {
return mapper.readValue(value, type);
} catch (IOException ex) {
throw new FrejaEidClientInternalException(String.format("Failed to deserialize value %s into object of class %s", new String(value, StandardCharsets.UTF_8), type.getName()), ex);
}
}
}