plokhotnyuk on v2.14.2
plokhotnyuk on master
Setting version to 2.14.2 Setting version to 2.14.3-SNAPS… (compare)
plokhotnyuk on master
Fix reporting of negative offse… (compare)
plokhotnyuk on master
More test coverage for ZoneId t… (compare)
plokhotnyuk on master
Clean up of tests (compare)
plokhotnyuk on master
Reduce allocations for re-entra… (compare)
plokhotnyuk on master
Reduce allocations for re-entra… (compare)
plokhotnyuk on master
Reduce allocations for re-entra… (compare)
plokhotnyuk on gh-pages
Update results from JVMs for v2… (compare)
plokhotnyuk on master
Update README.md (compare)
plokhotnyuk on master
Update README.md (compare)
plokhotnyuk on master
Update README.md (compare)
plokhotnyuk on master
Close #927 by fixing bit set re… Clear cache of dependencies (compare)
plokhotnyuk on master
Close #927 by fixing bit set re… (compare)
plokhotnyuk on master
Clear cache of dependencies (compare)
plokhotnyuk on fix-benchmark-for-jackson-module-scala
plokhotnyuk on master
Close #927 by fixing bit set re… (compare)
no ADT tags
option is not supported for derivation of codecs. The main reason is non-safe implementation of parsing for general case. plokhotnyuk/jsoniter-scala#435 is an issue where possible solutions were discussed.
I am converting from an AST based json library (well a few actually) I am looking for a recommendation. I have messaging middleware where effectively there are two layers one layer has the metadata and another layer handles the message bodies like so
case class Message(
context: MessageContext,
sender: NodeId,
destination: NodeId,
methodName: MethodName,
event: StreamEvent,
headers: Map[String,String],
body: JsonStr,
)
So JsonStr is really any valid json. In the AST json world I would use the AST type. In jsoniter world that doesn't exist. So I have this
object JsonStr {
implicit val codec: JsonValueCodec[JsonStr] =
new JsonValueCodec[JsonStr] {
override def decodeValue(in: JsonReader, default: JsonStr): JsonStr =
JsonStr(Chunk.array(in.readRawValAsBytes))
override def encodeValue(x: JsonStr, out: JsonWriter): Unit =
out.writeRawVal(x.rawBytes.toArray) // ??? optimize me we have at least one if not more extra array copies
override def nullValue: JsonStr = ???
}
val Null = JsonStr(Chunk.array("null".getBytes))
val EmptyObj = JsonStr(Chunk.array("{}".getBytes))
val EmptyArr = JsonStr(Chunk.array("[]".getBytes))
}
case class JsonStr(rawBytes: Chunk[Byte])
case class QueryRequest(query: String, batching: Int)
// the response
case class QueryResponse(rows: fs2.Stream[F, Chunk[Row]], metadata: QueryMetadata)
Array[Byte]
cannot be used directly but safely like in the RawVal
type here instead of using intermediate Chunk[Byte]
?
out.writeRawVal(x.rawBytes.toArray)
def writeRawVal(array: Array[Byte], offset: Int, len: Int)
(array: Array[Byte], offset: Int, len: Int)
then I can get zero copy on my side
java.nio.ByteBuffer
type a better option with adding following methods: def writeRawVal(bytes: java.nio.ByteBuffer): Unit
and def readRawValAsByteBuffer(bytes: java.nio.ByteBuffer): Unit
?
extends AnyVal
type around an Array[Byte]
to give you the data of the array but not have it be mutable would be really nice. I wonder if someone has done the work on such a thing
Currently I'm going to check if it will be possible to minimize derivation configuration by adding the support for Enumeratum in some additional sub-project or even in the jsoniter-scala-macros sub-project with an optionally scoped dependency on Enumeratum.
While EnumEntry from Enumeratum is supported [...]
@plokhotnyuk Hi Andriy! Have you had a chance to evaluate creating the Enumeratum support related jsoniter-scala-macros sub-project? Is it something in your current work scope and doable based on the existing work from the past (https://github.com/lloydmeta/enumeratum/pull/176/files)?
CodecMakerConfig.useEnumeratumEnumValueId
flag that turns on using of value from value
fields as ids for parsing and serialization of objects which extends sealed traits as it was done for Scala enums here: plokhotnyuk/jsoniter-scala@f37dde6 Also, it can bring maximum performance because Enumeratum API will not be used at all.
@plokhotnyuk I have one more question Andriy. Is it possible to add to generated code a SuppressWarnings
? Maybe as an option?
The macro causes a lot of Wartremover errors. So we need to ignore it like below.
@SuppressWarnings(Array("org.wartremover.warts.All"))
The problem is that it doesn't make sense to create a single file, let's say Codecs.scala, put all codecs there, and add wartremoverExcluded
for the whole file from build.sbt
.
In this case, we would have to import codecs manually, losing the advantage of implicit codec.
If we have a codec inside a companion object, this imports automatically and also, other features relying on this codec are working automatically.
So everywhere in code we add this linter ignore for JsonCodecMaker.make
. This is in each place we use Jsoniter.
object SomeFooBarEvent {
@SuppressWarnings(Array("org.wartremover.warts.All")) // silencing generated code false-positive issues
implicit val codec: JsonValueCodec[SomeFooBarEvent] = JsonCodecMaker.make
}
final case class SomeFooBarEvent(
metadata: FooBarMetadata,
request: FooBarRequest
)
Maybe such a SuppressWarnings could be already generated or with some option?
Or there is another solution?
sbt clean compile
from the console and that's it. Shall I provide some example warnings?
I'm not sure it's easy to fix those warnings as "null" is a thing which is prohibited or lack of triple equals and many things.
Here is example:
object FooBar {
implicit val codec: JsonValueCodec[FooBar] = JsonCodecMaker.make
}
final case class FooBar(foo: Int, bar: Option[String])
...causes:
[warn] /mnt/foobar/model/FooBar.scala:11:63: [wartremover:Equals] != is disabled - use =/= or equivalent instead
[warn] implicit val codec: JsonValueCodec[FooBar] = JsonCodecMaker.make
@SuppressWarnings(Array("org.wartremover.warts.Equals"))
. Then next will come, and next, and next...
[warn] /mnt/foobar/model/FooBar.scala:12:63: [wartremover:Null] null is disabled
So now I do:@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.Null"))
... and I get:
[warn] /mnt/foobar/model/FooBar.scala:12:63: [wartremover:OptionPartial] Option#get is disabled - use Option#fold instead
It looks like never ending story, so I end up in adding @SuppressWarnings(Array("org.wartremover.warts.All"))
.
That's why it would be great to already generate this SuppressWarnings, as this is kind of not nice thing we need to add in a code and excuse ;) for it in a comment.
If not generating Supress Warnings, I was thinking also how about implementing some custom annotation alias for Jsoniter like @SupressJsoniterWarts
which would have this suppress for All errors under the hood.
Maybe also a solution. So adding this short version of Supress Warnings (@SupressJsoniterWarts
) would be at least a bit nicer.
The best way would be to have it already generated, or have some configuration in build.sbt or whatever while importing a library that this will generate those @SuppressWarnings(Array("org.wartremover.warts.All"))
. Not sure this is possible.
git clone --depth 1000 --branch master git@github.com:plokhotnyuk/jsoniter-scala.git
, build and publish locally a snapshot version with sbt clean publishLocal
and then test the 2.8.2-SNAPSHOT
version if it works for you?