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

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]]

Related

How to write custom circe codec for Map[String, Any]

Is it possible to write custom Decoder for Map[String, Any] using circe? i've found this, but it's only conversion to Json:
def mapToJson(map: Map[String, Any]): Json =
map.mapValues(anyToJson).asJson
def anyToJson(any: Any): Json = any match {
case n: Int => n.asJson
case n: Long => n.asJson
case n: Double => n.asJson
case s: String => s.asJson
case true => true.asJson
case false => false.asJson
case null | None => None.asJson
case list: List[_] => list.map(anyToJson).asJson
case list: Vector[_] => list.map(anyToJson).asJson
case Some(any) => anyToJson(any)
case map: Map[String, Any] => mapToJson(map)
}
This Circe decoder will convert its Json into Map[String, Any]:
import io.circe.JavaDecoder._
implicit val objDecoder: Decoder[Any] = {
case x: HCursor if x.value.isObject =>
x.value.as[java.util.Map[String, Any]]
case x: HCursor if x.value.isString =>
x.value.as[String]
case x: HCursor if x.value.isBoolean =>
x.value.as[Boolean]
case x: HCursor if x.value.isArray =>
x.value.as[java.util.List[Any]]
case x: HCursor if x.value.isNumber =>
x.value.as[Double]
case x: HCursor if x.value.isNull =>
x.value.as[Unit]}
import io.circe.parser._
parse("""{"foo": 1, "bar": null, "baz": {"a" : "hi", "b" : true, "dogs" : [{ "name" : "Rover", "age" : 4}, { "name" : "Fido", "age" : 5 }] } }""")
.getOrElse(Json.Null)
.as[Map[String, _]]. // <-- this is the call to convert from Circe Json to the Java Map
It would be nicer to case match on the cursor value, but those classes are not public.
Note that I encoded null as Unit, which isn't quite right -- Scala and null don't play nicely and you may need to tailor your implementation to your use case. I actually omit that case entirely from my own implementation because I'm decoding Json that was encoded from Scala instances.
You also need to create a custom Decoder in io.circe for Java Map and List. I did the following, which uses package-private Circe code, making it concise but also vulnerable to changes in Circe and forcing you to have your own io.circe package.
package io.circe
import java.util.{List => JavaList, Map => JavaMap}
import scala.collection.mutable
import scala.collection.immutable.{List => ScalaList, Map => ScalaMap}
import scala.jdk.CollectionConverters.{MapHasAsJava, SeqHasAsJava}
object JavaDecoder {
implicit final def decodeJavaList[A](implicit decodeA: Decoder[A]): Decoder[JavaList[A]] = new SeqDecoder[A, JavaList](decodeA) {
final protected def createBuilder(): mutable.Builder[A, JavaList[A]] =
ScalaList.newBuilder[A].mapResult(_.asJava)
}
implicit final def decodeJavaMap[K, V](implicit
decodeK: KeyDecoder[K],
decodeV: Decoder[V]
): Decoder[JavaMap[K, V]] = new JavaMapDecoder[K, V, JavaMap](decodeK, decodeV) {
final protected def createBuilder(): mutable.Builder[(K, V), JavaMap[K, V]] =
ScalaMap.newBuilder[K, V].mapResult(_.asJava)
}
}
package io.circe
import scala.collection.{Map => ScalaMap}
import scala.collection.mutable
import scala.jdk.CollectionConverters.MapHasAsJava
abstract class JavaMapDecoder[K, V, M[K, V] <: java.util.Map[K, V]](
decodeK: KeyDecoder[K],
decodeV: Decoder[V]
) extends Decoder[M[K, V]] {
private val mapDecoder = new MapDecoder[K, V, ScalaMap](decodeK, decodeV) {
override protected def createBuilder(): mutable.Builder[(K, V), ScalaMap[K, V]] =
ScalaMap.newBuilder[K, V]
}
override def apply(c: io.circe.HCursor): io.circe.Decoder.Result[M[K,V]] =
mapDecoder.apply(c).map(_.asJava.asInstanceOf[M[K, V]])
}
Fwiw, I'm already thinking of submitting this as a PR to Circe.
import io.circe.syntax.EncoderOps
import io.circe.{Encoder, Json}
case class Person(name: String, age: Int)
object Person {
implicit val decoder: io.circe.Decoder[Person] = io.circe.generic.semiauto.deriveDecoder
implicit val encoder: io.circe.Encoder[Person] = io.circe.generic.semiauto.deriveEncoder
}
case class Home(area: Int)
object Home {
implicit val decoder: io.circe.Decoder[Home] = io.circe.generic.semiauto.deriveDecoder
implicit val encoder: io.circe.Encoder[Home] = io.circe.generic.semiauto.deriveEncoder
}
def jsonPrinter[A](obj: A)(implicit encoder: Encoder[A]): Json =
obj.asJson
jsonPrinter(Person("Eminem", 30))
jsonPrinter(Home(300))
This is done with generics, I hope it helps

json4s "Can't convert JString(2019-04-28T01:23:45.678Z) to class java.time.Instant"?

I'm struggling to get json4s to work with java.time.Instant values in my case class. The REPL looks like this.
scala> case class TestTime(tag: String, t: java.time.Instant)
defined class TestTime
scala> import org.json4s._
import org.json4s._
scala> import org.json4s.ext.JavaTimeSerializers
import org.json4s.ext.JavaTimeSerializers
scala> import org.json4s.native.JsonMethods._
import org.json4s.native.JsonMethods._
scala> implicit lazy val formats = DefaultFormats ++ JavaTimeSerializers.all
formats: org.json4s.Formats = <lazy>
scala> val parsed = parse("""{"tag": "second","t": "2019-04-28T01:23:45.678Z"}""")
parsed: org.json4s.JValue = JObject(List((tag,JString(second)), (t,JString(2019-04-28T01:23:45.678Z))))
scala> val second = parsed.extract[TestTime]
org.json4s.package$MappingException: No usable value for t
Can't convert JString(2019-04-28T01:23:45.678Z) to class java.time.Instant
at org.json4s.reflect.package$.fail(package.scala:95)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:569)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$3.applyOrElse(Extraction.scala:593)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$3.applyOrElse(Extraction.scala:591)
at scala.PartialFunction.$anonfun$runWith$1$adapted(PartialFunction.scala:145)
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
at scala.collection.TraversableLike.collect(TraversableLike.scala:274)
at scala.collection.TraversableLike.collect$(TraversableLike.scala:272)
at scala.collection.AbstractTraversable.collect(Traversable.scala:108)
at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:591)
at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:651)
at org.json4s.Extraction$.$anonfun$extract$10(Extraction.scala:410)
at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:658)
at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)
at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)
at scala.PartialFunction$$anon$1.applyOrElse(PartialFunction.scala:257)
at org.json4s.Extraction$.customOrElse(Extraction.scala:658)
at org.json4s.Extraction$.extract(Extraction.scala:402)
at org.json4s.Extraction$.extract(Extraction.scala:40)
at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
... 36 elided
Caused by: org.json4s.package$MappingException: Can't convert JString(2019-04-28T01:23:45.678Z) to class java.time.Instant
at org.json4s.CustomSerializer$$anonfun$deserialize$2.applyOrElse(Formats.scala:450)
at org.json4s.CustomSerializer$$anonfun$deserialize$2.applyOrElse(Formats.scala:447)
at org.json4s.Extraction$.customOrElse(Extraction.scala:658)
at org.json4s.Extraction$.extract(Extraction.scala:402)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:554)
... 56 more
scala> val instant = java.time.Instant.parse("2014-12-03T10:15:30.00Z")
instant: java.time.Instant = 2014-12-03T10:15:30Z
How do I get rid of that stacktrace and get this extraction to work?
Looking at the source, the custom serializer for Instant will only parse Int values, not String values:
case object JInstantSerializer extends CustomSerializer[Instant]( format => (
{
case JInt(d) => Instant.ofEpochMilli(d.toLong)
case JNull => null
},
{
case d: Instant => JInt(d.toEpochMilli)
}
))
You can add a rule to parse String to Instant to fix the problem.
case object MyInstantSerialzer extends CustomSerializer[Instant]( format => (
{
case JInt(d) => Instant.ofEpochMilli(d.toLong)
case JString(s) => Instant.parse(s)
case JNull => null
},
{
case d: Instant => JInt(d.toEpochMilli)
}
))
implicit lazy val formats = DefaultFormats + MyInstantSerialzer
Personally I think this is broken because it does not preserve the accuracy of Instant when read/writing to JSON. This feels better:
case object MyInstantSerialzer extends CustomSerializer[Instant]( format => (
{
case JString(s) => Instant.parse(s)
case JNull => null
},
{
case s: Instant => JString(s.toString)
}
))
There is also a more complicated solution in the answer to this question

Creating JSObject with JsArray from List[Long]

I need to create a JsObject with JsArray from List[Long]. I though there is something called JsLong
def someWork(someList: List[Long]): JsObject = {
JsObject(("someField", JsArray(someList.map(JsLong))) :: Nil)
}
apprently there is not. Any suggestion how I can create it?
There is only one numeric type in JavaScript/JSON, and that's Number. It looks like you're using Play-JSON, which has JsNumber, which can be constructed with a BigDecimal. (Just convert the Long to a BigDecimal).
import play.api.libs.json._
def someWork(someList: List[Long]): JsObject = {
Json.obj(
"someField" -> JsArray(someList.map(a => JsNumber.apply(BigDecimal(a))))
)
}
scala> someWork(List(1, 2, 3, 4))
res0: play.api.libs.json.JsObject = {"someField":[1,2,3,4]}

Json4s: Trouble while trying to convert Json attribute to java.sql.Date

I am using Json4s to deserialize json messages. I have a case class like
case class A(id: Int, b: Option[java.sql.Date])
Whenever I try to convert a json message to case class A, I get none as the value of b
scala> read[A]("""{"id":1,"b":12345}""")
res2: A = A(1,None)
scala> read[A]("""{"id":1,"b":"12345"}""")
res3: A = A(1,None)
scala> read[A]("""{"id":1,"b":"12/12/2014"}""")
res4: A = A(1,None)
How can I fix this issue
Something along these lines (you might want to be more specific with formats). And then mix this trait in the classes which need to have access to this custom serializer.
import org.json4s.DefaultJsonFormats._
trait JsonFormats {
case object DateSerializer extends CustomSerializer[java.sql.Date](format => (
{
case JString(s) => Date.valueOf(s)
case JNull => null
},
{
case d: Date => JString(d.toString())
}
)
)
implicit val json4sFormats = native.Serialization.formats(NoTypeHints) + DateSerializer
}

Scala: convert map to case class

Let's say I have this example case class
case class Test(key1: Int, key2: String, key3: String)
And I have a map
myMap = Map("k1" -> 1, "k2" -> "val2", "k3" -> "val3")
I need to convert this map to my case class in several places of the code, something like this:
myMap.asInstanceOf[Test]
What would be the easiest way of doing that? Can I somehow use implicit for this?
Two ways of doing this elegantly. The first is to use an unapply, the second to use an implicit class (2.10+) with a type class to do the conversion for you.
1) The unapply is the simplest and most straight forward way to write such a conversion. It does not do any "magic" and can readily be found if using an IDE. Do note, doing this sort of thing can clutter your companion object and cause your code to sprout dependencies in places you might not want:
object MyClass{
def unapply(values: Map[String,String]) = try{
Some(MyClass(values("key").toInteger, values("next").toFloat))
} catch{
case NonFatal(ex) => None
}
}
Which could be used like this:
val MyClass(myInstance) = myMap
be careful, as it would throw an exception if not matched completely.
2) Doing an implicit class with a type class creates more boilerplate for you but also allows a lot of room to expand the same pattern to apply to other case classes:
implicit class Map2Class(values: Map[String,String]){
def convert[A](implicit mapper: MapConvert[A]) = mapper conv (values)
}
trait MapConvert[A]{
def conv(values: Map[String,String]): A
}
and as an example you'd do something like this:
object MyObject{
implicit val new MapConvert[MyObject]{
def conv(values: Map[String, String]) = MyObject(values("key").toInt, values("foo").toFloat)
}
}
which could then be used just as you had described above:
val myInstance = myMap.convert[MyObject]
throwing an exception if no conversion could be made. Using this pattern converting between a Map[String, String] to any object would require just another implicit (and that implicit to be in scope.)
Here is an alternative non-boilerplate method that uses Scala reflection (Scala 2.10 and above) and doesn't require a separately compiled module:
import org.specs2.mutable.Specification
import scala.reflect._
import scala.reflect.runtime.universe._
case class Test(t: String, ot: Option[String])
package object ccFromMap {
def fromMap[T: TypeTag: ClassTag](m: Map[String,_]) = {
val rm = runtimeMirror(classTag[T].runtimeClass.getClassLoader)
val classTest = typeOf[T].typeSymbol.asClass
val classMirror = rm.reflectClass(classTest)
val constructor = typeOf[T].decl(termNames.CONSTRUCTOR).asMethod
val constructorMirror = classMirror.reflectConstructor(constructor)
val constructorArgs = constructor.paramLists.flatten.map( (param: Symbol) => {
val paramName = param.name.toString
if(param.typeSignature <:< typeOf[Option[Any]])
m.get(paramName)
else
m.get(paramName).getOrElse(throw new IllegalArgumentException("Map is missing required parameter named " + paramName))
})
constructorMirror(constructorArgs:_*).asInstanceOf[T]
}
}
class CaseClassFromMapSpec extends Specification {
"case class" should {
"be constructable from a Map" in {
import ccFromMap._
fromMap[Test](Map("t" -> "test", "ot" -> "test2")) === Test("test", Some("test2"))
fromMap[Test](Map("t" -> "test")) === Test("test", None)
}
}
}
Jonathan Chow implements a Scala macro (designed for Scala 2.11) that generalizes this behavior and eliminates the boilerplate.
http://blog.echo.sh/post/65955606729/exploring-scala-macros-map-to-case-class-conversion
import scala.reflect.macros.Context
trait Mappable[T] {
def toMap(t: T): Map[String, Any]
def fromMap(map: Map[String, Any]): T
}
object Mappable {
implicit def materializeMappable[T]: Mappable[T] = macro materializeMappableImpl[T]
def materializeMappableImpl[T: c.WeakTypeTag](c: Context): c.Expr[Mappable[T]] = {
import c.universe._
val tpe = weakTypeOf[T]
val companion = tpe.typeSymbol.companionSymbol
val fields = tpe.declarations.collectFirst {
case m: MethodSymbol if m.isPrimaryConstructor ⇒ m
}.get.paramss.head
val (toMapParams, fromMapParams) = fields.map { field ⇒
val name = field.name
val decoded = name.decoded
val returnType = tpe.declaration(name).typeSignature
(q"$decoded → t.$name", q"map($decoded).asInstanceOf[$returnType]")
}.unzip
c.Expr[Mappable[T]] { q"""
new Mappable[$tpe] {
def toMap(t: $tpe): Map[String, Any] = Map(..$toMapParams)
def fromMap(map: Map[String, Any]): $tpe = $companion(..$fromMapParams)
}
""" }
}
}
This works well for me,if you use jackson for scala:
def from[T](map: Map[String, Any])(implicit m: Manifest[T]): T = {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.convertValue(map)
}
Reference from:Convert a Map<String, String> to a POJO
I don't love this code, but I suppose this is possible if you can get the map values into a tuple and then use the tupled constructor for your case class. That would look something like this:
val myMap = Map("k1" -> 1, "k2" -> "val2", "k3" -> "val3")
val params = Some(myMap.map(_._2).toList).flatMap{
case List(a:Int,b:String,c:String) => Some((a,b,c))
case other => None
}
val myCaseClass = params.map(Test.tupled(_))
println(myCaseClass)
You have to be careful to make sure the list of values is exactly 3 elements and that they are the correct types. If not, you end up with a None instead. Like I said, not great, but it shows that it is possible.
commons.mapper.Mappers.mapToBean[CaseClassBean](map)
Details: https://github.com/hank-whu/common4s
Here's an update to Jonathon's answer for Scala 3 (which no longer has TypeTag). Be aware that this won't work for case classes nested inside of other classes. But for top-level case classes it seems to work fine.
import scala.reflect.ClassTag
object Reflect:
def fromMap[T <: Product : ClassTag](m: Map[String, ?]): T =
val classTag = implicitly[ClassTag[T]]
val constructor = classTag.runtimeClass.getDeclaredConstructors.head
val constructorArgs = constructor.getParameters()
.map { param =>
val paramName = param.getName
if (param.getType == classOf[Option[_]])
m.get(paramName)
else
m.get(paramName)
.getOrElse(throw new IllegalArgumentException(s"Missing required parameter: $paramName"))
}
constructor.newInstance(constructorArgs: _*).asInstanceOf[T]
And a test for the above:
case class Foo(a: String, b: Int, c: Option[String] = None)
case class Bar(a: String, b: Int, c: Option[Foo])
class ReflectSuite extends munit.FunSuite:
test("fromMap") {
val m = Map("a" -> "hello", "b" -> 42, "c" -> "world")
val foo = Reflect.fromMap[Foo](m)
assertEquals(foo, Foo("hello", 42, Some("world")))
val n = Map("a" -> "hello", "b" -> 43)
val foo2 = Reflect.fromMap[Foo](n)
assertEquals(foo2, Foo("hello", 43))
val o = Map("a" -> "yo", "b" -> 44, "c" -> foo)
val bar = Reflect.fromMap[Bar](o)
assertEquals(bar, Bar("yo", 44, Some(foo)))
}
test("fromMap should fail when required parameter is missing") {
val m = Map("a" -> "hello", "c" -> "world")
intercept[java.lang.IllegalArgumentException] {
Reflect.fromMap[Foo](m)
}
}