Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization of abstract class #180

Open
ariens opened this issue Apr 12, 2016 · 1 comment
Open

Deserialization of abstract class #180

ariens opened this issue Apr 12, 2016 · 1 comment

Comments

@ariens
Copy link

ariens commented Apr 12, 2016

Looking for some best practices for dealing with a use case that involves serialization and deserialization of an object hierarchy that based off an abstract class. I have the serialization working as expected but currently no good method for determining the appropriate class to use for deserialization. The only approach I can envision is to a) not allow deserialization (throwing the exception as per the below) or determining if the value can be made into an int, then long, then float, then double (but that would likely be terribly inefficient). Can anyone recommend a best practice using spray-json?

abstract class Metric
case class LongMetric(metric: String, timestamp: Long, value: Long) extends Metric
case class IntMetric(metric: String, timestamp: Long, value: Int) extends Metric
case class FloatMetric(metric: String, timestamp: Long, value: Float) extends Metric
case class DoubleMetric(metric: String, timestamp: Long, value: Double) extends Metric

object MetricJsonProtocol extends DefaultJsonProtocol {
  implicit object MetricJsonFormat extends RootJsonFormat[Metric] {

    def write(m: Metric) = m match {
      case longMetric: LongMetric     => longMetric.toJson
      case intMetric: IntMetric       => intMetric.toJson
      case floatMetric: FloatMetric   => floatMetric.toJson
      case doubleMetric: DoubleMetric => doubleMetric.toJson
    }
    def read(value: JsValue) = value match {
      case _ => deserializationError("deserialization of abstract class not supported")
    }
  }
  implicit val longMetricFmt = jsonFormat3(LongMetric)
  implicit val intMetricFmt = jsonFormat3(IntMetric)
  implicit val floatMetricFmt = jsonFormat3(FloatMetric)
  implicit val doubleMetricFmt = jsonFormat3(DoubleMetric)
}
@TimurFayruzov
Copy link

In this situation we changed writer to add 'type' field that captures the concrete type and use this information at deserialization time. I would be curious too to see whether there are more elegant ways to solve this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants