creating json array in argonaut scala - scala

I am fairly new to scala , play framework and 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].
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 :-
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
Edit 1
As suggested here is my test case class
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.
Do i need to over-ride them ?
Also how to call this implicit value ?
Edit 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
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.
How to use it ?
remedy.asJson

When you have a case class it is much easier because you can use 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.
In order to make it work you can define your AboutKundli, PlanetStatus and Remedy as case classes and create casecodecs for them in a similar way.
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. 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.
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.

Related

define list from val in object

I wrote this:
def fields(): List[String] = List(Fields.ZONE, Fields.API, Fields.LOCATION, Fields.FACTORY)
object Fields {
val ZONE: String = "Zone"
val API: String = "API"
val LOCATION: String = "location"
val FACTORY: String = "factory"
}
I want to find an intelligent way to define List in def fields without typing manually all constants wrapped in the Fields object.
Any suggestions, please.
Best regards
This is one way to do it
case class FieldNames(
ZONE: String = "Zone",
API: String = "API",
LOCATION: String = "location",
FACTORY: String = "factory",
)
object Fields extends FieldNames
def fields(): List[String] = Fields.productIterator.map(_.toString).toList
This uses the fact that a case class implements Product which allows you to enumerate the fields in the class.
Note that it would be more usual to omit the () and make fields a val:
val fields: List[String] = Fields.productIterator.map(_.toString).toList

Elastic4s search case class example errors when result document is missing a field

I've been working with this example from the Elastic4s manual. It is working fine until it attempts to retrieve a document that does not have a field specified in the case class.
In this example from the manual, let's say one result only had name and was missing the location field. It would yield this error:
java.util.NoSuchElementException: key not found: location
I'm looking for a good approach to deal with search results that have varying fields.
Code sample:
case class Character(name: String, location: String)
implicit object CharacterHitAs extends HitAs[Character] {
override def as(hit: RichSearchHit): Character = {
Character(hit.sourceAsMap("name").toString, hit.sourceAsMap("location").toString) }}
val resp = client.execute {
search in "gameofthrones" / "characters" query "kings landing"
}.await
val characters :Seq[Character] = resp.as[Character]
When developing a case class with optional parameters, use Option:
case class Character(name: String, location: Option[String])
Character("Tyrion Lannister", None)
Then all you have to do is modify your data extractor to pass a None Option if it doesn't find the data:
val tyrion = Map("location" -> "King's Landing", "name" -> "Cersei Lannister")
val cersei = Map("father" -> "Tywin Lannister?", "name" -> "Cersei Lannister")
val jaime = Map("father" -> "Tywin Lannister", "location" -> "Tower of the Hand")
val characters = List(tyrion, cersei, jaime)
case class Character(name: String, location: Option[String])
characters.map(x => Character(x.getOrElse("name", "A CHARACTER HAS NO NAME"), x.get("location")))
The result of characters.map(...) is this:
res0: List[Character] = List(
Character(Cersei Lannister,Some(King's Landing)),
Character(Cersei Lannister,None),
Character(A CHARACTER HAS NO NAME NAME,Some(Tower of the Hand)))
From the source code for RichSearchHit, sourceAsMap should return a Map object:
def sourceAsMap: Map[String, AnyRef] = if (java.sourceAsMap == null) Map.empty else java.sourceAsMap.asScala.toMap
Given that you're using a Map shorthand, you should be able to convert your code to:
case class Character(name: String, location: Option[String])
implicit object CharacterHitAs extends HitAs[Character] {
override def as(hit: RichSearchHit): Character = {
Character(hit.sourceAsMap.getOrElse("name", "A CHARACTER HAS NO NAME"), hit.sourceAsMap.get("location")) }}

Scala immutable container class extended with mixins

I'd like a container class that I can extend with some number of traits to contain groups of default vals that can later be changed in an immutable way. The traits will hold certain simple pieces of data that go together so that creating the class with a couple of traits will create an object with several collections of default values.
Then I'd like to be able to modify any of the vals immutably by copying the object while changing one new value at a time.
The class might have something like the following:
class Defaults(val string: String = "string", val int: Int = "int")
Then other traits like this
trait MoreDefaults{
val long: Long = 1l
}
Then I'd like to mix them when instantiated to build my the particular needed set of defaults
var d = new Defaults with MoreDefaults
and later to something like:
if (someFlag) d = d.copy( long = 1412341234l )
You can do something like this with a single case class but I run out of params at 22. But I'll have a bunch of groupings of defaults I'd like to mixin depending on the need, then allow changes to any of them (class defined or trait defined) in an immutable way.
I can stick a copy method in the Defaults class like this:
def copy(
string: String = string,
int: Int = int): Defaults = {
new Defaults(string, int)
}
then do something like
var d = new Defaults
if (someFlag) d = d.copy(int = 234234)
Question ====> This works for values in the base class but I can't figure how to extend this to the mixin traits. Ideally the d.copy would work on all vals defined by all of the class + traits. Overloading is trouble too since the vals are mainly Strings but all of the val names will be unique in any mix of class and traits or it is an error.
Using only classes I can get some of this functionality by having a base Defaults class then extending it with another class that has it's own non-overloaded copyMoreDefault function. This is really ugly and I hope a Scala expert will see it and have a good laugh before setting me straight--it does work though.
class Defaults(
val string: String = "one",
val boolean: Boolean = true,
val int: Int = 1,
val double: Double = 1.0d,
val long: Long = 1l) {
def copy(
string: String = string,
boolean: Boolean = boolean,
int: Int = int,
double: Double = double,
long: Long = long): Defaults = {
new Defaults(string, boolean, int, double, long)
}
}
class MoreDefaults(
string: String = "one",
boolean: Boolean = true,
int: Int = 1,
double: Double = 1.0d,
long: Long = 1l,
val string2: String = "string2") extends Defaults (
string,
boolean,
int,
double,
long) {
def copyMoreDefaults(
string: String = string,
boolean: Boolean = boolean,
int: Int = int,
double: Double = double,
long: Long = long,
string2: String = string2): MoreDefaults = {
new MoreDefaults(string, boolean, int, double, long, string2)
}
}
Then the following works:
var d = new MoreDefualts
if (someFlag) d = d.copyMoreDefaults(string2 = "new string2")
This method will be a mess if Defaults get's changed parameters! All the derived classes will have to be updated--ugh. There must be a better way.
I don't think I'm strictly speaking answering your question, rather suggesting an alternative solution. So your having problems with large case classes, e.g.
case class Fred(a: Int = 1, b: Int = 2, ... too many params ... )
What I would do is organize the params into more case classes:
case class Bar(a: Int = 1, b: Int = 2)
case class Foo(c: Int = 99, d: Int = 200)
// etc
case class Fred(bar: Bar = Bar(), foo: Foo = Foo(), ... etc)
Then when you want to do a copy and change, say one of the values of Foo you do:
val myFred: Fred = Fred()
val fredCopy: Fred = myFred.copy(foo = myFred.foo.copy(d = 300))
and you need not even define the copy functions, you get them for free.

Deep access of fields in Scala using runtime reflection

I have code that deeply walks a case class' constructor fields, which of course may themselves be complex (list of things, maps, options, and other case classes). The code I found to extract field values at runtime works great on the highest-level fields but explodes when I try to access deeper fields. Example below.
I real life my application introspects the fields at each level, so I know that 'stuff' is another case class (I have the Symbol/Type), and I know Dos' field Symbols/Types. But this is obtained at runtime so I think it's blowing up because it doesn't know [T]/Manifest[T]. Is there a way to get this at runtime via reflection? How might my code change? The examples I found seemed to all require various things[T], which I wouldn't have for 'dos', right?
case class Uno( name:String, age:Int, pets:List[String], stuff:Dos )
case class Dos( foo:String )
object Boom extends App {
val ru = scala.reflect.runtime.universe
val m = ru.runtimeMirror(getClass.getClassLoader)
val u = Uno("Marcus",19,List("fish","bird"),Dos("wow"))
println("NAME: "+unpack(u,"name")) // Works
println("PETS: "+unpack(u,"pets")) // Works
// ----- Goes Boom -------
val dos = unpack(u,"stuff")
println("Other: "+unpack(dos,"foo")) // Boom!
// -----------------------
// Get object value for named parameter of target
def unpack[T]( target:T, name:String )(implicit man:Manifest[T]) : Any = {
val im = m.reflect(target)
val fieldX = ru.typeOf[T].declaration(ru.newTermName(name)).asTerm.accessed.asTerm
im.reflectField(fieldX).get
}
}
You're exactly right, the type of your dos is Any.
FieldMirror.symbol.typeSignature is what you'd get from typeOf[Dos].
So consider returning a pair (Any, Type) from unpack to have something to pass to unpack(target, type, name). Somewhat like:
case class Uno(name: String, age: Int, pets: List[String], stuff: Dos)
case class Dos(foo: String)
object Boom extends App {
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{ currentMirror => cm }
import scala.reflect.ClassTag
val u = Uno("Marcus", 19, List("fish", "bird"), Dos("wow"))
println("NAME: " + unpack(u, "name")) // Works
println("PETS: " + unpack(u, "pets")) // Works
// ----- Goes Boom -------
val (dos, dosT) = unpack(u, "stuff")
println("Other: " + unpack(dos, dosT, "foo")) // Boom! ...or fizzle
// -----------------------
def unpack[T: TypeTag](target: T, name: String): (Any, Type) = unpack(target, typeOf[T], name)
// Get object value for named parameter of target
def unpack[T](target: T, t: Type, name: String): (Any, Type) = {
val im = cm.reflect(target)(ClassTag(target.getClass))
val fieldX = t.declaration(newTermName(name)).asTerm.accessed.asTerm
val fm = im.reflectField(fieldX)
(fm.get, fm.symbol.typeSignature)
}
}

Case Classes with optional fields in Scala

For example, I have this case class:
case class Student (firstName : String, lastName : String)
If I use this case class, is it possible that supplying data to the fields inside the case class are optional? For example, I'll do this:
val student = new Student(firstName = "Foo")
Thanks!
If you just want to miss the second parameter without a default information, I suggest you to use an Option.
case class Student(firstName: String, lastName: Option[String] = None)
Now you might create instances this way:
Student("Foo")
Student("Foo", None) // equal to the one above
Student("Foo", Some("Bar")) // neccesary to add a lastName
To make it usable as you wanted it, I will add an implicit:
object Student {
implicit def string2Option(s: String) = Some(s)
}
Now you are able to call it those ways:
import Student._
Student("Foo")
Student("Foo", None)
Student("Foo", Some("Bar"))
Student("Foo", "Bar")
You were close:
case class Student (firstName : String = "John", lastName : String = "Doe")
val student = Student(firstName = "Foo")
Another possibility is partially applied function:
case class Student (firstName : String, lastName : String)
val someJohn = Student("John", _: String)
//someJohn: String => Student = <function1>
val johnDoe = someJohn("Doe")
//johnDoe: Student = Student(John,Doe)
And to be complete, you can create some default object and then change some field:
val johnDeere = johnDoe.copy(lastName="Deere")
//johnDeer: Student = Student(John,Deere)
I would see two ways this is normally done.
1. default parameters
case class Student (firstName : String, lastName : String = "")
Student("jeypijeypi") # Student(jeypijeypi,)
2. alternative constructors
case class Student (firstName : String, lastName : String)
object Student {
def apply(firstName: String) = new Student(firstName,"")
}
Student("jeypijeypi") # Student(jeypijeypi,)
Which one is better depends slightly on the circumstances. The latter gives you more freedom: you can make any parameter(s) optional, or even change their order (not recommended). Default parameters need always to be at the end of the parameter list, I think. You can also combine these two ways.
Note: within the alternative constructors you need new to point the compiler to the actual constructor. Normally new is not used with case classes.