Replace Gson with Jackson.

Implements #26162.
This commit is contained in:
Karsten Loesing 2018-05-22 15:31:41 +02:00
parent 574a3ec4c6
commit 8184888365
5 changed files with 39 additions and 20 deletions

View File

@ -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

View File

@ -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" />

View File

@ -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(

View File

@ -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);

View File

@ -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));
}
}