amnaredo / test

0 stars 0 forks source link

Configurable sum encoding #102

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

I need to parse the Pandoc format which is produced via aeson. aeson supports configuring the sum encoding which is done as follows in Pandoc:

Aeson.sumEncoding = Aeson.TaggedObject "t" "c"

Obviously, this consumes more space than the current approach chosen by uPickle:

  def annotate[V: ClassTag](rw: R[V], n: String) = R[V]{
    case Js.Arr(Js.Str(`n`), x) => rw.read(x)
  }

Would it be possible to not import annotate by default, so that I could define it manually?

This feature would significantly increase interoperability with other picklers as there doesn't seem to be a common standard on how to serialise classes.

ID: 55 Original Author: tindzk

amnaredo commented 3 years ago

Possible. Would require some thought as to how to make it work well. Not sure whether typeclasses or virtual-classes would be the way to go for this

Original Author: lihaoyi

amnaredo commented 3 years ago

Here's my first attempt: https://github.com/tindzk/upickle/tree/change-sumencoding

It doesn't work as expected since the implicit is not resolved properly. Do you have any idea how to fix it?

Edit: The fix was simple: The case object needed to be flagged with implicit.

Original Author: tindzk

amnaredo commented 3 years ago

This is fixed in master https://github.com/lihaoyi/upickle-pprint/blob/master/upickle/shared/src/main/scala/upickle/Api.scala#L7-L40

Now you can define your own upickle bundle and override the CaseR/CaseW sum encoding, swap between the old array-wrapped typetags and type-attribute typetags, and other things. By default I provide import upickle.legacy._ for the old encoding and upickle.default for a seemingly much-more-useful default encoding

Original Author: lihaoyi

amnaredo commented 3 years ago

Great, thanks!

Original Author: tindzk