Repository: incubator-pirk Updated Branches: refs/heads/master 697913a06 -> 5ebe1bf2f
[PIRK-41]: Add JSON Serialization for Java Objects -- closes apache/incubator-pirk#42 Project: http://git-wip-us.apache.org/repos/asf/incubator-pirk/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-pirk/commit/5ebe1bf2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-pirk/tree/5ebe1bf2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-pirk/diff/5ebe1bf2 Branch: refs/heads/master Commit: 5ebe1bf2fddbd0ff9e36016bc49afb4ba33ab09d Parents: 697913a Author: smarthi <[email protected]> Authored: Mon Aug 1 19:43:08 2016 -0400 Committer: eawilliams <[email protected]> Committed: Mon Aug 1 19:43:08 2016 -0400 ---------------------------------------------------------------------- pom.xml | 8 ++ .../pirk/serialization/JavaSerializer.java | 15 +-- .../pirk/serialization/JsonSerializer.java | 12 +- .../serialization/LocalFileSystemStore.java | 6 +- .../pirk/serialization/SerializationTest.java | 134 +++++++++++++++++++ 5 files changed, 159 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-pirk/blob/5ebe1bf2/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 11450b3..a14cbbd 100644 --- a/pom.xml +++ b/pom.xml @@ -101,6 +101,7 @@ <elasticsearch.version>2.3.3</elasticsearch.version> <pirk.forkCount>1C</pirk.forkCount> <pirk.reuseForks>true</pirk.reuseForks> + <jackson.version>2.8.1</jackson.version> </properties> <dependencies> @@ -257,6 +258,13 @@ <version>2.6.2</version> </dependency> + <!-- Jackson dependency --> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-core</artifactId> + <version>${jackson.version}</version> + </dependency> + </dependencies> <build> http://git-wip-us.apache.org/repos/asf/incubator-pirk/blob/5ebe1bf2/src/main/java/org/apache/pirk/serialization/JavaSerializer.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/pirk/serialization/JavaSerializer.java b/src/main/java/org/apache/pirk/serialization/JavaSerializer.java index 27c3537..695ea6e 100644 --- a/src/main/java/org/apache/pirk/serialization/JavaSerializer.java +++ b/src/main/java/org/apache/pirk/serialization/JavaSerializer.java @@ -30,7 +30,7 @@ public class JavaSerializer extends SerializationService /** * Stores the given object on the given stream using Java serialization. * - * @param stream + * @param outputStream * The stream on which to store the object. * @param obj * The object to be stored. @@ -38,27 +38,26 @@ public class JavaSerializer extends SerializationService * If a problem occurs storing the object on the given stream. */ - public void write(OutputStream stream, Storable obj) throws IOException + public void write(OutputStream outputStream, Storable obj) throws IOException { - ObjectOutputStream oos = new ObjectOutputStream(stream); + ObjectOutputStream oos = new ObjectOutputStream(outputStream); oos.writeObject(obj); } /** * Read an object from the given stream of the given type. * - * @param stream + * @param inputStream * The stream from which to read the object. - * @param type + * @param classType * The type of object being retrieved. * @throws IOException * If a problem occurs reading the object from the stream. */ @SuppressWarnings("unchecked") - public <T> T read(InputStream stream, Class<T> type) throws IOException + public <T> T read(InputStream inputStream, Class<T> classType) throws IOException { - ObjectInputStream oin = new ObjectInputStream(stream); - try + try (ObjectInputStream oin = new ObjectInputStream(inputStream)) { return (T) oin.readObject(); } catch (ClassNotFoundException e) http://git-wip-us.apache.org/repos/asf/incubator-pirk/blob/5ebe1bf2/src/main/java/org/apache/pirk/serialization/JsonSerializer.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/pirk/serialization/JsonSerializer.java b/src/main/java/org/apache/pirk/serialization/JsonSerializer.java index c33366d..704a5cb 100644 --- a/src/main/java/org/apache/pirk/serialization/JsonSerializer.java +++ b/src/main/java/org/apache/pirk/serialization/JsonSerializer.java @@ -22,20 +22,22 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -//TODO: Waiting for Jackson adoption +import com.fasterxml.jackson.databind.ObjectMapper; + public class JsonSerializer extends SerializationService { + private ObjectMapper objectMapper = new ObjectMapper(); @Override - public void write(OutputStream w, Storable obj) throws IOException + public void write(OutputStream outputStream, Storable obj) throws IOException { - throw new RuntimeException("Not yet implemented"); + objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputStream, obj); } @Override - public <T> T read(InputStream stream, Class<T> type) throws IOException + public <T> T read(InputStream inputStream, Class<T> classType) throws IOException { - throw new RuntimeException("Not yet implemented"); + return objectMapper.readValue(inputStream, classType); } } http://git-wip-us.apache.org/repos/asf/incubator-pirk/blob/5ebe1bf2/src/main/java/org/apache/pirk/serialization/LocalFileSystemStore.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/pirk/serialization/LocalFileSystemStore.java b/src/main/java/org/apache/pirk/serialization/LocalFileSystemStore.java index cdc4d84..ac9cf2c 100644 --- a/src/main/java/org/apache/pirk/serialization/LocalFileSystemStore.java +++ b/src/main/java/org/apache/pirk/serialization/LocalFileSystemStore.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -15,7 +15,7 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - *******************************************************************************/ + */ package org.apache.pirk.serialization; import java.io.File; @@ -59,7 +59,7 @@ public class LocalFileSystemStore extends StorageService /** * Stores the given object at the given file location. The object is serialized using the configured serializer. * - * @param path + * @param file * The local file system location to store the object. * @param obj * The object to store. http://git-wip-us.apache.org/repos/asf/incubator-pirk/blob/5ebe1bf2/src/test/java/org/apache/pirk/serialization/SerializationTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/pirk/serialization/SerializationTest.java b/src/test/java/org/apache/pirk/serialization/SerializationTest.java new file mode 100644 index 0000000..8689d43 --- /dev/null +++ b/src/test/java/org/apache/pirk/serialization/SerializationTest.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pirk.serialization; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.Serializable; +import java.util.Objects; +import java.util.Random; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class SerializationTest +{ + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private static JsonSerializer jsonSerializer; + private static JavaSerializer javaSerializer; + + @BeforeClass + public static void setUp() throws Exception + { + jsonSerializer = new JsonSerializer(); + javaSerializer = new JavaSerializer(); + } + + @Test + public void testJsonSerDe() throws Exception + { + File tempFile = folder.newFile("test-json-serialize"); + FileOutputStream fos = new FileOutputStream(tempFile); + DummyRecord dummyRecord = new DummyRecord(); + + jsonSerializer.write(fos, dummyRecord); + + FileInputStream fis = new FileInputStream(tempFile); + Object deserializedDummyObject = jsonSerializer.read(fis, DummyRecord.class); + Assert.assertEquals(dummyRecord, deserializedDummyObject); + } + + @Test + public void testJavaSerDe() throws Exception + { + File tempFile = folder.newFile("test-java-serialize"); + FileOutputStream fos = new FileOutputStream(tempFile); + DummyRecord dummyRecord = new DummyRecord(); + + javaSerializer.write(fos, new DummyRecord()); + + FileInputStream fis = new FileInputStream(tempFile); + Object deserializedDummyObject = javaSerializer.read(fis, DummyRecord.class); + Assert.assertTrue(deserializedDummyObject.equals(dummyRecord)); + } + + private static class DummyRecord implements Serializable, Storable + { + private int id; + private String message; + private long seed = 100L; + + DummyRecord() + { + this.id = (new Random(seed)).nextInt(5); + this.message = "The next message id is " + id; + } + + public int getId() + { + return id; + } + + public void setId(int id) + { + this.id = id; + } + + public String getMessage() + { + return message; + } + + public void setMessage(String message) + { + this.message = message; + } + + @Override + public String toString() + { + return "DummyRecord{" + "id=" + id + ", message='" + message + '\'' + '}'; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + DummyRecord that = (DummyRecord) o; + return id == that.id && Objects.equals(message, that.message); + } + + @Override + public int hashCode() + { + return Objects.hash(id, message); + } + } + +}
