
com.maxmind.geoip2.model.AbstractResponse Maven / Gradle / Ivy
package com.maxmind.geoip2.model;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public abstract class AbstractResponse {
/**
* @return JSON representation of this object. The structure is the same as
* the JSON provided by the GeoIP2 web service.
* @throws IOException if there is an error serializing the object to JSON.
*/
public String toJson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);
mapper.configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, false);
return mapper.writeValueAsString(this);
}
@Override
public String toString() {
// This exception should never happen. If it does happen, we did
// something wrong.
try {
return getClass().getName() + " [ " + toJson() + " ]";
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy