
net.sf.aguacate.util.json.impl.JsonCodecImpl Maven / Gradle / Ivy
package net.sf.aguacate.util.json.impl;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.aguacate.util.json.JsonCodec;
public class JsonCodecImpl implements JsonCodec {
private static final ObjectMapper MAPPER;
static {
MAPPER = new ObjectMapper();
}
@Override
public String encode(Object object) {
try {
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public String encode(Map object) {
try {
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public void encode(Writer writer, Object object) throws IOException {
MAPPER.writeValue(writer, object);
}
@SuppressWarnings("unchecked")
@Override
public Map decode(String json) {
try {
return MAPPER.readValue(json, Map.class);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
@SuppressWarnings("unchecked")
@Override
public Map decode(Reader reader) {
try {
return MAPPER.readValue(reader, Map.class);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy