alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (LottoExample.scala)

This example Lift Framework source code file (LottoExample.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

fail, fail, int, int, list, list, long, long, lotto, lottoexample, option, specification, winner

The Lift Framework LottoExample.scala source code

package net.liftweb.json.scalaz

import scalaz._
import Scalaz._
import JsonScalaz._
import net.liftweb.json._

import org.specs.Specification

object LottoExample extends Specification {
  case class Winner(winnerId: Long, numbers: List[Int])
  case class Lotto(id: Long, winningNumbers: List[Int], winners: List[Winner], drawDate: Option[String])

  val json = parse("""{"id":5,"winning-numbers":[2,45,34,23,7,5],"winners":[{"winner-id":23,"numbers":[2,45,34,23,3,5]},{"winner-id":54,"numbers":[52,3,12,11,18,22]}]}""")

  // Lotto line must have exactly 6 numbers
  def len(x: Int) = (xs: List[Int]) => 
    if (xs.length != x) Fail("len", xs.length + " != " + x) else xs.success

  // FIXME enable when 2.8 no longer supported, 2.9 needs: import Validation.Monad._
/*
  // Note 'apply _' is not needed on Scala 2.8.1 >=
  implicit def winnerJSON: JSONR[Winner] =
    Winner.applyJSON(field("winner-id"), validate[List[Int]]("numbers") >=> len(6) apply _)

  implicit def lottoJSON: JSONR[Lotto] =
    Lotto.applyJSON(field("id")
                  , validate[List[Int]]("winning-numbers") >=> len(6) apply _
                  , field("winners")
                  , field("draw-date"))
  
  val winners = List(Winner(23, List(2, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22)))
  val lotto = Lotto(5, List(2, 45, 34, 23, 7, 5), winners, None)

  fromJSON[Lotto](json) mustEqual Success(lotto)
  */
}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework LottoExample.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.