sarutak commented on a change in pull request #32147:
URL: https://github.com/apache/spark/pull/32147#discussion_r614495771



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/json/JsonSuite.scala
##########
@@ -2844,6 +2844,25 @@ abstract class JsonSuite
       assert(readback.collect sameElements Array(Row(0), Row(1), Row(2)))
     }
   }
+
+  test("Write Non-ASCII character as codepoint") {

Review comment:
       I didn't add the JIRA ID because this is an improvement but I don't mind 
to add it.

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/json/JSONOptions.scala
##########
@@ -135,6 +135,9 @@ private[sql] class JSONOptions(
    */
   val inferTimestamp: Boolean = 
parameters.get("inferTimestamp").map(_.toBoolean).getOrElse(false)
 
+  val writeNonAsciiCharacterAsCodePoint: Boolean =

Review comment:
       O.K, I'll add description

##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/json/JsonSuite.scala
##########
@@ -2844,6 +2844,25 @@ abstract class JsonSuite
       assert(readback.collect sameElements Array(Row(0), Row(1), Row(2)))
     }
   }
+
+  test("Write Non-ASCII character as codepoint") {
+    // scalastyle:off nonascii
+    withTempPath { path =>
+      val basePath = path.getCanonicalPath
+      Seq("a", "\n", "\u3042").toDF.write
+        .option("writeNonAsciiCharacterAsCodePoint", "true").json(s"$basePath")
+      val actualText = spark.read.text(s"$basePath")
+        .sort("value").map(_.getString(0)).collect().mkString
+      val expectedText = 
"{\"value\":\"\\n\"}{\"value\":\"\\u3042\"}{\"value\":\"a\"}"
+      assert(actualText === expectedText)
+
+      val actualJson = spark.read.json(s"$basePath")
+        .sort("value").map(_.getString(0)).collect().mkString
+      val expectedJson = "\na\u3042"
+      assert(actualJson === expectedJson)
+    }
+    // scalastyle:on nonascii
+  }

Review comment:
       O.K, I'll cover the case of `pretty=true`.
   BTW, I've noticed another issue when writing the test case for `pretty=true` 
(#32203).
   So, I'll fix that issue first.

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/json/JacksonGenerator.scala
##########
@@ -73,7 +73,12 @@ private[sql] class JacksonGenerator(
 
   private val gen = {
     val generator = new 
JsonFactory().createGenerator(writer).setRootValueSeparator(null)
-    if (options.pretty) generator.useDefaultPrettyPrinter() else generator
+    val ppGenerator = if (options.pretty) generator.useDefaultPrettyPrinter() 
else generator
+    if (options.writeNonAsciiCharacterAsCodePoint) {
+      generator.setHighestNonEscapedChar(0x7F)

Review comment:
       We can set both options. Thanks. I'll fix it.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to