Commit 52494a7f authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Configure Jackson using class annotations.

parent 747995f9
......@@ -3,6 +3,21 @@
package org.torproject.onionoo.docs;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
/* Use snake_case for naming fields rather than camelCase. */
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
/* Exclude fields that are null or empty. */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
/* Only consider fields, no getters, setters, or constructors. */
@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE,
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE)
public abstract class Document {
private transient String documentString;
......
......@@ -5,12 +5,8 @@ package org.torproject.onionoo.docs;
import org.torproject.onionoo.util.FormattingUtils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -44,11 +40,7 @@ public class DocumentStore {
private static Logger log = LoggerFactory.getLogger(
DocumentStore.class);
private static ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
private static ObjectMapper objectMapper = new ObjectMapper();
private final File statusDir = new File("status");
......
......@@ -14,12 +14,8 @@ import org.torproject.onionoo.docs.UptimeDocument;
import org.torproject.onionoo.docs.WeightsDocument;
import org.torproject.onionoo.util.FormattingUtils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -35,11 +31,7 @@ public class ResponseBuilder {
private static final Logger logger =
LoggerFactory.getLogger(ResponseBuilder.class);
private static ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
private static ObjectMapper objectMapper = new ObjectMapper();
private DocumentStore documentStore;
private String buildRevision;
......
......@@ -14,10 +14,7 @@ import org.torproject.onionoo.docs.DummyDocumentStore;
import org.torproject.onionoo.docs.UpdateStatus;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.junit.Before;
import org.junit.Test;
......@@ -39,11 +36,7 @@ import java.util.TreeSet;
* which tests servlet specifics. */
public class ResourceServletTest {
private static ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
private static ObjectMapper objectMapper = new ObjectMapper();
private SortedMap<String, org.torproject.onionoo.docs.SummaryDocument> relays;
private SortedMap<String, org.torproject.onionoo.docs.SummaryDocument>
......@@ -357,6 +350,7 @@ public class ResourceServletTest {
}
@SuppressWarnings("MemberName")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
private static class SummaryDocument {
private String version;
private String next_major_version_scheduled;
......@@ -372,6 +366,7 @@ public class ResourceServletTest {
}
@SuppressWarnings("MemberName")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
private static class RelaySummary {
private String n;
private String f;
......@@ -380,6 +375,7 @@ public class ResourceServletTest {
}
@SuppressWarnings("MemberName")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
private static class BridgeSummary {
private String n;
private String h;
......
......@@ -9,11 +9,7 @@ import static org.junit.Assert.assertNotNull;
import org.torproject.onionoo.docs.DateTimeHelper;
import org.torproject.onionoo.docs.GraphHistory;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -30,11 +26,7 @@ import java.util.Map;
@RunWith(Parameterized.class)
public class GraphHistoryCompilerTest {
private static ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
private static ObjectMapper objectMapper = new ObjectMapper();
/** Provide test data. */
@Parameters
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment