Skip to content

Commit

Permalink
fix JsonFraming to accept multiple top-level arrays (akka#26098)
Browse files Browse the repository at this point in the history
  • Loading branch information
shkoder committed Dec 10, 2018
1 parent 229db51 commit 9eec1b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ class JsonFramingSpec extends AkkaSpec {
)
}

"parse multiple arrays" in {
val input1 =
"""
|[
| { "name" : "john" }
|]
|""".stripMargin

val input2 =
"""
|[
| { "name" : "jack" }
|]
|""".stripMargin

val result = Source(List(ByteString(input1), ByteString(input2)))
.via(JsonFraming.objectScanner(Int.MaxValue))
.runFold(Seq.empty[String]) {
case (acc, entry) acc ++ Seq(entry.utf8String)
}

result.futureValue shouldBe Seq(
"""{ "name" : "john" }""",
"""{ "name" : "jack" }"""
)
}

"emit single json element from string" in {
val input =
"""| { "name": "john" }
Expand Down Expand Up @@ -126,7 +153,7 @@ class JsonFramingSpec extends AkkaSpec {
val input = TestPublisher.probe[ByteString]()
val output = TestSubscriber.probe[String]()

val result = Source.fromPublisher(input)
Source.fromPublisher(input)
.via(JsonFraming.objectScanner(Int.MaxValue))
.map(_.utf8String)
.runWith(Sink.fromSubscriber(output))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import scala.annotation.switch
private var trimFront = 0 // number of chars to drop from the front of the bytestring before emitting (skip whitespace etc)
private var depth = 0 // counter of object-nesting depth, once hits 0 an object should be emitted

private var charsInObject = 0
private var completedObject = false
private var inStringExpression = false
private var isStartOfEscapeSequence = false
Expand Down Expand Up @@ -118,7 +117,8 @@ import scala.annotation.switch
trimFront += 1
} else if (input == SquareBraceEnd && outsideObject) {
// outer array completed!
pos = -1
pos += 1
trimFront += 1
} else if (input == Comma && outsideObject) {
// do nothing
pos += 1
Expand All @@ -139,10 +139,7 @@ import scala.annotation.switch
isStartOfEscapeSequence = false
depth -= 1
pos += 1
if (depth == 0) {
charsInObject = 0
completedObject = true
}
if (depth == 0) completedObject = true
} else if (isWhitespace(input) && !inStringExpression) {
pos += 1
if (depth == 0) trimFront += 1
Expand Down

0 comments on commit 9eec1b9

Please sign in to comment.