avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1347779 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java
Date Thu, 07 Jun 2012 20:41:45 GMT
Author: cutting
Date: Thu Jun  7 20:41:44 2012
New Revision: 1347779

URL: http://svn.apache.org/viewvc?rev=1347779&view=rev
Log:
AVRO-1099. Java: Fix JsonDecoder to permit floats and doubles to be read from JSON values
without decimal points, and for ints and longs to be read from JSON values with decimal points.

Added:
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java   (with
props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1347779&r1=1347778&r2=1347779&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Jun  7 20:41:44 2012
@@ -129,6 +129,10 @@ Avro 1.7.0 (28 May 2012)
     AVRO-1102. C: Remove memory leak in avro_string().  (Maxim Pugachev via
     dcreager)
 
+    AVRO-1099. Java: Fix JsonDecoder to permit floats and doubles to
+    be read from JSON values without decimal points, and for ints and
+    longs to be read from JSON values with decimal points. (cutting)
+
 Avro 1.6.3 (5 March 2012)
 
     AVRO-1077. Missing 'inline' for union set function. (thiru)

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java?rev=1347779&r1=1347778&r2=1347779&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java Thu Jun  7
20:41:44 2012
@@ -164,7 +164,7 @@ public class JsonDecoder extends Parsing
   @Override
   public int readInt() throws IOException {
     advance(Symbol.INT);
-    if (in.getCurrentToken() == JsonToken.VALUE_NUMBER_INT) {
+    if (in.getCurrentToken().isNumeric()) {
       int result = in.getIntValue();
       in.nextToken();
       return result;
@@ -176,7 +176,7 @@ public class JsonDecoder extends Parsing
   @Override
   public long readLong() throws IOException {
     advance(Symbol.LONG);
-    if (in.getCurrentToken() == JsonToken.VALUE_NUMBER_INT) {
+    if (in.getCurrentToken().isNumeric()) {
       long result = in.getLongValue();
       in.nextToken();
       return result;
@@ -188,7 +188,7 @@ public class JsonDecoder extends Parsing
   @Override
   public float readFloat() throws IOException {
     advance(Symbol.FLOAT);
-    if (in.getCurrentToken() == JsonToken.VALUE_NUMBER_FLOAT) {
+    if (in.getCurrentToken().isNumeric()) {
       float result = in.getFloatValue();
       in.nextToken();
       return result;
@@ -200,7 +200,7 @@ public class JsonDecoder extends Parsing
   @Override
   public double readDouble() throws IOException {
     advance(Symbol.DOUBLE);
-    if (in.getCurrentToken() == JsonToken.VALUE_NUMBER_FLOAT) {
+    if (in.getCurrentToken().isNumeric()) {
       double result = in.getDoubleValue();
       in.nextToken();
       return result;

Added: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java?rev=1347779&view=auto
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java (added)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java Thu Jun
 7 20:41:44 2012
@@ -0,0 +1,63 @@
+/**
+ * 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.avro.io;
+
+import org.apache.avro.Schema;
+import org.apache.avro.Schema.Parser;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.GenericDatumReader;
+
+import org.junit.Test;
+import org.junit.Assert;
+
+public class TestJsonDecoder {
+  
+  @Test public void testInt() throws Exception {
+    checkNumeric("int", 1);
+  }
+
+  @Test public void testLong() throws Exception {
+    checkNumeric("long", 1L);
+  }
+
+  @Test public void testFloat() throws Exception {
+    checkNumeric("float", 1.0F);
+  }
+
+  @Test public void testDouble() throws Exception {
+    checkNumeric("double", 1.0);
+  }
+
+  private void checkNumeric(String type, Object value) throws Exception {
+    String def = 
+      "{\"type\":\"record\",\"name\":\"X\",\"fields\":"
+      +"[{\"type\":\""+type+"\",\"name\":\"n\"}]}";
+    Schema schema = Schema.parse(def);
+    DatumReader<GenericRecord> reader =
+      new GenericDatumReader<GenericRecord>(schema);
+
+    String[] records = {"{\"n\":1}", "{\"n\":1.0}"};
+
+    for (String record : records) {
+      Decoder decoder = DecoderFactory.get().jsonDecoder(schema, record);
+      GenericRecord r = reader.read(null, decoder);
+      Assert.assertEquals(value, r.get("n"));
+    }
+  }
+
+}

Propchange: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message