Skip to content

Commit ec460d1

Browse files
committed
Remove all \u0000 characters from JSON strings in simple json support
1 parent d13b20a commit ec460d1

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/scala/com/github/tminglei/slickpg/PgJsonSupport.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.tminglei.slickpg
22

3+
import com.github.tminglei.slickpg.utils.JsonUtils
34
import slick.jdbc.{JdbcType, PositionedResult, PostgresProfile}
45
import scala.reflect.classTag
56

@@ -32,7 +33,7 @@ trait PgJsonSupport extends json.PgJsonExtensions with utils.PgCommonJdbcTypes {
3233
new GenericJdbcType[JsonString](
3334
pgjson,
3435
(v) => JsonString(v),
35-
(v) => v.value,
36+
(v) => JsonUtils.clean(v.value),
3637
hasLiteralForm = false
3738
)
3839

src/test/scala/com/github/tminglei/slickpg/PgJsonSupportSuite.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class PgJsonSupportSuite extends FunSuite {
2828
val testRec2 = JsonBean(35L, JsonString(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """))
2929
val testRec3 = JsonBean(37L, JsonString(""" ["a", "b"] """))
3030

31+
// Unicode testing
32+
// This byte string is equal to {"d":"123\u000045\u00006"}
33+
val unicodeJsonBytes: List[Byte] = List(123, 34, 100, 34, 58, 34, 49, 50, 51, 92, 117, 48, 48, 48, 48, 52, 53, 92, 117, 48, 48, 48, 48, 54, 34, 125)
34+
val unicodeJsonString = new String(unicodeJsonBytes.map(_.toChar).toArray)
35+
val unicodedJson = JsonString(unicodeJsonString)
36+
val unicodelessJson = JsonString("""{"d": "123456"}""")
37+
val testRec4 = JsonBean(39L, unicodedJson)
38+
3139
test("Json Lifted support") {
3240
val json1 = """{"a":"v1","b":2}"""
3341
val json2 = """{"a":"v5","b":3}"""
@@ -36,7 +44,7 @@ class PgJsonSupportSuite extends FunSuite {
3644
DBIO.seq(
3745
JsonTests.schema create,
3846
///
39-
JsonTests forceInsertAll List(testRec1, testRec2, testRec3)
47+
JsonTests forceInsertAll List(testRec1, testRec2, testRec3, testRec4)
4048
).andThen(
4149
DBIO.seq(
4250
JsonTests.filter(_.id === testRec2.id.bind).map(_.json).result.head.map(
@@ -123,7 +131,11 @@ class PgJsonSupportSuite extends FunSuite {
123131
// #-
124132
JsonTests.filter(_.id === 33L).map(_.json.set(List("c"), JsonString(""" [1] """))).result.head.map(
125133
r => assert(""" {"a": 101, "b": "aaa", "c": [1]} """.replace(" ", "") === r.value.replace(" ", ""))
126-
)
134+
),
135+
// \u0000 test
136+
JsonTests.filter(_.id === testRec4.id).map(_.json).result.head.map { r =>
137+
assert(unicodelessJson === r)
138+
}
127139
)
128140
).andFinally(
129141
JsonTests.schema drop
@@ -156,4 +168,4 @@ class PgJsonSupportSuite extends FunSuite {
156168
).transactionally
157169
), Duration.Inf)
158170
}
159-
}
171+
}

0 commit comments

Comments
 (0)