question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why will upickle convert `Option[T]` to array `[...]`?

See original GitHub issue

I found if there is an option value in case class, it will be converted to [] in js

case class User(name:String, age: Option[Int])

val user1 = User("Free", Some(100))
val user2 = User("Wind", None)

Will be converted to:

{ "name": "Free", "age": [100] }
{ "name": "Wind", "age": [] }

Why use array here rather than:

{ "name": "Free", "age": 100 }
{ "name": "Wind" }

The later is the one I usually do with other scala json libraries

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
l15k4commented, Feb 23, 2016

This writer is applied for Some(v) :

  implicit val optHavingWriter = upickle.default.Writer[Option[HavingSpec]]{
    case Some(v) => writeJs[HavingSpec](v)
    case None => Js.Null
  }

This one too :

  implicit val optBooleanWriter = upickle.default.Writer[Option[Boolean]]{
    case Some(v) => writeJs[Boolean](v)
    case None => Js.Null
  }

But these are not :

  implicit val optStringWriter = upickle.default.Writer[Option[String]]{
    case Some(v) => writeJs[String](v)
    case None => Js.Null
  }
  implicit val optIntWriter = upickle.default.Writer[Option[Int]]{
    case Some(v) => writeJs[Int](v)
    case None => Js.Null
  }

Unfortunately generic types doesn’t work at all, so I have to list a custom writer for each type :

  implicit def optWriter[T : Writer] = upickle.default.Writer[Option[T]]{
    case Some(v) => writeJs[T](v)
    case None => Js.Null
  }

It would be cool if it worked even for String and Int … any idea ?

1reaction
mycaulecommented, Dec 10, 2018

One common use case would be a reader for

case class MyCC(a: String, b: String, c: Option[String])

so that undefined is parsed too :

// expected
{"a": "x", "b": "y"}           => MyCC("x", "y", None)
// upickle.core.AbortException: missing keys in dictionary: c

// expected and verified
{"a": "x", "b": "y", "c": "z"} => MyCC("x", "y", Some("z"))

Last solution from @asdcdow does this

{"a": "x", "b": "y", "c": null} => MyCC("x", "y", None)

Modifying MyCC with Defaults

case class MyCC(a: String, b: String, c: Option[String] = None)

I get what I expect

// expected and verified
{"a": "x", "b": "y"}           => MyCC("x", "y", None)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why will upickle convert Option[T] to array [...] ? #75 - GitHub
I found if there is an option value in case class, it will be converted to [] in js case class User(name:String, age:...
Read more >
lihaoyi/upickle - Gitter
I have a problem using upickle with type aliases in a cake pattern. ... to override the Option value from scala My options...
Read more >
µPickle 2.0.0
uPickle (pronounced micro-pickle) is a lightweight JSON and binary (MessagePack) serialization library for Scala. It's key features are:.
Read more >
Li Haoyi on Twitter: "uPickle can also parse/serialize any of ...
uPickle 0.6.0 can convert *between* JSON ASTs from any of the different ... byte-arrays or files Performance often isn't quite as good as...
Read more >
How to parse a json string to a case class in scaja.js and vice ...
Use one of the pickling libraries that work with Scala.js. The following two produce well-behaved and predictable JSON: uPickle · Prickle.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found