avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject [38/43] avro git commit: AVRO-1711: Java: Fix JsonDecoder#skipChildren skipping extra tokens.
Date Sat, 14 May 2016 23:44:11 GMT
AVRO-1711: Java: Fix JsonDecoder#skipChildren skipping extra tokens.

Contributed by Zoltan Farkas and Thiruvalluvan M. G.


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/6de8e985
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/6de8e985
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/6de8e985

Branch: refs/heads/branch-1.8
Commit: 6de8e985675034fbb3cac4dc8bd3cbed31461b33
Parents: b9940d7
Author: Ryan Blue <blue@apache.org>
Authored: Sun May 8 15:26:37 2016 -0700
Committer: Ryan Blue <blue@apache.org>
Committed: Sat May 14 16:43:07 2016 -0700

----------------------------------------------------------------------
 CHANGES.txt                                        |  3 +++
 .../main/java/org/apache/avro/io/JsonDecoder.java  |  9 +++++----
 .../java/org/apache/avro/io/TestJsonDecoder.java   | 17 +++++++++++++++++
 3 files changed, 25 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/6de8e985/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 6c8e042..86f280d 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -57,6 +57,9 @@ Trunk (not yet released)
 
     AVRO-1814: Generated java code fails on variables with a TLD name like 'org' (nielsbasjes)
 
+    AVRO-1711: Java: Fix JsonParser#skipChildren to implement its defined contract.
+    (Zoltan Farkas and Thiruvalluvan M. G. via blue)
+
 Avro 1.8.0 (22 January 2016)
 
   INCOMPATIBLE CHANGES

http://git-wip-us.apache.org/repos/asf/avro/blob/6de8e985/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java b/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java
index 8045199..34a1862 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/JsonDecoder.java
@@ -579,9 +579,10 @@ public class JsonDecoder extends ParsingDecoder
 
       @Override
       public JsonParser skipChildren() throws IOException {
-        int level = 0;
-        do {
-          switch(elements.get(pos++).token) {
+        JsonToken tkn = elements.get(pos).token;
+        int level = (tkn == JsonToken.START_ARRAY || tkn == JsonToken.END_ARRAY) ? 1 : 0;
+        while (level > 0) {
+          switch(elements.get(++pos).token) {
           case START_ARRAY:
           case START_OBJECT:
             level++;
@@ -591,7 +592,7 @@ public class JsonDecoder extends ParsingDecoder
             level--;
             break;
           }
-        } while (level > 0);
+        }
         return this;
       }
 

http://git-wip-us.apache.org/repos/asf/avro/blob/6de8e985/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java b/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java
index 4ac07eb..6ee6fcb 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/io/TestJsonDecoder.java
@@ -60,4 +60,21 @@ public class TestJsonDecoder {
     }
   }
 
+  // Ensure that even if the order of fields in JSON is different from the order in schema,
+  // it works.
+  @Test public void testReorderFields() throws Exception {
+    String w =
+      "{\"type\":\"record\",\"name\":\"R\",\"fields\":"
+      +"[{\"type\":\"long\",\"name\":\"l\"},"
+      +"{\"type\":{\"type\":\"array\",\"items\":\"int\"},\"name\":\"a\"}"
+      +"]}";
+    Schema ws = Schema.parse(w);
+    DecoderFactory df = DecoderFactory.get();
+    String data = "{\"a\":[1,2],\"l\":100}{\"l\": 200, \"a\":[1,2]}";
+    JsonDecoder in = df.jsonDecoder(ws, data);
+    Assert.assertEquals(100, in.readLong());
+    in.skipArray();
+    Assert.assertEquals(200, in.readLong());
+    in.skipArray();
+  }
 }


Mime
View raw message