Skip to main content
added 173 characters in body
Source Link
Łukasz
  • 8.7k
  • 2
  • 30
  • 51

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

import org.joda.time.{DateTime, Duration}
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._

object When {
  def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = 
    (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Your second approach makes no sense. Either use reads and compose them or just manually check if something is there and if not return a different result, but it is not worth doing this, just go with default approach.

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Your second approach makes no sense. Either use reads and compose them or just manually check if something is there and if not return a different result, but it is not worth doing this, just go with default approach.

Here is working example of how to do this:

import org.joda.time.{DateTime, Duration}
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._

object When {
  def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = 
    (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Your second approach makes no sense. Either use reads and compose them or just manually check if something is there and if not return a different result, but it is not worth doing this, just go with default approach.

added 220 characters in body
Source Link
Łukasz
  • 8.7k
  • 2
  • 30
  • 51

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Your second approach makes no sense. Either use reads and compose them or just manually check if something is there and if not return a different result, but it is not worth doing this, just go with default approach.

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Your second approach makes no sense. Either use reads and compose them or just manually check if something is there and if not return a different result, but it is not worth doing this, just go with default approach.

added 490 characters in body
Source Link
Łukasz
  • 8.7k
  • 2
  • 30
  • 51

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

Here is working example of how to do this:

object When { def apply(dateTime: DateTime): When = When(Left(dateTime))

  def apply(duration: Duration): When = When(Right(duration))

  val reads: Reads[When] = (__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
    (__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))

  val writes: Writes[When] = new Writes[When] {
    override def writes(o: When): JsValue = Json.obj(
      o.when.fold(
        duration => "duration" -> duration.getMillis,
        dateTime => "dateTime" -> dateTime.getMillis
      )
    )
  }

  implicit val format = Format(reads, writes)
}

basically you should map the reads

(__ \ "dateTime").read[Long]

gives you Reads[Long], then you can map result to When. You were just passing parameter. This parameter could be a Long, to just ignore what is read and return that value, or implicit reads for long that you probably don't want to change and should let it stay implicit.

So then in similar way you can create another reads for duration and combine them with alternative (|) and done, you have reads.

Source Link
Łukasz
  • 8.7k
  • 2
  • 30
  • 51
Loading