Skip to content

Fix of unexpected MatchError in case of invalid escape character #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ final class JsonReader(val json: String) {
case 'r' => '\r'
case 't' => '\t'
case 'u' => ((readHex() << 12) + (readHex() << 8) + (readHex() << 4) + readHex()).toChar
case c => throw new ReadFailure(s"Unexpected character: '${c.toChar}'")
}
sb.append(unesc)
plainStart = i
case _ =>
}
}
if (sb != null) {
sb.append(json, plainStart, i - 1)
sb.toString
sb.append(json, plainStart, i - 1).toString
} else {
json.substring(plainStart, i - 1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,33 @@ trait BaseJsonOutput {
var i = 0
var s = 0
while (i < str.length) {
val esc = str.charAt(i) match {
case '"' => "\\\""
case '\\' => "\\\\"
case '\b' => "\\b"
case '\f' => "\\f"
case '\n' => "\\n"
case '\r' => "\\r"
case '\t' => "\\t"
case c if (ascii && c.toInt > 127) || Character.isISOControl(c) =>
c.toInt.formatted("\\u%04x")
case _ => null
val ch = str.charAt(i)
val esc = ch match {
case '"' => '"'
case '\\' => '\\'
case '\b' => 'b'
case '\f' => 'f'
case '\n' => 'n'
case '\r' => 'r'
case '\t' => 't'
case _ => (if ((ascii && ch.toInt > 127) || Character.isISOControl(ch)) 1 else 0).toChar
}
if (esc != null) {
builder.append(str, s, i).append(esc)
if (esc != 0) {
builder.append(str, s, i).append('\\')
s = i + 1
if (esc != 1) {
builder.append(esc)
} else {
builder.append('u').append(toHex((ch >> 12) & 15)).append(toHex((ch >> 8) & 15))
.append(toHex((ch >> 4) & 15)).append(toHex(ch & 15))
}
}
i += 1
}
builder.append(str, s, str.length)
builder.append('"')
builder.append(str, s, str.length).append('"')
}

protected final def toHex(nibble: Int): Char = (nibble + (if (nibble >= 10) 'a' - 10 else '0')).toChar
}

final class JsonStringOutput(builder: JStringBuilder, options: JsonOptions = JsonOptions.Default, depth: Int = 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class JsonStringInputOutputTest extends FunSuite with SerializationTestUtils wit
val options = JsonOptions(asciiOutput = true)
assert(write[String]("ąę", options) == "\"\\u0105\\u0119\"")
assert(read[String]("\"\\u0105\\u0119\"", options) == "ąę")
intercept[ReadFailure](read[String]("\"\\x0105\""))
}

test("indentation") {
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "1.29.1"
version in ThisBuild := "1.29.2-SNAPSHOT"