diff --git hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/JsonSerDe.java hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/JsonSerDe.java index 9c87aa3..d27fa76 100644 --- hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/JsonSerDe.java +++ hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/JsonSerDe.java @@ -512,15 +512,20 @@ private static void buildJSONString(StringBuilder sb, Object o, ObjectInspector case DECIMAL: sb.append(((HiveDecimalObjectInspector)poi).getPrimitiveJavaObject(o)); break; - case VARCHAR: - appendWithQuotes(sb, - ((HiveVarcharObjectInspector)poi).getPrimitiveJavaObject(o).toString()); + case VARCHAR: { + String s = SerDeUtils.escapeString( + ((HiveVarcharObjectInspector) poi).getPrimitiveJavaObject(o).toString()); + appendWithQuotes(sb, s); break; - case CHAR: + } + case CHAR: { //this should use HiveChar.getPaddedValue() but it's protected; currently (v0.13) // HiveChar.toString() returns getPaddedValue() - appendWithQuotes(sb, ((HiveCharObjectInspector)poi).getPrimitiveJavaObject(o).toString()); + String s = SerDeUtils.escapeString( + ((HiveCharObjectInspector) poi).getPrimitiveJavaObject(o).toString()); + appendWithQuotes(sb, s); break; + } default: throw new RuntimeException("Unknown primitive type: " + poi.getPrimitiveCategory()); } diff --git hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java index b4a810a..5ba1737 100644 --- hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java +++ hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java @@ -56,7 +56,7 @@ rlist.add(new Long(1000L)); rlist.add(new Double(5.3D)); rlist.add(new Float(2.39F)); - rlist.add(new String("hcat and hadoop")); + rlist.add(new String("hcat\nand\nhadoop")); rlist.add(null); List innerStruct = new ArrayList(2); @@ -94,8 +94,8 @@ c1.add(c1_1); rlist.add(c1); rlist.add(HiveDecimal.create(new BigDecimal("123.45")));//prec 5, scale 2 - rlist.add(new HiveChar("hive_char", 10)); - rlist.add(new HiveVarchar("hive_varchar", 20)); + rlist.add(new HiveChar("hive\nchar", 10)); + rlist.add(new HiveVarchar("hive\nvarchar", 20)); rlist.add(Date.valueOf("2014-01-07")); rlist.add(new Timestamp(System.currentTimeMillis())); @@ -307,4 +307,29 @@ public void testUpperCaseKey() throws Exception { assertTrue(HCatDataCheckUtil.recordsEqual((HCatRecord)rjsd.deserialize(text2), expected2)); } + + public void testEscapedStrings() throws Exception { + Configuration conf = new Configuration(); + Properties props = new Properties(); + + props.put(serdeConstants.LIST_COLUMNS, "empid,name"); + props.put(serdeConstants.LIST_COLUMN_TYPES, "int,string"); + JsonSerDe rjsd = new JsonSerDe(); + SerDeUtils.initializeSerDe(rjsd, conf, props, null); + + Text text1 = new Text("{ \"empId\" : 123, \"name\" : \"John\\tDoe\" } "); + System.out.println("*** as string: " + (HCatRecord)rjsd.deserialize(text1)); + } + public void testEscapedStrings1() throws Exception { + Configuration conf = new Configuration(); + Properties props = new Properties(); + + props.put(serdeConstants.LIST_COLUMNS, "empid,name"); + props.put(serdeConstants.LIST_COLUMN_TYPES, "int,varchar(10)"); + JsonSerDe rjsd = new JsonSerDe(); + SerDeUtils.initializeSerDe(rjsd, conf, props, null); + + Text text1 = new Text("{ \"empId\" : 123, \"name\" : \"John\\tDoe\" } "); + System.out.println("*** as varchar: " + (HCatRecord)rjsd.deserialize(text1)); + } }