Created
April 15, 2015 12:37
-
-
Save mfirry/cd356be1749cdcdb1e2c to your computer and use it in GitHub Desktop.
Foolin' around with the idea that Pickling could work with EDN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scala.pickling | |
import scala.pickling.internal._ | |
import scala.language.implicitConversions | |
package object edn extends EdnFormats { | |
} | |
package edn { | |
import scala.reflect.runtime.universe._ | |
import definitions._ | |
import scala.collection.mutable.{StringBuilder, Stack} | |
trait EdnFormats { | |
implicit val pickleFormat: EdnPickleFormat = new EdnPickleFormat | |
implicit def toEdnPickle(value: String): EdnPickle = EdnPickle(value) | |
implicit def ednPickleToUnpickleOps(value: String): UnpickleOps = new UnpickleOps(EdnPickle(value)) | |
} | |
case class EdnPickle(value: String) extends Pickle { | |
type ValueType = String | |
type PickleFormatType = EdnPickleFormat | |
} | |
class EdnPickleFormat extends PickleFormat { | |
type PickleType = EdnPickle | |
type OutputType = Output[String] | |
def createBuilder() = new EdnPickleBuilder(this, new StringOutput) | |
def createBuilder(out: Output[String]): PBuilder = new EdnPickleBuilder(this, out) | |
def createReader(pickle: EdnPickle) = ??? | |
} | |
class EdnPickleBuilder(format: EdnPickleFormat, buf: Output[String]) extends PBuilder with PickleTools { | |
import scaledn._ | |
import write._ | |
private def append(s: String) = { | |
buf.put(s) | |
} | |
private val tags = new Stack[FastTypeTag[_]]() | |
private val primitives = Map[String, Any => Unit]( | |
FastTypeTag.Unit.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.Null.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.Ref.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.Int.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.Long.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.Short.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.Double.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.Float.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.Boolean.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.Byte.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.Char.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.String.key -> ((picklee: Any) => append(toEDNString(picklee))), | |
FastTypeTag.ArrayByte.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.ArrayShort.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.ArrayChar.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.ArrayInt.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.ArrayLong.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.ArrayBoolean.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.ArrayFloat.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly")), | |
FastTypeTag.ArrayDouble.key -> ((picklee: Any) => throw new Error("fatal error: shouldn't be invoked explicitly"))) | |
def beginCollection(length: Int): scala.pickling.PBuilder = {println("beginCollection"); this} | |
def beginEntry(picklee: Any): scala.pickling.PBuilder = withHints { hints => | |
if (primitives.contains(hints.tag.key)) { | |
primitives(hints.tag.key)(picklee) | |
} else { | |
val ts = | |
if (hints.tag.key.contains("anonfun$")) picklee.getClass.getName | |
else hints.tag.key | |
append("\"$type\": \"" + ts + "\"") | |
} | |
this | |
} | |
def endCollection(): Unit = println("endCollection") | |
def endEntry(): Unit = { | |
println("endEntry") | |
} | |
def putElement(pickler: scala.pickling.PBuilder => Unit): scala.pickling.PBuilder = { | |
pickler(this) | |
this | |
} | |
def putField(name: String,pickler: scala.pickling.PBuilder => Unit): scala.pickling.PBuilder = { | |
pickler(this) | |
this | |
} | |
def result(): scala.pickling.Pickle = { | |
println("result") | |
EdnPickle(buf.toString) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment