Skip to content

Commit

Permalink
jackson: parse using a ByteBuffer (#23)
Browse files Browse the repository at this point in the history
* jackson: parse using a ByteBuffer

Update JacksonSupport.scala

* Update JacksonSupport.scala
  • Loading branch information
pjfanning authored May 17, 2024
1 parent 15093fe commit a12e702
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 59 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import com.fasterxml.jackson.core.util.{ BufferRecycler, JsonRecyclerPools, Recy
import com.fasterxml.jackson.core.{
JsonFactory,
JsonFactoryBuilder,
JsonParser,
StreamReadConstraints,
StreamReadFeature,
StreamWriteConstraints
}
import com.fasterxml.jackson.core.async.ByteBufferFeeder
import com.fasterxml.jackson.databind.{ Module, ObjectMapper }
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.scala.{ ClassTagExtensions, JavaTypeable }
Expand Down Expand Up @@ -204,14 +206,19 @@ trait JacksonSupport {
implicit def fromByteStringUnmarshaller[A: JavaTypeable](implicit
objectMapper: ObjectMapper with ClassTagExtensions = defaultObjectMapper
): Unmarshaller[ByteString, A] =
if (ByteStringInputStream.byteStringSupportsAsInputStream) {
Unmarshaller { _ => bs =>
Future.fromTry(Try(objectMapper.readValue[A](ByteStringInputStream(bs))))
}
} else {
Unmarshaller { _ => bs =>
Future.fromTry(Try(objectMapper.readValue[A](bs.toArrayUnsafe())))
}
Unmarshaller { _ => bs =>
Future.fromTry(Try {
val parser = objectMapper.getFactory
.createNonBlockingByteBufferParser()
.asInstanceOf[JsonParser with ByteBufferFeeder]
bs match {
case bs: ByteString.ByteStrings =>
bs.asByteBuffers.foreach(parser.feedInput)
case bytes =>
parser.feedInput(bytes.asByteBuffer)
}
objectMapper.readValue[A](parser)
})
}

/**
Expand Down

0 comments on commit a12e702

Please sign in to comment.