Circe Encoding Sealed Traits and Co Product Case class Fails - json

I have the following case class:
case class SmartMeterData(
dateInterval: SmartMeterDataInterval = HalfHourInterval(),
powerUnit: PowerUnit = KWH,
smartMeterId: String,
timestamp: String,
value: Double
) {
def timeIntervalInDuration: FiniteDuration = dateInterval match {
case HalfHourInterval(_) => FiniteDuration(30, TimeUnit.MINUTES)
case FullHourInterval(_) => FiniteDuration(60, TimeUnit.MINUTES)
}
}
object SmartMeterData {
sealed trait SmartMeterDataInterval { def interval: String }
case class HalfHourInterval(interval: String = "HH") extends SmartMeterDataInterval
case class FullHourInterval(interval: String = "FH") extends SmartMeterDataInterval
sealed trait PowerUnit
case object WH extends PowerUnit
case object KWH extends PowerUnit
case object MWH extends PowerUnit
}
I just wrote a very simple unit test to see if Circe works for my scenario:
"SmartMeterData" should "successfully parse from a valid JSON AST" in {
val js: String = """
{
"dateInterval" : "HH",
"powerUnit" : "KWH",
"smartMeterId" : "LCID-001-X-54",
"timestamp" : "2012-10-12 00:30:00.0000000",
"value" : 23.0
}
"""
val expectedSmartMeterData = SmartMeterData(smartMeterId = "LCID-001-X-54", timestamp = "2012-10-12 00:30:00.0000000", value = 23.0)
decode[SmartMeterData](js) match {
case Left(err) => fail(s"Error when parsing valid JSON ${err.toString}")
case Right(actualSmartMeterData) =>
assert(actualSmartMeterData equals expectedSmartMeterData)
}
}
But it fails with the following error:
Error when parsing valid JSON DecodingFailure(CNil, List(DownField(dateInterval)))
Is there a known limitation with circe where it does not yet work for my case above?

There's no reason that Circe wouldn't work, but the way you've designed the data model, there's basically zero chance of any Scala JSON library that can automatically generate an encoder/decoder working without a lot of manual work.
For example
sealed trait SmartMeterDataInterval { def interval: String }
case class HalfHourInterval(interval: String = "HH") extends SmartMeterDataInterval
case class FullHourInterval(interval: String = "FH") extends SmartMeterDataInterval
the schema for both, in any Scala JSON library which automatically derives instances for case classes, would be along the lines of
{ "interval": "HH" }
for HalfHourInterval and
{ "interval": "FH" }
for FullHourInterval (i.e. because each has a single string field named interval, they're effectively the same class). In fact your model allows you to have FullHourInterval("HH"), which for at least one method of generating a decoder for an ADT hierarchy in circe (the one in the documentation which uses shapeless) would be the result of decoding { "interval": "HH" }, since that essentially takes the first constructor in lexical order which matches (i.e. FullHourInterval). If the intent is to only allow full- or half-hour intervals, then I'd suggest expressing that as:
case object HalfHourInterval extends SmartMeterDataInterval { def interval: String = "HH" }
case object FullHourInterval extends SmartMeterDataInterval { def interval: String = "FH" }
I'm not directly familiar with how circe encodes case objects, but you can pretty easily define an encoder and decoder for SmartMeterDataInterval:
object SmartMeterDataInterval {
implicit val encoder: Encoder[SmartMeterDataInterval] =
Encoder.encodeString.contramap[SmartMeterDataInterval](_.interval)
implicit val decoder: Decoder[SmartMeterDataInterval] =
Decoder.decodeString.emap {
case "HH" => Right(HalfHourInterval)
case "FH" => Right(FullHourInterval)
case _ => Left("not a valid SmartMeterDataInterval")
}
}
You would then do something similar to define an Encoder/Decoder for PowerUnit

Related

Play JSON Reads Format for Case Objects with Sealed Traits

I have a sealed trait that encapsulates an enum type like below:
sealed trait MyType
object MyType {
case object Type1 extends MyType
case object Type2 extends MyType
}
I will get this as a JSON request and I'm finding it a bit difficult to read the incoming String to a case object. Here is what I have:
implicit val myFormat: Format[MyType] = new Format[MyType] {
override def reads(json: JsValue): JsResult[MyType] = (json \ "type").as[String] match {
case "MyType1" => MyType.Type1
case "MyType2" => MyType.Type2
}
.....
.....
It however fails compilation saying that:
Expected JsResult[MyType] but found MyType.Type1.type
What is wrong with my approach?
Fixed it like this:
implicit val myTypeReads: Reads[MyType] = Reads {
case JsString("MyType1") => JsSuccess(MyType1)
case JsString("MyType2") => JsSuccess(MyType2)
case jsValue => JsError(s"MyType $jsValue is not one of supported [${MyType.values.mkString(",")}]")
}

How to decode a JSON string to a given KClass?

I have my code structure like this:
File 1:
abstract class SomeClass {
abstract fun print()
companion object {
val versions = arrayOf(ClassV1::class, ClassV2::class)
}
}
#Serializable
data class ClassV1(val x: Int) : SomeClass() {
override fun print() {
println("Hello")
}
}
#Serializable
data class ClassV2(val y: String) : SomeClass() {
override fun print() {
println("World")
}
}
File 2:
fun <T : SomeClass> getSomeObject(json: String, kClass: KClass<T>): SomeClass {
return Json.decodeFromString(json)
}
fun printData(version: Int, json: String) {
val someClass: SomeClass = getSomeObject(json, SomeClass.versions[version])
someClass.print()
}
I have a json in printData that is a serialized form of some sub-class of SomeClass. I also have a version which is used to determine which class structure does the json represent. Based on the version, I want to de-serialize my json string to the appropriate sub-class of SomeClass.
Right now the getSomeObject function deserializes the json to SomeClass (which crashes, as expected). I want to know if there is a way I can deserialize it to the provided KClass.
I know I can do this like below:
val someClass = when (version) {
0 -> Json.decodeFromString<ClassV1>(json)
else -> Json.decodeFromString<ClassV2>(json)
}
But I am trying to avoid this since I can have a lot of such versions. Is there a better way possible?
It seems to me that the following is what you are looking for:
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "version",
visible = false)
#JsonSubTypes(
JsonSubTypes.Type(value = ClassV1::class, name = "V1"),
JsonSubTypes.Type(value = ClassV2::class, name = "V2"))
abstract class SomeClass {
(...)
}
This basically means that your JSON would be deserialized as ClassV1 or ClassV2 based on the JSON property version:
V1 would mean that ClassV1 is the target class;
V2 would mean that ClassV2 is the target class.
You can find more information about this at the following online resources:
https://fasterxml.github.io/jackson-annotations/javadoc/2.4/com/fasterxml/jackson/annotation/JsonTypeInfo.html
https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonSubTypes.Type.html
https://www.baeldung.com/jackson-annotations#jackson-polymorphic-type-handling-annotations

Extracting a case class with an upper bound

I want to extract a case class from a JSON String, and reuse the code for every class.
Something like this question would have been perfect. But this means that I have to write for every class I want to extract.
I was hoping to do something like:
abstract class SocialMonitorParser[C <: SocialMonitorData] extends Serializable {
def toJSON(socialMonitorData: C): String = {
Requirements.notNull(socialMonitorData, "This field cannot be NULL!")
implicit val formats = DefaultFormats
write(socialMonitorData)
}
def fromJSON(json: String): Option[C] = {
implicit val formats = DefaultFormats // Brings in default date formats etc.
val jsonObj = liftweb.json.parse(json)
try {
val socialData = jsonObj.extract[C]
Some(socialData)
} catch {
case e: Exception => {
Logger.get(this.getClass.getName).warn("Unable to parse the following JSON:\n" + json + "\nException:\n" + e.toString())
None
}
}
}
}
But it gives me the following error:
Error:(43, 39) No Manifest available for C.
val socialData = jsonObj.extract[C]
Error:(43, 39) not enough arguments for method extract: (implicit formats: net.liftweb.json.Formats, implicit mf: scala.reflect.Manifest[C])C.
Unspecified value parameter mf.
val socialData = jsonObj.extract[C]
I was hoping I could do something like this, and maybe there is a way. But I can't wrap my head around this.
I will try to extend the question with some other information. Supposing I have Twitter and Facebook data, in case class like these:
case class FacebookData(raw_data: String, id: String, social: String) extends SocialMonitorData
case class TwitterData(...) extends SocialMonitorData{ ...}
I wish I could reuse the fromJSON and toJSON just once passing the Upper Bound type
class TwitterParser extends SocialMonitorParser[TwitterData] {
override FromJSON}
class FacebookParser extends SocialMonitorParser[FacebookData]
Much obliged.
I'm not sure why you want SocialMonitorParser to be abstract or Serializable or how are you going to use it but if you look closer at the error you may see that the compilers wants a Manifest for C. Manifest is a Scala way to preserve type information through the type erasure enforced onto generics by JVM. And if you fix that, then the code like this compiles:
import net.liftweb.json._
import net.liftweb.json.Serialization._
trait SocialMonitorData
case class FacebookData(raw_data: String, id: String, social: String) extends SocialMonitorData
class SocialMonitorParser[C <: SocialMonitorData : Manifest] extends Serializable {
def toJSON(socialMonitorData: C): String = {
// Requirements.notNull(socialMonitorData, "This field cannot be NULL!")
implicit val formats = DefaultFormats
write(socialMonitorData)
}
def fromJSON(json: String): Option[C] = {
implicit val formats = DefaultFormats // Brings in default date formats etc.
val jsonObj = parse(json)
try {
val socialData = jsonObj.extract[C]
Some(socialData)
} catch {
case e: Exception => {
// Logger.get(this.getClass.getName).warn("Unable to parse the following JSON:\n" + json + "\nException:\n" + e.toString())
None
}
}
}
}
and you can use it as
def test(): Unit = {
val parser = new SocialMonitorParser[FacebookData]
val src = FacebookData("fb_raw_data", "fb_id", "fb_social")
println(s"src = $src")
val json = parser.toJSON(src)
println(s"json = $json")
val back = parser.fromJSON(json)
println(s"back = $back")
}
to get the output exactly as one would expect.

json4s convert inherited class to json

I am trying to serialize / deserialize to json with json4s native the following case class :
case class UserId(value: String) extends MappedTo[String] with Guid
case class FootballTeamId(value: String) extends Guid
trait Guid extends Id[String] with MappedTo[String]
object Guid {
import scala.reflect.runtime._
import scala.reflect.runtime.universe._
def next[T <: Guid : TypeTag]: T = {
val tt = typeTag[T]
val constructor: MethodSymbol = tt.tpe.members.filter {
m ⇒
m.isMethod && m.asMethod.isConstructor
}.head.asMethod
currentMirror.reflectClass(tt.tpe.typeSymbol.asClass).reflectConstructor(constructor)(UUID.randomUUID().toString).asInstanceOf[T]
}
}
I can do like this :
case object UserIdSerializer extends CustomSerializer[UserId](format => ({
case JString(s) => {
UserId(s)
}
}, {
case s: UserId => JString(s.value)
}))
and have one custom serializer by type, but I'd rather be dry and have one custom serialiser that would do the job for all object of type Guid. I had no luck. I would be grateful for any help I could get on the topic !
Thanks

Vector deserialization by using lift-json

How can i deserialize json array using lift-json to scala vector?
For example:
case class Foo(bar: Vector[Bar])
trait Bar {
def value: Int
}
case class Bar1(value: Int) extends Bar
case class Bar2(value: Int) extends Bar
import net.liftweb.json.{ShortTypeHints, Serialization, DefaultFormats}
implicit val formats = new DefaultFormats {
override val typeHintFieldName = "type"
override val typeHints = ShortTypeHints(List(classOf[Foo],classOf[Bar1],classOf[Bar2]))
}
println(Serialization.writePretty(Foo(Vector(Bar1(1), Bar2(5), Bar1(1)))))
The result is:
{
"type":"Foo",
"bar":[{
"type":"Bar1",
"value":1
},{
"type":"Bar2",
"value":5
},{
"type":"Bar1",
"value":1
}]
}
Good. But when i try to deserialize this string
println(Serialization.read[Foo](Serialization.writePretty(Foo(Vector(Bar1(1), Bar2(5), Bar1(1))))))
i get an exception:
net.liftweb.json.MappingException: Parsed JSON values do not match
with class constructor args=List(Bar1(1), Bar2(5), Bar1(1)) arg
types=scala.collection.immutable.$colon$colon constructor=public
test.Foo(scala.collection.immutable.Vector)
It's means that json array associated with scala list, not vector type that defined in class Foo. I know that there is way to create custom serializer by extending net.liftweb.json.Serializer and include it to formats value. But how can i restore type of objects that stores in Vector. I wanna get result of deserializing like this:
Foo(Vector(Bar1(1), Bar2(5), Bar1(1)))
I've often been annoyed by the List-centricness of Lift, and have found myself needing to do similar things in the past. The following is the approach I've used, adapted a bit for your example:
trait Bar { def value: Int }
case class Bar1(value: Int) extends Bar
case class Bar2(value: Int) extends Bar
case class Foo(bar: Vector[Bar])
import net.liftweb.json._
implicit val formats = new DefaultFormats { outer =>
override val typeHintFieldName = "type"
override val typeHints =
ShortTypeHints(classOf[Bar1] :: classOf[Bar2] :: Nil) +
new ShortTypeHints(classOf[Foo] :: Nil) {
val FooName = this.hintFor(classOf[Foo])
override def deserialize = {
case (FooName, foo) => foo \ "bar" match {
case JArray(bars) => Foo(
bars.map(_.extract[Bar](outer, manifest[Bar]))(collection.breakOut)
)
case _ => throw new RuntimeException("Not really a Foo.")
}
}
}
}
Kind of ugly, and could probably be cleaned up a bit, but it works.
You could add an implicit conversion:
implicit def listToVect(list:List[Bar]):Vector[Bar] = list.map(identity)(breakOut)
after that, Serialization.read[Foo] works as expected.