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

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

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

import net.liftweb.json._

import scalax.collection.config.CoreConfig
import scalax.collection.generic.Edge

/** This custom serializer is to be registered whenever a class to be (de)serialized contains any `Graph` instances.
  *  For usage see `scalax.collection.io.json.serializer.TGraphSerializer`.
  */
final class GraphSerializer[N, E <: Edge[N]](descriptor: Descriptor[N])(implicit edgeManifest: Manifest[E])
    extends Serializer[AnyGraph[N, E]] {

  override def deserialize(implicit format: Formats) = {
    case (TypeInfo(clazz, _), json) if clazz.isAssignableFrom(classOf[AnyGraph[N, E]]) =>
      json match {
        case JObject(_) =>
          mutable.Graph.fromJson[N, E](json, descriptor)(CoreConfig())
        case x => throw new MappingException("Can't convert " + x + " to " + clazz.getName)
      }
  }

  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case graph: AnyGraph[N @unchecked, E @unchecked] =>
      val `export` = new exp.Export(graph, descriptor)
      import `export`._
      jsonAST(List(jsonASTNodes, jsonASTEdges))
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy