1

I am working with the Upickle/Ujson and want to write a custom encoder to get the hang of things.

Suppose I have the following hierarchy (from the tutorial here: Link)

import upickle.default._

object TestDrive extends App {
  sealed trait TypedFoo

  object TypedFoo{
    import upickle.default._

    implicit val readWriter: ReadWriter[TypedFoo] = ReadWriter.merge(
      macroRW[Bar], macroRW[Baz], macroRW[Quz]
    )

    case class Bar(i: Int) extends TypedFoo
    case class Baz(s: String) extends TypedFoo
    case class Quz(b: Boolean) extends TypedFoo
  }

  import TypedFoo._
  println(writeJs(Bar(100)))
}

Firstly this fails to compile. Why is this the case? Have I misunderstood the page?

Secondly

What if I want to serialize Bar and add a field, "parent":"TypedFoo", at the same time? So Bar looks like:

{"parent":"TypedFoo", "$type":"package.TestDrive.TypedFoo.Bar","i":100}

How can I do this too?

finite_diffidence
  • 893
  • 2
  • 11
  • 20

0 Answers0