0

I meet a problem while implementing Json Write for my case class that I use with squeryl and which extend KeyedEntity. It looks like this:

case class Order(fk_soc : Int, order_date: String, date_creation: Timestamp,fk_uther_author: Int, fk_statut : Int, tva : Option[Double], total_ht: Option[Double],total_ttc: Option[Double], note: Option[String]) extends KeyedEntity[Int] {
   val id : Int = 0

  val ref : String = ""


  val date_modif : Option[Timestamp] = Some(new Timestamp(DateTime.now.getMillis))


}

And my Writes:

implicit val orderFormat : Writes[Order] = (
    ( __ \ 'fk_societe).write[Int] and
    ( __ \ 'order_date).write[String] and
    ( __ \ 'date_creation).write[Timestamp] and
    ( __ \ 'fk_user_author).write[Int] and
    ( __ \ 'fk_statut).write[Int] and
    ( __ \ 'tva).write[Option[Double]] and
    ( __ \ 'total_ht).write[Option[Double]] and
    ( __ \ 'total_ttc).write[Option[Double]] and
    ( __ \ 'note).write[Option[String]]
    )(unlift(Order.unapply)) 

And it works fine except that i need include "id" field in my json but as it's in the body of my case class, it doesn't falls in to the generated value. So if I add "id" field to my Writes:

implicit val orderFormat : Writes[Order] = (
        ( __ \ 'id).write[Int] and
        ( __ \ 'fk_societe).write[Int] and
        .......

It doesn't work as expected... How implement this Writes to include id field? I need a custom apply/unapply method? I stress that my case class is implemented so to works with squeryl KeyedEntity ( id in the body ...)

arussinov
  • 1,237
  • 11
  • 16

1 Answers1

0

So I resolved it this way:

I have created another case class that represents my data as I need: First was :

case class Order(fk_soc : Int, order_date: String, date_creation: Timestamp,fk_uther_author: Int, fk_statut : Int, tva : Option[Double], total_ht: Option[Double],total_ttc: Option[Double], note: Option[String]) extends KeyedEntity[Int] {
  val id : Int = 0
  val ref : String = ""
  val date_modif : Option[Timestamp] = Some(new Timestamp(DateTime.now.getMillis))
}

and the second :

case class OrderJ(id: Int, ref: String, date_modif : Option[Timestamp], fk_soc : Int, order_date: String, date_creation: Timestamp,
                  fk_uther_author: Int, fk_statut : Int, tva : Option[Double], total_ht: Option[Double],
                  total_ttc: Option[Double], note: Option[String])

I updated my Writes with 3 new fields... (id,ref,date_modif)

Then when I need to perform deserialisation first I transform Order object to OrderJ, I use a function for this transformation:

def toOrderJ(o : Order): OrderJ = {
    OrderJ(o.id,o.ref,o.date_modif, o.fk_soc, o.order_date, o.date_creation, o.fk_uther_author,
      o.fk_statut,o.tva, o.total_ht, o.total_ttc, o.note)
  }

And only then I make a call Json.toJson on my OrderJ object and it works as expected. But truly I don't like this approach, It seems to me that there is a more elegant solution. I really hope on your suggestions and opinions.

arussinov
  • 1,237
  • 11
  • 16