avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fo...@apache.org
Subject [avro] branch master updated: AVRO-1852: Make Java org.apache.Schema serializable (#589)
Date Fri, 19 Jul 2019 07:48:59 GMT
This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new e19fe14  AVRO-1852: Make Java org.apache.Schema serializable (#589)
e19fe14 is described below

commit e19fe14a3a945539a66b672c74740ede19af6b6e
Author: RyanSkraba <rskraba@talend.com>
AuthorDate: Fri Jul 19 09:48:53 2019 +0200

    AVRO-1852: Make Java org.apache.Schema serializable (#589)
    
    * [add] [AVRO-1852]
    
    * AVRO-1852: Update unit tests with feedback.
---
 .../avro/src/main/java/org/apache/avro/Schema.java | 23 +++++++++++++++++++++-
 .../src/test/java/org/apache/avro/TestSchema.java  | 23 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/Schema.java b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
index d5e8735..c3052ad 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
@@ -29,6 +29,7 @@ import com.fasterxml.jackson.databind.node.NullNode;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Serializable;
 import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -83,7 +84,27 @@ import org.apache.avro.util.internal.JacksonUtils;
  * property.
  * </ul>
  */
-public abstract class Schema extends JsonProperties {
+public abstract class Schema extends JsonProperties implements Serializable {
+
+  private static final long serialVersionUID = 1L;
+
+  protected Object writeReplace() {
+    SerializableSchema ss = new SerializableSchema();
+    ss.schemaString = toString();
+    return ss;
+  }
+
+  private static final class SerializableSchema implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private String schemaString;
+
+    private Object readResolve() {
+      return new Schema.Parser().parse(schemaString);
+    }
+  }
+
   static final JsonFactory FACTORY = new JsonFactory();
   static final ObjectMapper MAPPER = new ObjectMapper(FACTORY);
 
diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
index c0e4cec..43531c7 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
@@ -19,6 +19,12 @@ package org.apache.avro;
 
 import static org.junit.Assert.*;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -177,4 +183,21 @@ public class TestSchema {
     return Schema.createRecord("name", "doc", "namespace", false);
   }
 
+  @Test
+  public void testSerialization() throws IOException, ClassNotFoundException {
+    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(bos);
+        InputStream jsonSchema = getClass().getResourceAsStream("/SchemaBuilder.avsc")) {
+
+      Schema payload = new Schema.Parser().parse(jsonSchema);
+      oos.writeObject(payload);
+
+      try (ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+          ObjectInputStream ois = new ObjectInputStream(bis)) {
+        Schema sp = (Schema) ois.readObject();
+        assertEquals(payload, sp);
+      }
+    }
+  }
+
 }


Mime
View raw message