Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 09:28
    plokhotnyuk commented #689
  • 09:26

    plokhotnyuk on v2.7.2

    (compare)

  • 09:26

    plokhotnyuk on master

    Setting version to 2.7.2 Setting version to 2.7.3-SNAPSH… (compare)

  • 08:42

    plokhotnyuk on wa-for-compiler-error

    (compare)

  • 08:42
    plokhotnyuk closed #689
  • 08:42

    plokhotnyuk on master

    Fix #689 using W/A for compiler… (compare)

  • 08:42
    plokhotnyuk closed #692
  • 08:12
    david-bouyssie commented #689
  • Apr 15 17:51

    plokhotnyuk on master

    Remove Travis CI build (compare)

  • Apr 15 17:22

    plokhotnyuk on master

    Remove Travis CI build (compare)

  • Apr 15 11:53

    plokhotnyuk on master

    Update commons-core to 2.1.1 (#… (compare)

  • Apr 15 11:53
    plokhotnyuk closed #693
  • Apr 15 10:43
    scala-steward opened #693
  • Apr 14 13:28

    plokhotnyuk on master

    Add ability to override ADT lea… (compare)

  • Apr 14 09:53
    david-bouyssie commented #689
  • Apr 14 08:37
    plokhotnyuk commented #689
  • Apr 14 08:35
    plokhotnyuk opened #692
  • Apr 14 08:34

    plokhotnyuk on wa-for-compiler-error

    Fix #689 using W/A for compiler… (compare)

  • Apr 14 08:19
    plokhotnyuk labeled #689
  • Apr 13 13:01

    plokhotnyuk on gh-pages

    Update results of Scala.js benc… (compare)

Marcin Szałomski
@baldram
So one config for all enum related codecs. Excellent.
Thank you for looking into this!
Dermot Haughey
@hderms
Anyone know any Redis clients for Java/Scala that allow you to get a key's value in the form of an InputStream?
it's bizarre that it seems so hard to find that
Marcin Szałomski
@baldram
@hderms I think you hit wrong room by mistake. btw. eg. if you're doing Scala in ZIO ecosystem, there is simply zio-redis. Please try in some general-Scala or ZIO related one. Good luck.
Dermot Haughey
@hderms
ah yeah I meant for it to be in scala room
it's a shame because I'm using jsoniter to parse some JSON data from redis, but I can't find any redis clients that will let me read a value as an InputStream, which would work well with jsoniter
Tushar Mathur
@tusharmath
Hi
is there a codec available for Throwable type?
Andriy Plokhotnyuk
@plokhotnyuk
Hi, Tushar! There are no predefined codecs, but you can derive them for classes with primary constructors which contains all fields that need to be serialized, like here
Mateusz Kubuszok
@MateuszKubuszok
Hi! Are there some plans for adding some helpers for custom codecs in the near future, as well as Refined types? I saw issues but with no development for a while. For now I am implemented https://github.com/branchtalk-io/backend/blob/master/modules/common-api/src/main/scala/io/branchtalk/api/JsoniterSupport.scala#L26 which let imap over already derived codec, and add Refined and @newtype do it, but it's really inconvenient. (i)map or similar could be a part of official API, I am not confident with my implementation. It would also be helpful if support for refined and newtypes (ar any other extension) could be added with some generic implicits - so far I was unsuccessful in doing so, and my generic implicits seem to be ignored by macros. Only creating a specific refined/newtype implicits seem to work. Is there anything I can help to make implement these features?
Andriy Plokhotnyuk
@plokhotnyuk
Hi, Mateusz! Yep, current macros are not extendable and support injection of custom or differently preconfigured codecs only for specific (non generic types) using nearest implicit val values in scope.
Mateusz Kubuszok
@MateuszKubuszok
And what about adding support for .map/.mapDecode? Does it fit into what core library should be able to do?
Andriy Plokhotnyuk
@plokhotnyuk
I think map and mapDecode would be helpful in simple cases. But when stuff is getting complex easier approach would be extracting of business logic transformations and validations to some other places using a separated representation and libraries like Chimey.
Mateusz Kubuszok
@MateuszKubuszok
I usually use them for cases where deriving the target representation would be to much of a pain with configuration (or configuration would to invasive/obfuscating), and it is easier to use some dedicated derivation-only type and imap from it to a target representation. I find it especially useful when I want to have the same conventions across several libraries e.g. Tapir's Schema and Codecs and configuring them would require a lot of annotations and repetitive configurations that I could easily misalign.
corentin
@corenti13711539_twitter

I'd need to decode a set of JSON messages that include a type discriminator field indicating how the message should be parsed. The messages arrive from the same source and the type discriminator is the only way of inferring how to decode the messages. The JSON messages look similar to this:

{"type":"message", "foo": 123}
{"type":"message", "bar": "baz"}

What's the recommended approach of implementing decoding for these with jsoniter-scala?

corentin
@corenti13711539_twitter

That JSON should have been as follows:

{"type":"foo_type", "foo": 123}
{"type":"bar_type", "bar": "baz"}

Using a sealed trait as a base type and then case classes extending and then setting up CodecMakerConfig with withDiscriminatorFieldName would seem to work.
However, now I'm running into problems with the discriminator field values apparently needing to exactly match the Scala type names - is there a way to customize this?

corentin
@corenti13711539_twitter
That would apparently be withAdtLeafClassNameMapper.
Andriy Plokhotnyuk
@plokhotnyuk
@corenti13711539_twitter Yes, the withAdtLeafClassNameMapper configuration option allow to use any function that maps from class names to hint strings in compile time.
corentin
@corenti13711539_twitter
@plokhotnyuk Thanks! :thumbsup:

I'm getting a compiler warning when creating a JsonValueCodec in a very basic case:

import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros._

case class Foo(id: Int)
implicit val foo: JsonValueCodec[Foo] = JsonCodecMaker.make

Here's the warning:

[warn] .../Foo.scala:19:58: match may not be exhaustive.
[warn] It would fail on the following input: (x: Int forSome x not in 0)
[warn]   implicit val foo: JsonValueCodec[Foo] = JsonCodecMaker.make
[warn]                                                          ^

I'm using Scala v2.13 and jsoniter v2.7.0.
What's causing this and is there a way to fix it other than suppressing the warning?

Andriy Plokhotnyuk
@plokhotnyuk
I've never experienced such kind of message with the JsonCodecMaker.make. Here is my try to reproduce, but compiler output is clear - no warning when built with sbt clean run: https://github.com/plokhotnyuk/jsoniter-scala/pull/683/files
@corenti13711539_twitter do you have possibility to reproduce the warning in a small separated project?
corentin
@corenti13711539_twitter
@plokhotnyuk Sure - here's a project for reproducing the warning: https://we.tl/t-bFilnAAWrv
corentin
@corenti13711539_twitter
I'm able to reproduce the warning with this project using sbt 'clean; compile'
Andriy Plokhotnyuk
@plokhotnyuk
Thanks for the reporting! I've fixed it in the following commit and will cut a release: plokhotnyuk/jsoniter-scala@4155010
corentin
@corenti13711539_twitter
Awesome :raised_hands: ! Thanks @plokhotnyuk ! :bow:
Andriy Plokhotnyuk
@plokhotnyuk
@corenti13711539_twitter Done! Please try 2.7.1 release and keep giving your GitHub stars to loved projects ;)
corentin
@corenti13711539_twitter
@plokhotnyuk Fabulous! The warning disappears with the new version :muscle:
Repository starred :star: - thanks! :thumbsup:
corentin
@corenti13711539_twitter
Btw, any thoughts on performance implications of using .withRequireDiscriminatorFirst(false)? I'd imagine this would be affected at least to some extent by the size of the JSON as well as discriminator field position. I'm decoding JSON that's produced by another party and unfortunately won't be able to affect field order.
Andriy Plokhotnyuk
@plokhotnyuk
@corenti13711539_twitter .withRequireDiscriminatorFirst(false) doesn't affect performance, but if the hint was set in the end the expected average performance drop is about 1.5x times for each level of nested coproducts. As an example the GeoJSON sample in the benchmark sub-project has 3 nested sum types: FeatureCollection, Feature, and Polygon, so when all hints appear in the end the performance drop will be about 1.5 * 1.5 * 1.5 = 3.375 times.
Andriy Plokhotnyuk
@plokhotnyuk
From 12 JSON serializers for Scala that are tested by benchmarks only circe and spray-json serialize hints in the end.
corentin
@corenti13711539_twitter
@plokhotnyuk thanks! :thumbsup:
Andriy Plokhotnyuk
@plokhotnyuk
@corenti13711539_twitter Thank you too for the feedback and support!
yankun1992
@yankun1992
hello everyone!I am looking for help,if I have a json like ["hello", "world", 1, 0, false] . How to parse it ?
Andriy Plokhotnyuk
@plokhotnyuk
@yankun1992 Hi! Do you have a stable structure of JSON and a data type to which it should be parsed? If no than probably you should try dijon, that uses jsoniter-scala-core under the hood.
yankun1992
@yankun1992
I
I want to parse it to Array[String]
or Array[Any]
Andriy Plokhotnyuk
@plokhotnyuk
The most efficient way parsing to Array[String] or Array[Any] is a custom codec. Have you tried dijon for that yet? How are you going to use parsed values?
yankun1992
@yankun1992
no, my json is byte code in bytebuffer, I want parse it to scala case class directly.
```json
{
    "request_id": "",
    "code": 0,
    "msg": "",
    "data": {
        "fields": [
            "exchange",
            "cal_date",
            "is_open",
            "pretrade_date"
        ],
        "items": [
            [
                "SSE",
                "19901219",
                1,
                null
            ],
            [
                "SSE",
                "19941222",
                1,
                "19941221"
            ],
            [
                "SSE",
                "19941223",
                1,
                "19941222"
            ]
        ],
        "has_more": false
    }
}
and i want to parse it to
case class TushareResponse(
                            request_id: String,
                            code: Int,
                            msg: String,
                            data: Data
                          )
case class Data(
                   fields: Array[String],
                   items: Array[Array[Option[String]]],
                   has_more: Boolean
                 )
Andriy Plokhotnyuk
@plokhotnyuk
Below is an implementation of a custom codec for Option[String] of items` elements:
        implicit val optStringCodec: JsonValueCodec[Option[String]] = new JsonValueCodec[Option[String]] {
          override def decodeValue(in: JsonReader, default: Option[String]): Option[String] = {
            val b = in.nextToken()
            if (b == 'n') {
              in.readNullOrError(None, "expected `null` value")
              None
            } else if (b == '"') {
              in.rollbackToken()
              Some(in.readString(null))
            } else if (b == 't' || b == 'f') {
              in.rollbackToken()
              Some(in.readBoolean().toString)
            } else if ((b >= '0' && b <= '9') || b == '-') {
              in.rollbackToken()
              Some(in.readBigDecimal(null).toString)
            } else in.decodeError("unexpected value")
          }

          override def encodeValue(x: Option[String], out: JsonWriter): Unit =
            x.fold(out.writeNull())(out.writeVal)

          override def nullValue: Option[String] = None
        }
Full code please see here
The expected output:
{
    "request_id": "",
    "code": 0,
    "msg": "",
    "data": {
        "fields": [
            "exchange",
            "cal_date",
            "is_open",
            "pretrade_date"
        ],
        "items": [
            [
                "SSE",
                "19901219",
                "1",
                null
            ],
            [
                "SSE",
                "19941222",
                "1",
                "19941221"
            ],
            [
                "SSE",
                "19941223",
                "1",
                "19941222"
            ]
        ],
        "has_more": false
    }
}
yankun1992
@yankun1992
Aha, I got it. Thank you ever much.
By the way, this is a really cool library! Thank you again!
Chetan Mehrotra
@chetanmeh

By the way, this is a really cool library! Thank you again!

+1 To that!