简体   繁体   English

在argonaut scala中创建json数组

[英]creating json array in argonaut scala

I am fairly new to scala , play framework and argonaut. 我对scala,游戏框架和argonaut相当陌生。 Following is my case class which contains a variable of type List[Remedy] My aim is to generate a json response of the PredictionModel object which contains JsonArray of List[Remedy]. 以下是我的案例类,其中包含类型为List [Remedy]的变量。我的目标是生成PredictionModel对象的json响应,该对象包含List [Remedy]的JsonArray。

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 = ""

}

Implicit for PredictionModel is written like :- 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)

Everything about runs just fine, but what should i do to add JsonArray in prodiction Json object 关于一切的一切都很好,但我应该怎么做才能在生产Json对象中添加JsonArray

Edit 1 As suggested here is my test case class 编辑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")
}

While declaring casecodec20 i have an error in apply and unapply method. 声明casecodec20时,apply和unapply方法出错。 Do i need to over-ride them ? 我需要超越他们吗? Also how to call this implicit value ? 还如何调用此隐式值?

Edit 2 编辑2

So here is what needs to be done. 所以这是需要做的。 Create case class with all the parameters in constrcutor and a subsequent object class which contains CodecJson like below 使用构造函数中的所有参数以及随后的包含CodecJson的对象类创建案例类,如下所示

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

}

object Remedy{

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

In my case i had more complex models inside models so i just created implicit CodecJson for all. 就我而言,我在模型内部拥有更复杂的模型,因此我只为所有人创建了隐式CodecJson。 How to use it ? 如何使用它 ?

 remedy.asJson

When you have a case class it is much easier because you can use casecodec : 当拥有一个case类时,它会容易得多,因为您可以使用casecodec

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

And now you have a JSON codec for your PredictionModel which will encode and decode your case class from/to json. 现在,您的PredictionModel具有JSON编解码器,它将对json中的case类进行编码和解码。

In order to make it work you can define your AboutKundli , PlanetStatus and Remedy as case classes and create casecodec s for them in a similar way. 为了使其工作,您可以将AboutKundliPlanetStatusRemedy定义为案例类,并以类似的方式为其创建casecodec

Note that it is highly recommended to declare case classes in a way where all the paramerers are defined in their constructors and not how you show in your example. 请注意,强烈建议以所有参数在其构造函数中定义的方式声明case类,而不是在示例中显示方式。 A better way would be: 更好的方法是:

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)

It is not only stylistic and there are technical reasons to do so (related to how apply/unapply/equality are generated for the case class) so it is better to follow this practice. 这样做不仅是风格上的,而且有技术上的原因(与案例类如何生成适用/不适用/平等有关),因此最好遵循这种做法。

In general you should follow the rule that case classes are immutable and all the values are declared as their constructor parameters. 通常,您应该遵循以下规则:case类是不可变的,并且所有值都声明为其构造函数参数。

Once you have declared codecs for your case classes you have codecs for lists "for free" because Argonaut can encode List[A] if it can see that it can encode that A , so you don't need to do anything special here. 在为案例类声明了编解码器之后,就可以“免费”使用列表的编解码器,因为Argonaut如果可以看到List[A]可以对A进行编码,就可以对其进行编码,因此您无需在此处进行任何特殊操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM