All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalax.collection.io.json.serializer.edgeSerializers.scala Maven / Gradle / Ivy

The newest version!
package scalax.collection.io.json
package serializer

import net.liftweb.json._

import scalax.collection.io.edge._
import scalax.collection.io.edge.Types._
import scalax.collection.{OneOrMore, Several}
import error.JsonGraphError._

/** Serializes `EdgeParameters` to a JSON array of the form ["",""] and reversely
  * where  and  represent the node-Ids.
  */
class EdgeSerializer extends Serializer[EdgeParameters] {
  override def deserialize(implicit format: Formats) = {
    case (_, JArray(JString(n1) :: JString(n2) :: Nil))                        => EdgeParameters(n1, n2)
    case (_, JObject(JField(_, JString(n1)) :: JField(_, JString(n2)) :: Nil)) => EdgeParameters(n1, n2)
    case (TypeInfo(clazz, _), x) => throw couldNotConvertException(x, clazz)
  }

  override def serialize(implicit format: Formats) = { case EdgeParameters(nodeId_1, nodeId_2) =>
    JArray(JString(nodeId_1) :: JString(nodeId_2) :: Nil)
  }
}

/** Serializes `LEdgeParameters[Double]` to a JSON array of the form `["", "", ]` and reversely
  * where `` and `` represent the node-Ids and `` a JSON number mapping to `Double`.
  */
class WEdgeSerializer extends Serializer[LEdgeParameters[Double]] {
  override def deserialize(implicit format: Formats) = {
    case (_, JArray(JString(n1) :: JString(n2) :: JDouble(weight) :: Nil)) => LEdgeParameters(n1, n2, weight)
    case (TypeInfo(clazz, _), x)                                           => throw couldNotConvertException(x, clazz)
  }

  override def serialize(implicit format: Formats) = { case LEdgeParameters(nId_1, nId_2, weight: Double) =>
    JArray(JString(nId_1) :: JString(nId_2) :: JDouble(weight) :: Nil)
  }
}

/** @define LSERIALIZER one or more lift-json custom `Serializer`s for labels.
  */
abstract class LSerializer[L: Manifest](labelSerializers: Serializer[L]*) {
  object LabelSerialization {
    implicit val labelFormats: Formats = Serialization.formats(NoTypeHints) ++ labelSerializers
    def extract(json: JValue)          = json.extract[L]
    def decompose(label: L)            = Extraction.decompose(label)
  }
}

/** Serializes `LEdgeParameters` to a JSON array of the form `["", "", 




© 2015 - 2025 Weber Informatics LLC | Privacy Policy