user79074
user79074

Reputation: 5300

Custom writer/reader in upickle no longer works after upgrade to 0.7.1

I have my own custom DateTime class for which I wrote the following custom writer and reader:

val dtWriter = Writer[DateTime]{
    case t => Js.Str(format(t))
}
val dtReader = Reader[DateTime]{
    case Js.Str(time) =>
        try {
            parse(time)
        }
        catch {
            case _: Exception =>
                SDateTime(0)
        }
}

However, I tried to upgrade my package version from 0.4.4 to 0.7.1 and this code no longer compiles giving me the following errors:

Error:(7, 8) object Js is not a member of package upickle

import upickle.Js

Error:(112, 23) object Writer does not take type parameters.

val dtWriter = Writer[DateTime]{

How should this be rewritten in the latest version of upickle?

Upvotes: 1

Views: 484

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170859

Based just on reading http://www.lihaoyi.com/upickle/#CustomPicklers:

val dtRW = readwriter[String].bimap[DateTime](
  t => format(t), // also `format _` or just `format` should work
  time =>
    try {
        parse(time)
    }
    catch {
        case _: Exception =>
            SDateTime(0)
    }
)

Upvotes: 4

Related Questions