I am trying to convert com.google.gson.JsonObject to case class object. Sometime some element in the record are missing in which case I want it to be assigned as None to case class member
object tmp {
case class person(name: String, age: Long)
def main(args: Array[String]): Unit = {
val parser = new JsonParser();
//PERSON 2
val record2 = parser.parse("""{"name":"xyz"}""").getAsJsonObject()
val record2_name = record2.get("name").getAsString.toLowerCase
val record2_age = if(record2.get("age") != null) record2.get("age") else None
val person2 = new person(record2_name,record2_age) //-> Type Mismatch, expected: Long, actual: Long
println(person2);
}
}
In your case record2_age is of type JsonElement which is simply of type java Object, but since you are assigning None to it, it becomes Any WHICH you are trying to assign to type Long.
Short answer would be ,
val person = new person(jsonObject.get("name").getAsString.toLowerCase,
Option(jsonObject.get("age")).map(_.getAsLong).getOrElse(0l))
This way if jsonObject.get("age") is null, Option(null) gives you None, or if present you get Option(28) = Some(28)
You probably want your age to be 0l if empty. If you want it to be None, then you can use Option[Long].
1) simpler way
class GsonCaseClassSpecs extends FunSpec {
describe("case class conversion") {
it("converts gson with name/age to case class") {
case class person(name: String, age: Long)
val parser = new JsonParser()
//case 1
val jsonObject : JsonObject = parser.parse("""{"name":"xyz"}""").getAsJsonObject
val age = jsonObject.get("age") match {
case null => None
case value => Some(value.getAsLong)
}
val person1 = new person(jsonObject.get("name").getAsString.toLowerCase, age.getOrElse(0l))
assert(person1.name == "xyz")
assert(person1.age == 0)
//case 2
val jsonObject2 : JsonObject = parser.parse("""{"name":"xyz", "age" : 28}""").getAsJsonObject
val age2 : Option[Long] = jsonObject2.get("age") match {
case null => None
case value => Some(value.getAsLong)
}
val person2 = new person(jsonObject2.get("name").getAsString.toLowerCase, age2.getOrElse(0l))
assert(person2.name == "xyz")
assert(person2.age == 28)
}
}
}
2) If you want to make the age to be Option[Long]. Option[T] can have Some(x) or None.
class CaseClassFunSpecs extends FunSpec {
it("converts json to case class with empty age"){
case class person(name: String, age: Option[Long])
val parser = new JsonParser()
val json = parser.parse("""{"name":"xyz"}""").getAsJsonObject()
val personInstance = new person(json.get("name").getAsString.toLowerCase,
Option(json.get("age")).map(_.getAsLong))
assert(personInstance.name == "xyz")
assert(personInstance.age == None)
// NOTE
// do not do personInstance.age.get which would throw
// None.get
// java.util.NoSuchElementException: None.get
// at scala.None$.get(Option.scala:347)
// at scala.None$.get(Option.scala:345)
//rather use pattern match
personInstance.age match {
case Some(x) => println("value = " + x)
case None => throw new RuntimeException("Age can not be empty.")
}
}
it("converts json to case class with non-empty age"){
case class person(name: String, age: Option[Long])
val parser = new JsonParser()
val json = parser.parse("""{"name":"xyz", "age" : 28}""").getAsJsonObject()
val personInstance = new person(json.get("name").getAsString.toLowerCase,
Option(json.get("age")).map(_.getAsLong))
assert(personInstance.name == "xyz")
assert(personInstance.age == Some(28))
assert(personInstance.age.get == 28) //.get gives you the value
}
}
You simply can't call a parameter of one type, age: Long in your case, with an argument of a different type Option[Long].
At least this is what your question seems to imply.
You need to define the attributes that could be missing as Options.
e.g. in your example
case class person(name: String, age: Option[Long])
and then when you extract the json value you can make use of the Option.apply method, that returns None if the argument is null
…
val record2_age = Option(record2.get("age")).map(_.getAsLong) // None if get("age) returns null
…
val person2 = new person(record2_name,record2_age) //There should be no more type mismatch now
Related
I have 2 case classes:
case class Person(fname: String, lname: String, age: Int, address: String)
case class PersonUpdate(fname: String, lname: String, age: Int)
so PersonUpdate dosent have all the fields Person have, and I want to write effective that get Person and PersonUpdate and find the fields that have different values:
for example:
def findChangedFields(person: Person, personUpdate: PersonUpdate): Seq[String] = {
var listOfChangedFields: List[String] = List.empty
if (person.fname == personUpdate.fname)
listOfChangedFields = listOfChangedFields :+ "fname"
if (person.lname == personUpdate.lname)
listOfChangedFields = listOfChangedFields :+ "lname"
if (person.age == personUpdate.age)
listOfChangedFields = listOfChangedFields :+ "age"
listOfChangedFields
}
findChangedFields(per, perUpdate)
but this is very ugly, how can I write this nicely with the magic of scala?
Something like this, maybe?
val fields = Seq("fname", "lname", "age")
val changedFields = person.productIterator
.zip(personUpdate.productIterator)
.zip(fields.iterator)
.collect { case ((a, b), name) if a != b => name }
.toList
Something like this:
case class Person(fname: String, lname: String, age: Int, address: String)
case class PersonUpdate(fname: String, lname: String, age: Int)
def findFirstNameChanged(person: Person, personUpdate: PersonUpdate): List[String] =
{
if (person.fname == personUpdate.fname) List("fname")
else Nil
}
def findLastNameChanged(person: Person, personUpdate: PersonUpdate): List[String] = {
if (person.lname == personUpdate.lname) List("lname")
else Nil
}
def findAgeNameChanged(person: Person, personUpdate: PersonUpdate): List[String] = {
if (person.age == personUpdate.age) List("age")
else Nil
}
def findChangedFields(person: Person, personUpdate: PersonUpdate): Seq[String] = {
findFirstNameChanged(person,personUpdate):::
findLastNameChanged(person,personUpdate) ::: findAgeNameChanged(person,personUpdate)
}
val per = Person("Pedro","Luis",22,"street")
val personUpdate = PersonUpdate("Pedro", "Luis",27)
findChangedFields(per, personUpdate)
I think your problem is similar to compare two Set of tuple. Please feel free two correct me.
So here is my solution which will work for any two case class having field names in any order
def caseClassToSet[T](inp: T)(implicit ct: ClassTag[T]): Set[(String, AnyRef)] = {
ct.runtimeClass.getDeclaredFields.map(f => {
f.setAccessible(true)
val res = (f.getName, f.get(inp))
f.setAccessible(false)
res
}).toSet
}
val person = Person("x", "y", 10, "xy")
val personUpdate = PersonUpdate("z","y",12)
val personParams: Set[(String, AnyRef)] = caseClassToSet(person)
val personUpdateParams: Set[(String, AnyRef)] = caseClassToSet(personUpdate)
println(personUpdateParams diff personParams)
Got help from Get field names list from case class
My case classes looks like this:
case class Person(personalInfo: PersonalInfo, bankInfo: BankInfo)
case class PersonalInfo(fname: String, lname: String)
case class BankInfo(atmCode: Int, creditCard: CreditCard)
case class CreditCard(number: Int, experationDate: String)
so to be able to get a person in my controller I added serealizer for person:
object PersonSerealizer {
implicit val PersonalInfoFormat: OFormat[PersonalInfo] = Json.format[PersonalInfo]
implicit val CreditCardFormat: OFormat[CreditCard] = Json.format[CreditCard]
implicit val BankInfoFormat: OFormat[BankInfo] = Json.format[BankInfo]
implicit val PersonFormat: OFormat[Person] = Json.format[Person]
}
in my controller I have a super simple action that looks like this:
i
mport serializers.PersonSerealizer._
def getBrothers(): Action[JsValue] = Action.async(parse.json) { request =>
request.body.validate[Person] match {
case JsSuccess(person, _) =>
brothersService.getBrothers(person) // this returns a List[Person]
.map(res => Future{Ok(res)})
case JsError(errors) => Future(BadRequest("Errors! " + errors.mkString))
}
}
but I get this error:
Error:(23, 81) No unapply or unapplySeq function found for class
BankInfo: / implicit val BankInfoFormat:
OFormat[BankInfo] = Json.format[BankInfo]
something is weird...from what I know it supposed to work
The order of defining your implicits seems to matter. Also, I think it is safer to use companion objects instead of defining them inside an arbitrary object.
case class PersonalInfo(fname: String, lname: String)
object PersonalInfo {
implicit val personalInfoJsonFormat = Json.format[PersonalInfo]
}
case class CreditCard(number: Int, experationDate: String)
object CreditCard {
implicit val creditCardJsonFormat = Json.format[CreditCard]
}
case class BankInfo(atmCode: Int, creditCard: CreditCard)
object BankInfo {
implicit val bankInfoJsonFormat = Json.format[BankInfo]
}
case class Person(personalInfo: PersonalInfo, bankInfo: BankInfo)
object Person {
implicit val personJsonFmt = Json.format[Person]
}
// Your action will be like this
def getBrothers() = Action.async(parse.json) { req =>
req.body.validate[Person] match {
case JsSuccess(p, _) =>
Future.successful(Ok(Json.toJson(service.getBrothers(p))))
case JsError(errors) =>
Future.successful(BadRequest(JsError.toJson(errors)))
}
}
I am trying to understand the scala unapply method.
Below is my understanding. Say if I have a Person object:
class Person(val fname: String, val lname: String)
object Person{
def unapply(x: Person) : Option[(String, String)] =
Some(x.fname,x.lname)
}
new Person("Magic", "Mike") match {
case Person(x, y) => s"Last Name is ${y}"
case _ => "Unknown"
}
This i presume the case calls something like:
val temp = Person.unapply(new Person("Magic", "Mike"))
if (temp != None) { val (x, y) = temp.get }
else { <go to next case> }
but how does below unapply work when i have like below:
new Person("Magic", "Mike") match {
case Person("Harold", y) => s"Last Name is ${y}"
case Person("Magic", y) => s"Last Name is ${y}"
case _ => "Unknown"
}
How does it access the value of fname("Magic") in unapply method and give me the same/correct result as the first one?
Running scalac with -Xprint:patmat will show you how syntactic trees look after pattern matching phase:
scalac -Xprint:patmat test.scala
case <synthetic> val x1: Person = new Person("Magic", "Mike");
case10(){
<synthetic> val o12: Option[(String, String)] = Person.unapply(x1);
if (o12.isEmpty.unary_!)
{
<synthetic> val p3: String = o12.get._1;
val y: String = o12.get._2;
if ("Harold".==(p3))
matchEnd9(scala.StringContext.apply("Last Name is ", "").s(y))
else
case11()
}
else
case11()
};
case11(){
<synthetic> val o14: Option[(String, String)] = Person.unapply(x1);
if (o14.isEmpty.unary_!)
{
<synthetic> val p5: String = o14.get._1;
val y: String = o14.get._2;
if ("Magic".==(p5))
matchEnd9(scala.StringContext.apply("Last Name is ", "").s(y))
else
case13()
}
else
case13()
};
case13(){
matchEnd9("Unknown")
};
As you can see, for each case first it calls unapply on the matched object, then if Option is not empty (so it has matched), it checks if one of the elements of tuple is equal to expected value, if so then it matches to the closure for this case.
In order to get height of a binary tree I use this code :
object optionfun {
println("Welcome to the Scala worksheet")
case class Node(var isVisited: Boolean, var leftNode: Option[Node], var rightNode: Option[Node], name: String) {
def this(name: String) = this(false, None, None, name)
}
val a = new Node("a")
val b = new Node("b")
val c = new Node("c")
a.leftNode = Some(b)
a.rightNode = Some(c)
def getHeight(root: Option[Node]): Int = {
//if root contains a None type then it should return 0, should I pattern match on the option type here?
Math.max(getHeight(root.leftNode),
getHeight(root.rightNode.get)) + 1
}
getHeight(a)
}
But I receive compiler error for line :
Math.max(getHeight(root.leftNode)
error is :
Multiple markers at this line - value leftNode is not a member of Option[optionfun.Node] - value
leftNode is not a member of Option[Node]
I've mixed up the types somehow but I don't know what to pass into getHeight method "root.leftNode" is of type Option ?
The problem with accepting root as an Option[Node] is that you can't just call root.leftNode, you have to map or match the Option.
You could re-write it without the Option like this:
def getHeight(root: Node): Int = {
Math.max(
root.leftNode.map(getHeight(_)).getOrElse(0),
root.rightNode.map(getHeight(_)).getOrElse(0)
) + 1
}
Or with Option:
def getHeight(root: Option[Node]): Int = root match {
case Some(r) => Math.max(getHeight(r.leftNode), getHeight(r.rightNode)) + 1
case None => 0
}
I have created a class called CaseInsensitive which wraps a string (see Implementing a string class that does case insensitive comparisions in Scala).
I've created a case class which has a member variable of type CaseInsensitive, so it gets a default unapply method, which extracts a variable of type CaseInsensitive, but I was hoping to use it like this:
case class PropertyKey( val name : CaseInsensitive )
val foo = new PropertyKey("foo")
val result = foo match {
case PropertyKey("foo") => true
case _ => false
}
This code fails to compile: (on the extractor line, not the constructor line)
type mismatch;
found : java.lang.String("foo")
required: com.acme.CaseInsensitive
But I thought my implicit conversions from String to CaseInsensitive would enable this to compile, rather than me having to type the more verbose:
case class PropertyKey( val name : CaseInsensitive )
val foo = new PropertyKey("foo")
val result = foo match {
case PropertyKey(CaseInsensitive("foo")) => true
case _ => false
}
Here is the implementation of CaseInsensitive:
/** Used to enable us to easily index objects by string, case insensitive
*
* Note: this class preserve the case of your string!
*/
case class CaseInsensitive ( val _s : String ) extends Proxy {
require( _s != null)
val self = _s.toLowerCase
override def toString = _s
def i = this // convenience implicit conversion
}
object CaseInsensitive {
implicit def CaseInsensitive2String(c : CaseInsensitive) = if ( c == null ) null else c._s
implicit def StringToCaseInsensitive(s : String) = CaseInsensitive(s)
def fromString( s : String ) = s match {
case null => None
case _ => Some(CaseInsensitive(s))
}
}
You could always define a convenience extractor and import it (feel free to use a shorter name):
object PropertyKeyCI {
def unapply(p: PropertyKey): Option[String] = Some(p.name.self)
}
Then, extraction is:
val foo = new PropertyKey("foo")
val result = foo match {
case PropertyKeyCI("foo") => true
case _ => false
}
(Bad Semantics Alert)
Although note that this would match as false for PropertyKeyCI("Foo"), because your "CaseInsensitive" class is really a "LowerCase" class. I say this because it is hard for me to imagine what the desired behavior would be for the unapply() method otherwise. From your case class default, you are returning an Option[String] of the original (unlowercased) string, which gives this unintuitive behavior:
// result == false !!!!
val foo = new CaseInsensitive("Foo")
val result = foo match {
case CaseInsensitive("foo") => true
case _ => false
}
val CaseInsensitive(s) = "aBcDeF"
assertFalse(s == "abcdef")
Awwww.... toss it. Just use DOS.