Skip to content

Commit

Permalink
small refactoring - named constants in JsonCodec.splitOnJsonBoundary
Browse files Browse the repository at this point in the history
  • Loading branch information
gregor-rayman committed Sep 10, 2024
1 parent 052d94f commit 52d42a0
Showing 1 changed file with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ object JsonCodec {
schemaBasedBinaryCodec[A](JsonCodec.Config.default)

val splitOnJsonBoundary: ZPipeline[Any, Nothing, String, String] = {
val validNumChars = Set('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'E', 'e', '-', '+', '.')
val validNumChars = Set('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'E', 'e', '-', '+', '.')
val ContextJson = 'j'
val ContextString = 's'
val ContextBoolean = 'b'
val ContextNull = 'u'
val ContextNullAfterFirstL = 'x'
val ContextNumber = 'n'
val ContextEscape = 'e'

ZPipeline.suspend {
val stringBuilder = new StringBuilder
var depth = 0
var valueType = 'j' // j = json, s = string, b = boolean, u = null, n = number, x = null after first null, e = escape in string
var context = ContextJson

def fetchChunk(chunk: Chunk[String]): Chunk[String] = {
val chunkBuilder = ChunkBuilder.make[String]()
Expand All @@ -78,61 +86,61 @@ object JsonCodec {
c <- string
} {
var valueEnded = false
valueType match {
case 'e' =>
valueType = 's'
case 's' =>
context match {
case ContextEscape =>
context = 's'
case ContextString =>
c match {
case '\\' => valueType = 'e'
case '\\' => context = ContextEscape
case '"' =>
valueType = 'j'
context = ContextJson
valueEnded = true
case _ =>
}
case 'b' =>
case ContextBoolean =>
if (c == 'e') {
valueType = 'j'
context = ContextJson
valueEnded = true
}
case 'u' =>
case ContextNull =>
if (c == 'l') {
valueType = 'x'
context = ContextNullAfterFirstL
}
case 'x' =>
case ContextNullAfterFirstL =>
if (c == 'l') {
valueType = 'j'
context = ContextJson
valueEnded = true
}
case 'n' =>
case ContextNumber =>
c match {
case '}' | ']' =>
depth -= 1
valueType = 'j'
context = ContextJson
valueEnded = true
case _ if !validNumChars(c) =>
valueType = 'j'
context = ContextJson
valueEnded = true
case _ =>
}
case 'j' =>
case _ =>
c match {
case '{' | '[' =>
depth += 1
case '}' | ']' =>
depth -= 1
valueEnded = true
case '"' =>
valueType = 's'
context = ContextString
case 't' | 'f' =>
valueType = 'b'
context = ContextBoolean
case 'n' =>
valueType = 'u'
context = ContextNull
case x if validNumChars(x) =>
valueType = 'n'
context = ContextNumber
case _ =>
}
}
if (depth > 0 || valueType != 'j' || valueEnded)
if (depth > 0 || context != ContextJson || valueEnded)
stringBuilder.append(c)

if (valueEnded && depth == 0) {
Expand Down

0 comments on commit 52d42a0

Please sign in to comment.