Commit 205d6da1 authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Replace Gson with Jackson.

Note that a side-effect of this change is that the generated news.json
uses slightly different formatting. These are just cosmetic changes
that don't have any effect on provided website content.

Implements #26169.
parent 18280f6b
......@@ -9,7 +9,7 @@
<property name="javadoc-title" value="MetricsWeb API Documentation"/>
<property name="implementation-title" value="metrics-web" />
<property name="release.version" value="1.0.3-dev" />
<property name="metricslibversion" value="2.2.0" />
<property name="metricslibversion" value="2.4.0" />
<property name="jetty.version" value="-9.2.21.v20170120" />
<property name="warfile"
value="metrics-web-${release.version}.war"/>
......@@ -44,7 +44,9 @@
<patternset id="common" >
<include name="commons-codec-1.10.jar"/>
<include name="commons-lang3-3.5.jar"/>
<include name="gson-2.4.jar" />
<include name="jackson-annotations-2.8.6.jar"/>
<include name="jackson-core-2.8.6.jar"/>
<include name="jackson-databind-2.8.6.jar"/>
<include name="logback-core-1.1.9.jar" />
<include name="logback-classic-1.1.9.jar" />
<include name="REngine.jar"/>
......
......@@ -3,9 +3,13 @@
package org.torproject.metrics.web;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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 java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -13,7 +17,18 @@ import java.util.List;
public class ContentProvider {
private static ContentProvider instance = new ContentProvider();
private static ObjectMapper objectMapper;
private static ContentProvider instance;
static {
objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
instance = new ContentProvider();
}
public static ContentProvider getInstance() {
return ContentProvider.instance;
......@@ -26,17 +41,24 @@ public class ContentProvider {
private List<News> newsList;
private ContentProvider() {
Gson gson = new GsonBuilder().create();
this.metricsList = Arrays.asList(gson.fromJson(new InputStreamReader(
this.getClass().getClassLoader()
.getResourceAsStream("WEB-INF/json/metrics.json")),
Metric[].class));
this.categoriesList = Arrays.asList(gson.fromJson(new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream(
"WEB-INF/json/categories.json")), Category[].class));
this.newsList = Arrays.asList(gson.fromJson(new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream(
"WEB-INF/json/news.json")), News[].class));
try {
this.metricsList = Arrays.asList(objectMapper.readValue(
new InputStreamReader(this.getClass().getClassLoader()
.getResourceAsStream("WEB-INF/json/metrics.json")),
Metric[].class));
this.categoriesList = Arrays.asList(objectMapper.readValue(
new InputStreamReader(this.getClass().getClassLoader()
.getResourceAsStream("WEB-INF/json/categories.json")),
Category[].class));
this.newsList = Arrays.asList(objectMapper.readValue(
new InputStreamReader(this.getClass().getClassLoader()
.getResourceAsStream("WEB-INF/json/news.json")),
News[].class));
} catch (IOException e) {
/* Abort the web server start rather than continuing with broken provided
* JSON content files. */
throw new RuntimeException(e);
}
}
public List<Metric> getMetricsList() {
......
......@@ -3,9 +3,6 @@
package org.torproject.metrics.web;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@SuppressWarnings("checkstyle:membername")
public class Metric {
......@@ -29,8 +26,6 @@ public class Metric {
private String[] data_column_spec;
@Expose
@SerializedName("include_related_events")
private boolean includeRelatedEvents = false;
public String getId() {
......
......@@ -10,7 +10,7 @@ import java.util.TreeMap;
public class News {
public class Link {
public static class Link {
String label;
String target;
......
package org.torproject.metrics.web;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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 java.io.BufferedReader;
import java.io.FileWriter;
......@@ -16,10 +19,6 @@ public class UpdateNews {
URL textFile = new URL(
"https://trac.torproject.org/projects/tor/wiki/doc/"
+ "MetricsTimeline?format=txt");
Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.setPrettyPrinting()
.create();
List<News> news = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(
textFile.openStream()))) {
......@@ -123,8 +122,15 @@ public class UpdateNews {
news.add(entry);
}
}
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);
String newsString = objectMapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(news);
try (FileWriter fw = new FileWriter(args[0])) {
fw.write(gson.toJson(news));
fw.write(newsString);
}
}
}
This diff is collapsed.
Markdown is supported
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