Skip to content

Commit

Permalink
Support the pretty option
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGekk committed Sep 23, 2018
1 parent 826e8c3 commit 051c8fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ private[sql] class JSONOptions(
}
val lineSeparatorInWrite: String = lineSeparator.getOrElse("\n")

/**
* Generating JSON strings in pretty representation if the parameter enabled.
*/
val pretty: Boolean = parameters.get("pretty").map(_.toBoolean).getOrElse(false)

/** Sets config options on a Jackson [[JsonFactory]]. */
def setJacksonOptions(factory: JsonFactory): Unit = {
factory.configure(JsonParser.Feature.ALLOW_COMMENTS, allowComments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ private[sql] class JacksonGenerator(
s"Initial type ${dataType.catalogString} must be a ${MapType.simpleString}")
}

private val gen = new JsonFactory().createGenerator(writer).setRootValueSeparator(null)
private val gen = {
val generator = new JsonFactory().createGenerator(writer).setRootValueSeparator(null)
if (options.pretty) generator.useDefaultPrettyPrinter() else generator
}

private val lineSeparator: String = options.lineSeparatorInWrite

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,21 @@ class JsonFunctionsSuite extends QueryTest with SharedSQLContext {
test("pretty print - roundtrip from_json -> to_json") {
val json = """[{"book":{"publisher":[{"country":"NL","year":[1981,1986,1999]}]}}]"""
val jsonDF = Seq(json).toDF("root")
val expected =
"""[ {
| "book" : {
| "publisher" : [ {
| "country" : "NL",
| "year" : [ 1981, 1986, 1999 ]
| } ]
| }
|} ]""".stripMargin

checkAnswer(
jsonDF.select(to_json(from_json($"root", schema_of_json(lit(json))))),
Seq(Row(json)))
jsonDF.select(
to_json(
from_json($"root", schema_of_json(lit(json))),
Map("pretty" -> "true"))),
Seq(Row(expected)))
}
}

0 comments on commit 051c8fd

Please sign in to comment.