# Getting around the Option\[Seq\] constraint with play-json macros (Reads)

**URL:** <https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362>\
**Category:** Libraries\
**Created:** [November 20, 2019, 3:55pm UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362 "2019-11-20T15:55:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [November 20, 2019, 3:55pm UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/1 "2019-11-20T15:55:52Z")

</div>

Not sure if [cross-posting](https://discuss.lightbend.com/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5428) is ok, but trying my luck here…

Upon reading JSON documents in play-json, it seems the code generated by the macros expects sequences to be enclosed in an Option if they may be empty. Is there a way to prevent this?

Thanks!

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://users.scala-lang.org/user_avatar/users.scala-lang.org/martijnhoekstra/32/424_2.png) [@martijnhoekstra](https://users.scala-lang.org/u/martijnhoekstra)\
**Post date:** [November 20, 2019, 4:15pm UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/2 "2019-11-20T16:15:07Z")

</div>

Does this really go for empty collections, or null/absence of a collection?

---

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [November 20, 2019, 4:24pm UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/3 "2019-11-20T16:24:59Z")

</div>

Actually I had only tried with an absent value, but it turns it the same problem occurs with `null` but not `[]`

The following illustrates the problem (this excerpt runs sucessfully):

```auto
  import play.api.libs.json._
  
  case class Foo1(bar: String, baz: Seq[Int])
  case class Foo2(bar: String, baz: Option[Seq[Int]])
  
  implicit val FormatFoo1 = Json.format[Foo1]
  implicit val FormatFoo2 = Json.format[Foo2]
  
  def main(args: Array[String]): Unit = {       
    assert(Json.fromJson[Foo1](Json.parse("""{"bar":"hello","baz":[1, 2, 3]}""")).isSuccess)
    assert(Json.fromJson[Foo1](Json.parse("""{"bar":"hello","baz":[] }""")).isSuccess)
    assert(Json.fromJson[Foo1](Json.parse("""{"bar":"hello","baz":null }""")).isError )
    assert(Json.fromJson[Foo1](Json.parse("""{"bar":"hello" }""")).isError )

    assert(Json.fromJson[Foo2](Json.parse("""{"bar":"hello","baz":[1, 2, 3]}""")).isSuccess)
    assert(Json.fromJson[Foo2](Json.parse("""{"bar":"hello","baz":[] }""")).isSuccess)
    assert(Json.fromJson[Foo2](Json.parse("""{"bar":"hello","baz":null }""")).isSuccess)
    assert(Json.fromJson[Foo2](Json.parse("""{"bar":"hello" }""")).isSuccess)
  }

```

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://users.scala-lang.org/user_avatar/users.scala-lang.org/martijnhoekstra/32/424_2.png) [@martijnhoekstra](https://users.scala-lang.org/u/martijnhoekstra)\
**Post date:** [November 20, 2019, 6:08pm UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/4 "2019-11-20T18:08:28Z")

</div>

That seems correct to me as default behaviour: no collection is different from an empty collection.

Manually creating the `Reads` instance would work around. `myopt.getOrElse(Nil)` for example will flatten an `Option[Seq[A]]` into a `List[A]`

---

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [November 20, 2019, 7:41pm UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/5 "2019-11-20T19:41:32Z")

</div>

You’re right that I should rephrase my question, in that it’s not a “problem” with play-json.

Basically I was hoping for a workaround, one that would still allow me to use the macros (else it quickly becomes very verbose). But I suspect it’s going to be more work than just wrapping it in an Option 🙂

---

<div class="post-metadata">

**Author:** ![cbley](https://users.scala-lang.org/user_avatar/users.scala-lang.org/cbley/32/1064_2.png) [@cbley](https://users.scala-lang.org/u/cbley)\
**Post date:** [November 20, 2019, 8:45pm UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/6 "2019-11-20T20:45:42Z")

</div>

The problem is: how should the case class be constructed if one of its parameters are missing?

What you could do, is to use default parameters:

```auto
case class Foo1(bar: String, baz: Seq[Int] = Seq.empty)

val implicit foo1Reads = Json.using[Json.MacroOptions with Json.DefaultValues].reads[Foo1]

```

---

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [November 21, 2019, 3:11am UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/7 "2019-11-21T03:11:01Z")

</div>

Very nice thanks! I think that might be just what I needed. It’s unfortunate the documentation for the [Macros](https://www.playframework.com/documentation/2.7.x/ScalaJsonAutomated) doesn’t mention `.using`

---

<div class="post-metadata">

**Author:** ![godenji](https://users.scala-lang.org/user_avatar/users.scala-lang.org/godenji/32/785_2.png) [@godenji](https://users.scala-lang.org/u/godenji)\
**Post date:** [November 28, 2020, 3:22am UTC](https://users.scala-lang.org/t/getting-around-the-option-seq-constraint-with-play-json-macros-reads/5362/8 "2020-11-28T03:22:09Z")

</div>

Yes!!! Wrapping possibly empty collections in Option begone.

`Option[List[T]]`, just ughhhh, no.

Thanks
