Author: thiru
Date: Wed Dec 9 07:43:01 2009
New Revision: 888720
URL: http://svn.apache.org/viewvc?rev=888720&view=rev
Log:
AVRO-246 Java schema parser should take schema from InputStream in addition to
file
Modified:
hadoop/avro/trunk/CHANGES.txt
hadoop/avro/trunk/src/java/org/apache/avro/Schema.java
hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java
Modified: hadoop/avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=888720&r1=888719&r2=888720&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Wed Dec 9 07:43:01 2009
@@ -125,6 +125,9 @@
AVRO-247. In reflection, add Stringable annotation to indicate
classes that can be represented by an Avro string. (cutting)
+
+ AVRO-246 Java schema parser should take schema from InputStream
+ in addition to file. (thiru)
OPTIMIZATIONS
Modified: hadoop/avro/trunk/src/java/org/apache/avro/Schema.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/Schema.java?rev=888720&r1=888719&r2=888720&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/Schema.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/Schema.java Wed Dec 9 07:43:01
2009
@@ -19,6 +19,7 @@
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collections;
@@ -680,6 +681,14 @@
public NullSchema() { super(Type.NULL); }
}
+ /**
+ * Constructs a Schema object from JSON schema file <tt>file</tt>.
+ * The contents of <tt>file</tt> is expected to be in UTF-8 format.
+ * @param file The file to read the schema from.
+ * @return The freshly built Schema.
+ * @throws IOException if there was trouble reading the contents
+ * @throws JsonParseException if the contents are invalid
+ */
public static Schema parse(File file) throws IOException {
JsonParser parser = FACTORY.createJsonParser(file);
try {
@@ -689,6 +698,23 @@
}
}
+ /**
+ * Constructs a Schema object from JSON schema stream <tt>in</tt>.
+ * The contents of <tt>in</tt> is expected to be in UTF-8 format.
+ * @param in The input stream to read the schema from.
+ * @return The freshly built Schema.
+ * @throws IOException if there was trouble reading the contents
+ * @throws JsonParseException if the contents are invalid
+ */
+ public static Schema parse(InputStream in) throws IOException {
+ JsonParser parser = FACTORY.createJsonParser(in);
+ try {
+ return Schema.parse(MAPPER.readTree(parser), new Names());
+ } catch (JsonParseException e) {
+ throw new SchemaParseException(e);
+ }
+ }
+
/** Construct a schema from <a href="http://json.org/">JSON</a> text. */
public static Schema parse(String jsonSchema) {
return parse(parseJson(jsonSchema), new Names());
Modified: hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java?rev=888720&r1=888719&r2=888720&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java (original)
+++ hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java Wed Dec 9
07:43:01 2009
@@ -198,6 +198,13 @@
Schema s = Schema.parse(json);
assertEquals(null, s.getProp("foo"));
}
+
+ @Test
+ public void testParseInputStream() throws IOException {
+ Schema s = Schema.parse(
+ new ByteArrayInputStream("\"boolean\"".getBytes("UTF-8")));
+ assertEquals(Schema.parse("\"boolean\""), s);
+ }
private static void checkParseError(String json) {
try {