avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [avro] branch master updated: Fix map with no string keys toString method
Date Thu, 08 Nov 2018 16:55:40 GMT
This is an automated email from the ASF dual-hosted git repository.

dkulp 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 561a811  Fix map with no string keys toString method
561a811 is described below

commit 561a811f671a16139403741f707da6bfdf23997c
Author: trompa <trompa@gmail.com>
AuthorDate: Wed Aug 22 15:33:48 2018 +0200

    Fix map with no string keys toString method
---
 .../java/org/apache/avro/generic/GenericData.java  |  5 ++-
 .../org/apache/avro/generic/TestGenericData.java   | 46 ++++++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
index ba538d2..714f4e8 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
@@ -540,8 +540,9 @@ public class GenericData {
       @SuppressWarnings(value="unchecked")
       Map<Object,Object> map = (Map<Object,Object>)datum;
       for (Map.Entry<Object,Object> entry : map.entrySet()) {
-        toString(entry.getKey(), buffer, seenObjects);
-        buffer.append(": ");
+        buffer.append("\"");
+        buffer.append(String.valueOf(entry.getKey()));
+        buffer.append("\": ");
         toString(entry.getValue(), buffer, seenObjects);
         if (++count < map.size())
           buffer.append(", ");
diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
index ac896a0..a644916 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
@@ -316,6 +316,52 @@ public class TestGenericData {
     mapper.readTree(parser);
   }
 
+  @Test
+  public void testMapWithNonStringKeyToStringIsJson() throws Exception {
+    Schema intMapSchema = new Schema.Parser().parse("{\"type\": \"map\", \"values\": \"string\",
\"java-key-class\" : \"java.lang.Integer\"}");
+    Field intMapField = new Field("intMap", Schema.createMap(intMapSchema), null, null);
+    Schema decMapSchema = new Schema.Parser().parse("{\"type\": \"map\", \"values\": \"string\",
\"java-key-class\" : \"java.math.BigDecimal\"}");
+    Field decMapField = new Field("decMap", Schema.createMap(decMapSchema), null, null);
+    Schema boolMapSchema = new Schema.Parser().parse("{\"type\": \"map\", \"values\": \"string\",
\"java-key-class\" : \"java.lang.Boolean\"}");
+    Field boolMapField = new Field("boolMap", Schema.createMap(decMapSchema), null, null);
+    Schema fileMapSchema = new Schema.Parser().parse("{\"type\": \"map\", \"values\": \"string\",
\"java-key-class\" : \"java.io.File\"}");
+    Field fileMapField = new Field("fileMap", Schema.createMap(decMapSchema), null, null);
+    Schema schema = Schema.createRecord("my_record", "doc", "mytest", false);
+    schema.setFields(Arrays.asList(intMapField,decMapField,boolMapField,fileMapField));
+
+    HashMap<Integer, String> intPair =  new HashMap<>();
+    intPair.put(1, "one");
+    intPair.put(2, "two");
+
+    HashMap<java.math.BigDecimal, String> decPair =  new HashMap<>();
+    decPair.put(java.math.BigDecimal.valueOf(1), "one");
+    decPair.put(java.math.BigDecimal.valueOf(2), "two");
+
+    HashMap<Boolean, String> boolPair =  new HashMap<>();
+    boolPair.put(true, "isTrue");
+    boolPair.put(false, "isFalse");
+    boolPair.put(null, null);
+
+    HashMap<java.io.File, String> filePair =  new HashMap<>();
+    java.io.File f = new java.io.File( getClass().getResource("/SchemaBuilder.avsc").toURI()
);
+    filePair.put(f, "File");
+
+    GenericRecord r = new GenericData.Record(schema);
+    r.put(intMapField.name(), intPair);
+    r.put(decMapField.name(), decPair);
+    r.put(boolMapField.name(), boolPair);
+    r.put(fileMapField.name(), filePair);
+
+    String json = r.toString();
+    System.out.println(json);
+    JsonFactory factory = new JsonFactory();
+    JsonParser parser = factory.createJsonParser(json);
+    ObjectMapper mapper = new ObjectMapper();
+
+    // will throw exception if string is not parsable json
+    mapper.readTree(parser);
+  }
+
   @Test public void testToStringEscapesControlCharsInBytes() throws Exception {
     GenericData data = GenericData.get();
     ByteBuffer bytes = ByteBuffer.wrap(new byte[] {'a', '\n', 'b'});


Mime
View raw message