How to check against an empty JSON - scala

I am using https://circe.github.io/circe/ and would like to figure out, if a property has an empty JSON object or not.
For example:
val json: String = """
{
"id": "c730433b-082c-4984-9d66-855c243266f0",
"name": "Foo",
"counts": [1, 2, 3],
"values": {
}
}
"""
As you can see on the code above, the property values is an empty JSON structure.
How to validate, if a property is empty or not?

There are lots of ways you could do this. For example:
import io.circe.jawn.parse
def valuesIsEmpty(in: String): Option[Boolean] = for {
parsed <- parse(in).right.toOption
parsedObj <- parsed.asObject
values <- parsedObj("values")
valuesObj <- values.asObject
} yield valuesObj.size == 0
And then:
scala> valuesIsEmpty(json)
res0: Option[Boolean] = Some(true)
Here None would indicate that the input is not valid JSON or isn't an object with a values member.
In general you wouldn't perform validation at this level, though—you'd build it into your decoder. For example:
import io.circe.Decoder, io.circe.generic.semiauto.deriveDecoder
case class Entry(id: String, name: String, counts: List[Int], values: Map[String, String])
implicit val decodeEntry: Decoder[Entry] = deriveDecoder[Entry].emap {
case e if e.values.isEmpty => Left("empty values")
case e => Right(e)
}
And then:
scala> io.circe.jawn.decode[Entry](json)
res0: Either[io.circe.Error,Entry] = Left(DecodingFailure(empty values, List()))

Related

Get type of primitive field from an object using Scala reflection

So I am attempting to grab the types of each field in a Scala object class:
package myapp.model
object MyObject {
val theInt: Option[Int]
}
Using the ReflectionHelper so graciously provided by Brian in this post. I use getFieldType but it returns Option[Object] instead of what it is, which is Option[Int]. The example code in that answer works for a case class, for example:
package myapp.model
case class Person(
name: String,
age: Option[Int]
)
scala> ReflectionHelper.getFieldType("myapp.model.Person", "age") // int
res12: Option[reflect.runtime.universe.Type] = Some(Option[Int])
However, if I run getFieldType on a Scala object field, we get this:
scala> ReflectionHelper.getFieldType("myapp.model.MyObject$", "theInt")
res10: Option[reflect.runtime.universe.Type] = Some(Option[Object])
What is different about Scala objects that causes this behavior and how can I get getFieldType to return Option[Int] instead of Option[Object] like it does for the case class?
Here is the ReflectionHelper from the other question for convenience:
import scala.reflect.runtime.{ universe => u }
import scala.reflect.runtime.universe._
object ReflectionHelper {
val classLoader = Thread.currentThread().getContextClassLoader
val mirror = u.runtimeMirror(classLoader)
def getFieldType(className: String, fieldName: String): Option[Type] = {
val classSymbol = mirror.staticClass(className)
for {
fieldSymbol <- classSymbol.selfType.members.collectFirst({
case s: Symbol if s.isPublic && s.name.decodedName.toString() == fieldName => s
})
} yield {
fieldSymbol.info.resultType
}
}
def maybeUnwrapFieldType[A](fieldType: Type)(implicit tag: TypeTag[A]): Option[Type] = {
if (fieldType.typeConstructor == tag.tpe.typeConstructor) {
fieldType.typeArgs.headOption
} else {
Option(fieldType)
}
}
def getFieldClass(className: String, fieldName: String): java.lang.Class[_] = {
// case normal field return its class
// case Option field return generic type of Option
val result = for {
fieldType <- getFieldType(className, fieldName)
unwrappedFieldType <- maybeUnwrapFieldType[Option[_]](fieldType)
} yield {
mirror.runtimeClass(unwrappedFieldType)
}
// Consider changing return type to: Option[Class[_]]
result.getOrElse(null)
}
}
Try
import scala.reflect.runtime.universe._
import scala.reflect.runtime
val runtimeMirror = runtime.currentMirror
runtimeMirror.staticClass("myapp.model.Person").typeSignature
.member(TermName("age")).typeSignature // => Option[Int]
runtimeMirror.staticModule("myapp.model.MyObject").typeSignature
.member(TermName("theInt")).typeSignature // => Option[Int]

Circe: force an optional field to `null`

Is there a way to serialize a single None field to "null" ?
For example:
// When None, I'd like to serialize only f2 to `null`
case class Example(f1: Option[Int], f2: Option[Int])
val printer = Printer.noSpaces.copy(dropNullValues = true)
Example(None, None).asJson.pretty(printer) === """{"f2":null}"""
You can do this pretty straightforwardly by mapping a filter over the output of an encoder (which can be derived, defined with Encoder.forProductN, etc.):
import io.circe.{ Json, ObjectEncoder }
import io.circe.generic.semiauto.deriveEncoder
case class Example(f1: Option[Int], f2: Option[Int])
val keepSomeNulls: ((String, Json)) => Boolean = {
case ("f1", v) => !v.isNull
case (_, _) => true
}
implicit val encodeExample: ObjectEncoder[Example] =
deriveEncoder[Example].mapJsonObject(_.filter(keepSomeNulls))
And then:
scala> import io.circe.syntax._
import io.circe.syntax._
scala> Example(Some(1), Some(2)).asJson.noSpaces
res0: String = {"f1":1,"f2":2}
scala> Example(Some(1), None).asJson.noSpaces
res1: String = {"f1":1,"f2":null}
scala> Example(None, Some(2)).asJson.noSpaces
res2: String = {"f2":2}
scala> Example(None, None).asJson.noSpaces
res3: String = {"f2":null}
Note that configuring a printer to drop null values will still remove "f2": null here. This is part of the reason I think in general it's best to make the preservation of null values solely the responsibility of the printer, but in a case like this, the presence or absence of null-valued fields is clearly semantically meaningful, so you kind of have to mix up the levels.

Scala create a function to run on existing type

For simplicities sake I removed all the types from what I intend to do and using a simple list here. Imagine I have a function which takes two parameters: a collection and a function and uses this to produce a new collection.
I know! this looks like reinventing map but the actual use case is a lot more different and complex. What I tried so far is something like:
trait SomeHelper {
class CoolFunction( right: String => Boolean ) extends Function1[List[String], List[Any]] {
inst =>
override def apply( left: List[ String ] ): List[ Any ] = {
left match {
case a: List[String] => a.filter( right )
case _ => List()
}
}
}
def coolFunction( right: String => Boolean ) = new CoolFunction( right )
}
object SomeHelper extends SomeHelper
import SomeHelper._
val myList = List("apple", "orange", "banana")
myList coolFunction {
a => a == "orange"
}
produces me a "value coolFunction is not a member of List[String]"
feels like I am so close but can't figure it out
I guess you could use an implicit class. They allow to add a method onto existing types.
implicit class MyListHelper(list: List[String]) {
def doStuff(function: String => Any): List[Any] = {
list.map(function)
}
}
And use it like this:
import MyListHelper
val list = List("a", "b", "c")
val result = list.doStuff(s => s + s) // List("aa", "bb", "cc")

Is it possible to parse JSON array into a tuple with json4s?

Take the following example:
import org.json4s.native.JsonMethods._
import org.json4s._
implicit val formats = DefaultFormats
case class A(name: String)
case class B(age: Int)
val json = parse("""[ {"name": "mark"}, { "age": 27 }, 5 ]""")
json.extract[Tuple3[A, B, Int]]
This errors out:
org.json4s.package$MappingException: No usable value for _1 No usable
value for name Did not find value which can be converted into
java.lang.String
Json4s scalaz seems to have tuple support. I am not sure if there is any built in way to do this in json4s. I generally solved this issue something like this
implicit val formats = DefaultFormats
class MySerializer extends CustomSerializer[Tuple3[A,B,Int]](format => (
{
case JArray(x :: y :: z :: Nil ) => {
( x.extract[A], y.extract[B], z.extract[Int])}
},
{
case x:Tuple3[A,B,Int] => null
}
))
And then from your code do something like this
implicit val formats = DefaultFormats + new MySerializer
val json = parse("""[ {"name": "mark"}, { "age": 27 }, 5 ]""")
json.extract[Tuple3[A,B,Int]]

Merge two case class of same type, except some fields

If you have a case class like:
case class Foo(x: String, y: String, z: String)
And you have two instances like:
Foo("x1","y1","z1")
Foo("x2","y2","z2")
Is it possible to merge instance 1 in instance 2, except for field z, so that the result would be:
Foo("x1","y1","z2")
My usecase is just that I give JSON objects to a Backbone app through a Scala API, and the Backbone app gives me back a JSON of the same structure so that I can save/update it. These JSON objects are parsed as case class for easy Scala manipulation. But some fields should never be updated by the client side (like creationDate). For now I'm doing a manual merge but I'd like a more generic solution, a bit like an enhanced copy function.
What I'd like is something like this:
instanceFromDB.updateWith(instanceFromBackbone, excludeFields = "creationDate" )
But I'd like it to be typesafe :)
Edit:
My case class have a lot more fields and I'd like the default bevavior to merge fields unless I explicitly say to not merge them.
What you want is already there; you just need to approach the problem the other way.
case class Bar(x: String, y: String)
val b1 = Bar("old", "tired")
val b2 = Bar("new", "fresh")
If you want everything in b2 not specifically mentioned, you should copy from b2; anything from b1 you want to keep you can mention explicitly:
def keepY(b1: Bar, b2: Bar) = b2.copy(y = b1.y)
scala> keepY(b1, b2)
res1: Bar = Bar(new,tired)
As long as you are copying between two instances of the same case class, and the fields are immutable like they are by default, this will do what you want.
case class Foo(x: String, y: String, z: String)
Foo("old_x", "old_y", "old_z")
// res0: Foo = Foo(old_x,old_y,old_z)
Foo("new_x", "new_y", "new_z")
// res1: Foo = Foo(new_x,new_y,new_z)
// use copy() ...
res0.copy(res1.x, res1.y)
// res2: Foo = Foo(new_x,new_y,old_z)
// ... with by-name parameters
res0.copy(y = res1.y)
// res3: Foo = Foo(old_x,new_y,old_z)
You can exclude class params from automatic copying by the copy method by currying:
case class Person(name: String, age: Int)(val create: Long, val id: Int)
This makes it clear which are ordinary value fields which the client sets and which are special fields. You can't accidentally forget to supply a special field.
For the use case of taking the value fields from one instance and the special fields from another, by reflectively invoking copy with either default args or the special members of the original:
import scala.reflect._
import scala.reflect.runtime.{ currentMirror => cm }
import scala.reflect.runtime.universe._
import System.{ currentTimeMillis => now }
case class Person(name: String, age: Int = 18)(val create: Long = now, val id: Int = Person.nextId) {
require(name != null)
require(age >= 18)
}
object Person {
private val ns = new java.util.concurrent.atomic.AtomicInteger
def nextId = ns.getAndIncrement()
}
object Test extends App {
/** Copy of value with non-defaulting args from model. */
implicit class Copier[A: ClassTag : TypeTag](val value: A) {
def copyFrom(model: A): A = {
val valueMirror = cm reflect value
val modelMirror = cm reflect model
val name = "copy"
val copy = (typeOf[A] member TermName(name)).asMethod
// either defarg or default val for type of p
def valueFor(p: Symbol, i: Int): Any = {
val defarg = typeOf[A] member TermName(s"$name$$default$$${i+1}")
if (defarg != NoSymbol) {
println(s"default $defarg")
(valueMirror reflectMethod defarg.asMethod)()
} else {
println(s"def val for $p")
val pmethod = typeOf[A] member p.name
if (pmethod != NoSymbol) (modelMirror reflectMethod pmethod.asMethod)()
else throw new RuntimeException("No $p on model")
}
}
val args = (for (ps <- copy.paramss; p <- ps) yield p).zipWithIndex map (p => valueFor(p._1,p._2))
(valueMirror reflectMethod copy)(args: _*).asInstanceOf[A]
}
}
val customer = Person("Bob")()
val updated = Person("Bobby", 37)(id = -1)
val merged = updated.copyFrom(customer)
assert(merged.create == customer.create)
assert(merged.id == customer.id)
}
case class Foo(x: String, y: String, z: String)
val foo1 = Foo("x1", "y1", "z1")
val foo2 = Foo("x2", "y2", "z2")
val mergedFoo = foo1.copy(z = foo2.z) // Foo("x1", "y1", "z2")
If you change Foo later to:
case class Foo(w: String, x: String, y: String, z: String)
No modification will have to be done. Explicitly:
val foo1 = Foo("w1", "x1", "y1", "z1")
val foo2 = Foo("w2", "x2", "y2", "z2")
val mergedFoo = foo1.copy(z = foo2.z) // Foo("w1", "x1", "y1", "z2")