Skip to content

Commit

Permalink
impove formatting and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Aug 14, 2023
1 parent 59a3715 commit a2d42b5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* jdbc: DB_READONLY=true env var can be used to make the whole app read-only (e.g. when migrating the DB, instead of Heroku maintenance mode)
* jdbc: CrudRepository.get() now has optional forUpdate parameter
* HttpExchange.fileName() added for setting of Content-Disposition
* json: json within json (and '\') are now properly escaped
* csv: new module for CSV parsing/generation

# 1.5.5
Expand Down
2 changes: 1 addition & 1 deletion json/src/JsonRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JsonRenderer(private val out: Writer, private val opts: JsonMapper): AutoC
private fun writeString(s: String) {
write('\"')
s.forEach { when(it) {
'\n' -> write("\\n");'\\' -> write("\\\\"); '\r' -> write("\\r"); '\t' -> write("\\t"); '"' -> write("\\\"")
'\n' -> write("\\n"); '\r' -> write("\\r"); '\t' -> write("\\t"); '"' -> write("\\\""); '\\' -> write("\\\\")
in '\u0000'..'\u001F' -> { write("\\u"); write(it.code.toString(16).padStart(4, '0')) }
else -> write(it)
} }
Expand Down
5 changes: 2 additions & 3 deletions json/test/JsonRendererTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class JsonRendererTest {

@Test fun string() {
expect(mapper.render("Hello")).toEqual("\"Hello\"")
expect(mapper.render("Hello\n\t\"World\"\u001F")).toEqual("\"Hello\\n\\t\\\"World\\\"\\u001f\"")
expect(mapper.render("Hello\n\t\"World\"\u001F\\")).toEqual("\"Hello\\n\\t\\\"World\\\"\\u001f\\\\\"")
}

@Test fun converter() {
Expand All @@ -46,8 +46,7 @@ class JsonRendererTest {
expect(mapper.render(mapOf(1 to mapOf(2 to arrayOf(1, 2, 3))))).toEqual("""{"1":{"2":[1,2,3]}}""")
}

@Test
fun escaped() {
@Test fun `json in json`() {
val value = mapper.render(mapOf("a" to "\"foo\""))
expect(mapper.render(mapOf("x" to value))).toEqual("""{"x":"{\"a\":\"\\\"foo\\\"\"}"}""")
}
Expand Down

0 comments on commit a2d42b5

Please sign in to comment.