1

我正在 Scala 中开发我的第一个 Play 2.1 应用程序。我要完成的任务是将 json 解析为 3 个不同的案例类。问题是 - 我不知道在哪里声明所有案例类。每个类都在它自己的文件中或全部在一个文件中。这是我所做的(它不起作用,case 值在控制器对象中不可见):

  1. 文件 LoginBase.scala

    package models
    
    
    abstract class LoginBase 
    
    case class Login(email: String, password: String) extends         LoginBase
    case class RestoreLogin(email: String, captchaID: String,         captchaAnswer: String) extends LoginBase
    case class RegisterLogin(email: String, password: String,         captchaID: String, captchaAnswer: String) extends LoginBase
    
  2. 文件

    package controllers
    
    import play.api._
    import play.api.mvc._
    import play.api.data._
    import play.api.data.Forms._
    import play.api.Play.current
    import play.api.cache.Cache
    import play.api.libs.json._
    import play.api.mvc._
    import play.api.libs.functional.syntax._
    
    import nl.captcha.Captcha
    import nl.captcha.Captcha._
    import nl.captcha.gimpy.FishEyeGimpyRenderer
    import nl.captcha.text.renderer.ColoredEdgesWordRenderer
    import nl.captcha.text.renderer.DefaultWordRenderer
    import nl.captcha.gimpy.DropShadowGimpyRenderer
    
    import java.io.ByteArrayOutputStream
    import javax.imageio.ImageIO
    import java.util.UUID
    
    import play.api.libs.functional.syntax._
    import models.LoginBase
    
    object LoginActions extends Controller {
    
    
        implicit val loginReads = (
          (__ \ "email").read[String] and
          (__ \ "password").read[String]
        )(Login.apply _) 
    
    
        implicit val restoreLoginReads = (
          (__ \ "email").read[String] and
          (__ \ "captchaID").read[String] and
          (__ \ "captchaAnswer").read[String]
        )(RestoreLogin) 
    
        implicit val registerLoginReads = (
          (__ \ "email").read[String] and
          (__ \ "password").read[String] and
          (__ \ "captchaID").read[String] and
          (__ \ "captchaAnswer").read[String]
        )(RegisterLogin) 
    
    
    
        def registerLogin = Action(parse.json){
            /*  To be implementd */
        }
    
    }
    

如果有人可以帮助我,我将不胜感激。我能找到的只是 REPL 示例,但我缺少一些基本的东西——把所有的代码放在哪里?什么是文件结构?

谢谢,安德烈。

4

2 回答 2

1

Having multiple classes and objects inside a single file is considered good form in Scala, as long as the classes are tightly related.

please refer Elements of Scala Style? for more detail answer.

Here is example of converting json to scala case class in restful ws.

enter code here

import play.api.libs.json.{Reads, JsError, Json}
import play.api.libs.json.JsValue

object someController e

xtends Controller{

case class SomeResource(val Id:String, val someType:String, val Serialno:String)

implicit val reads:Reads[SomeResource] = Json.reads[SomeResource]

/**
   * save some object
   */
  def someService = Action(parse.json){request=>
    unmarshalOrderResource(request, (resource: SomeResource) => {
            val someValue = SomeResource(resource.Id,
                            resource.someType,resource.Serialno);
            someservice.save(someValue);
            Created(someValue.Id)
        })
  }


// method
 private def unmarshalOrderResource(request: Request[JsValue],
                                      block: (SomeResource) => Result): Result = {
        request.body.validate[SomeResource].fold(
            valid = block,
            invalid = (e => {
                val error = e.mkString
                Logger.error(error)
                BadRequest(error)
            })
        )
    }

}
于 2013-11-21T19:42:49.347 回答
0

Java 和 Scala 之间的不同之处之一是 Scala 不需要特定的文件/文件夹结构。这些包甚至不必与文件夹相对应。此外,每个文件可以有多个类。

基本上,你所拥有的一切都很好。

必须保持不变的是进口。因此,如果您问为什么您的东西无法编译,请尝试导入模型包中的所有内容。

import models._
于 2013-04-22T19:42:05.117 回答