在argonaut scala中创建json数组

时间:2016-01-25 09:15:37

标签: scala playframework

我对scala,玩框架和argonaut相当新。 以下是我的case类,其中包含List [Remedy]类型的变量 我的目标是生成包含List [Remedy]的JsonArray的PredictionModel对象的json响应。

package models

import scala.collection.mutable.ListBuffer
import argonaut._, Argonaut._


/**
 * Created by abhishek on 15/01/16.
 */
    case class PredictionModel() {

  var aboutKundali: AboutKundli = new AboutKundli()
  var planetStatus = new ListBuffer[PlanetStatus].toList
  var donate = ""
  var notToDonate = ""
  var colorNotToWear = ""
  var favouredGod = ""
  var aboutEducationAndOccupation = ""
  var mixMahaDashaCurrent = ""
  var mixMahaDashaNotCurrent = ""
  var category = ""
  var rinnPitri = ""
  var lifeHead = ""
  var disease = ""
  var occupation = ""
  var marriedLife = ""
  var santan = ""
  var parents = ""
  var nature = ""
  var remedyList = new ListBuffer[Remedy].toList
  var importantInformation = ""

}

PredictionModel的隐式编写如下: -

implicit def predicationEncodeJson: EncodeJson[PredictionModel] =
    EncodeJson((prediction: PredictionModel) =>
      ("about_kundali" := argonaut.Json(
        "birth_rashi" := prediction.aboutKundali.birthRashi,
        "lagan_rashi" := prediction.aboutKundali.laganRashi,
        "birth_day_planet" := prediction.aboutKundali.birthDayPlanet,
        "birth_time_planet" := prediction.aboutKundali.birthTimePlanet,
        "current_maha_dasha" := prediction.aboutKundali.currentMahaDasha,
        "lucky_day" := prediction.aboutKundali.luckyDay,
        "lucky_number" := prediction.aboutKundali.luckyNumber
      )) ->:
        ("important_information" := prediction.importantInformation) ->:
        ("rinnPitri" := prediction.rinnPitri) ->:
        ("category" := prediction.category) ->:
        ("mix_mahadasha_not_current" := prediction.mixMahaDashaNotCurrent) ->:
        ("mix_mahadasha_current" := prediction.mixMahaDashaCurrent) ->:
        ("about_education_and_occupation" := prediction.aboutEducationAndOccupation) ->:
        ("favored_god" := prediction.favouredGod) ->:
        ("color_not_to_wear" := prediction.colorNotToWear) ->:
        ("donate" := prediction.donate) ->:
        ("not_to_donate" := prediction.notToDonate) ->: jEmptyObject)

关于运行的一切都很好,但我应该怎么做才能在产生Json对象中添加JsonArray

修改1 正如这里建议的那样是我的测试用例类

import argonaut._, Argonaut._

case class TestModel(id: Int, name: String) {

}

object TestModel{
  implicit def PredictionModelCodecJson = CodecJson[TestModel] =
    casecodec20(TestModel.apply, TestModel.unapply)("id", "name")
}

在声明casecodec20时,我在apply和unapply方法中有错误。 我需要超越他们吗? 另外如何调用这个隐含值?

修改2

所以这是需要做的。 使用constrcutor中的所有参数创建case类,并创建一个包含CodecJson的后续对象类,如下所示

case class Remedy(no: Int, description: String) {

}

object Remedy{

  implicit def RemedyCodecJson: CodecJson[Remedy] =
    casecodec2(Remedy.apply, Remedy.unapply)("number", "description")
}

在我的情况下,我在模型中有更复杂的模型,所以我只为所有人创建了隐式CodecJson。 怎么用?

 remedy.asJson

1 个答案:

答案 0 :(得分:1)

当你有一个案例类时,它会更容易,因为你可以使用casecodec

implicit def PredictionModel CodecJson: CodecJson[PredictionModel] =
    casecodec20(PredictionModel.apply, PredictionModel.unapply)("aboutKundali", "planetStatus", "donate", .....)

现在你有了PredictionModel的JSON编解码器,它将从/到json对你的case类进行编码和解码。

为了使其有效,您可以将AboutKundliPlanetStatusRemedy定义为案例类,并以类似的方式为它们创建casecodec

请注意,强烈建议以一种方式声明case类,其中所有参数都在其构造函数中定义,而不是在示例中显示的方式。更好的方法是:

case class PredictionModel(
  aboutKundali: AboutKundli,
  planetStatus: List[PlanetStatus],
  donate: String,
  notToDonate: String
  colorNotToWear: String
  favouredGod: String,
  aboutEducationAndOccupation: String,
  mixMahaDashaCurrent: String,
  mixMahaDashaNotCurrent: String,
  category: String,
  rinnPitri: String,
  lifeHead: String,
  disease: String,
  occupation: String,
  marriedLife: String,
  santan: String,
  parents: String,
  nature: String,
  remedyList: List[Remedy],
  importantInformation: String)

这不仅具有风格,而且还有技术上的理由(与如何为案例类生成apply / unapply / equality相关),因此最好遵循这种做法。

通常,您应该遵循以下规则:案例类是不可变的,并且所有值都被声明为构造函数参数。

一旦您为您的案例类声明了编解码器,您就拥有列表的编解码器"免费"因为Argonaut可以编码List[A],如果它可以看到它可以编码A,那么你不需要在这里做任何特别的事情。