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?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.