[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [collector/master] Replace Gson with Jackson.
commit 818488836545aaa08bb8ef90e4a2b1d5accf535b
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Tue May 22 15:31:41 2018 +0200
Replace Gson with Jackson.
Implements #26162.
---
CHANGELOG.md | 3 ++-
build.xml | 6 ++++--
.../collector/index/CreateIndexJson.java | 7 +------
.../collector/relaydescs/ReferenceChecker.java | 22 +++++++++++-----------
.../collector/relaydescs/ReferenceCheckerTest.java | 21 +++++++++++++++++++++
5 files changed, 39 insertions(+), 20 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d9cf7da..9c6cb85 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,7 +1,8 @@
# Changes in version 1.6.0 - 2018-0?-??
* Medium changes
- - Update and adapt to metrics-lib 2.3.0.
+ - Update and adapt to metrics-lib 2.4.0.
+ - Replace Gson with Jackson.
# Changes in version 1.5.1 - 2018-03-19
diff --git a/build.xml b/build.xml
index 921ad33..39a46e9 100644
--- a/build.xml
+++ b/build.xml
@@ -11,13 +11,15 @@
<property name="release.version" value="1.5.1-dev" />
<property name="project-main-class" value="org.torproject.collector.Main" />
<property name="name" value="collector"/>
- <property name="metricslibversion" value="2.3.0" />
+ <property name="metricslibversion" value="2.4.0" />
<property name="jarincludes" value="collector.properties logback.xml" />
<patternset id="runtime" >
<include name="commons-codec-1.10.jar"/>
<include name="commons-compress-1.13.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="xz-1.6.jar"/>
<include name="metrics-lib-${metricslibversion}.jar"/>
<include name="logback-core-1.1.9.jar" />
diff --git a/src/main/java/org/torproject/collector/index/CreateIndexJson.java b/src/main/java/org/torproject/collector/index/CreateIndexJson.java
index 49bfb11..5f030eb 100644
--- a/src/main/java/org/torproject/collector/index/CreateIndexJson.java
+++ b/src/main/java/org/torproject/collector/index/CreateIndexJson.java
@@ -12,9 +12,6 @@ import org.torproject.descriptor.index.FileNode;
import org.torproject.descriptor.index.IndexNode;
import org.torproject.descriptor.internal.FileType;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -164,9 +161,7 @@ public class CreateIndexJson extends CollecTorMain {
private void writeIndex(IndexNode indexNode) throws Exception {
indexJsonFile.getParentFile().mkdirs();
- Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
- .create();
- String indexNodeString = gson.toJson(indexNode);
+ String indexNodeString = IndexNode.makeJsonString(indexNode);
for (String filename : new String[] {indexJsonFile.toString(),
indexJsonFile + ".gz", indexJsonFile + ".xz", indexJsonFile + ".bz2"}) {
FileType type = FileType.valueOf(
diff --git a/src/main/java/org/torproject/collector/relaydescs/ReferenceChecker.java b/src/main/java/org/torproject/collector/relaydescs/ReferenceChecker.java
index 43706dd..df0508a 100644
--- a/src/main/java/org/torproject/collector/relaydescs/ReferenceChecker.java
+++ b/src/main/java/org/torproject/collector/relaydescs/ReferenceChecker.java
@@ -14,14 +14,14 @@ import org.torproject.descriptor.RelayNetworkStatusConsensus;
import org.torproject.descriptor.RelayNetworkStatusVote;
import org.torproject.descriptor.ServerDescriptor;
-import com.google.gson.Gson;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.text.DateFormat;
@@ -50,6 +50,10 @@ public class ReferenceChecker {
private SortedSet<Reference> references = new TreeSet<>();
+ private static ObjectMapper objectMapper = new ObjectMapper()
+ .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
+ .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
+
private static DateFormat dateTimeFormat;
static {
@@ -141,10 +145,9 @@ public class ReferenceChecker {
if (!this.referencesFile.exists()) {
return;
}
- Gson gson = new Gson();
- try (FileReader fr = new FileReader(this.referencesFile)) {
- this.references.addAll(Arrays.asList(gson.fromJson(fr,
- Reference[].class)));
+ try {
+ this.references.addAll(Arrays.asList(objectMapper.readValue(
+ this.referencesFile, Reference[].class)));
} catch (IOException e) {
logger.warn("Cannot read existing references file "
+ "from previous run.", e);
@@ -321,11 +324,8 @@ public class ReferenceChecker {
}
private void writeReferencesFile() {
- Gson gson = new Gson();
try {
- FileWriter fw = new FileWriter(this.referencesFile);
- gson.toJson(this.references, fw);
- fw.close();
+ objectMapper.writeValue(this.referencesFile, this.references);
} catch (IOException e) {
logger.warn("Cannot write references file for next "
+ "run.", e);
diff --git a/src/test/java/org/torproject/collector/relaydescs/ReferenceCheckerTest.java b/src/test/java/org/torproject/collector/relaydescs/ReferenceCheckerTest.java
index adb0b48..da2d9ad 100644
--- a/src/test/java/org/torproject/collector/relaydescs/ReferenceCheckerTest.java
+++ b/src/test/java/org/torproject/collector/relaydescs/ReferenceCheckerTest.java
@@ -74,5 +74,26 @@ public class ReferenceCheckerTest {
.get(0));
}
+ @Test()
+ public void testEmptyReferencedString() throws Exception {
+ String validEmptyReferencedString
+ = validReferenceJson.substring(0,
+ validReferenceJson.indexOf("S-D8736"))
+ + validReferenceJson.substring(
+ validReferenceJson.indexOf("\",\"weight"));
+ File descDir = tmpf.newFolder();
+ File refsFile = tmpf.newFile();
+ File histFile = tmpf.newFile();
+ Files.write(refsFile.toPath(), validEmptyReferencedString.getBytes());
+ assertEquals(validEmptyReferencedString,
+ Files.readAllLines(refsFile.toPath(),
+ Charset.forName("US-ASCII")).get(0));
+ ReferenceChecker rc = new ReferenceChecker(descDir, refsFile, histFile);
+ rc.check();
+ assertTrue(refsFile.exists());
+ assertEquals(validEmptyReferencedString,
+ Files.readAllLines(refsFile.toPath(),
+ Charset.forName("US-ASCII")).get(0));
+ }
}
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits