我正在尝试为遵循以下结构化类型的 ADT 数据类型制作通用序列化程序
/**
* Our standard ADT representation, indexed by ID. ADT's also have a formattedString (formal name representing ADT)
*/
type ADT = {def id: Long; def formattedName:String}
/**
* This is a structured type that forces the companion objects associated with an ADT to have an all value. This all
* value is an enumeration of all of the sum types of the ADT, and is generated by a macro
* @tparam A
*/
type ADTCompanion[A] = {val all:Set[A]}
我还编写了处理 ADT 的辅助函数,即
/**
* Look up an sum type by its id
* @param companion The companion object being looked up
* @param id The id to look up
* @tparam A The type of the companion object
* @return The corresponding sum type
*/
def getADT[A <: ADT](companion:ADTCompanion[A], id:Long) = {
var re:Option[A] = None
try {
for (item <- companion.all) {
if (item.id == id) {
re = Some(item)
}
}
re.getOrElse(throw new InvalidId(companion,id))
} catch {
case e:Throwable => throw new InvalidId(companion,id)
}
}
/**
* Look up a sum type by its formattedName
* @param companion The companion object being looked up
* @param formattedName The formattedName to look up
* @tparam A The type of the companion object
* @return The corresponding sum type
*/
def getADT[A <: ADT](companion:ADTCompanion[A], formattedName:String) = {
var re:Option[A] = None
try {
for (item <- companion.all) {
if (item.formattedName == formattedName) {
re = Some(item)
}
}
re.getOrElse(throw new InvalidFormattedName(companion,formattedName))
} catch {
case e:Throwable => throw new InvalidFormattedName(companion,formattedName)
}
}
现在我的问题在以下
private def jsonADTSerializer[A <: ADT](adtName:String,adt:ADTCompanion[A]):(PartialFunction[JValue, A], PartialFunction[Any,JValue]) = (
{
case JObject
(JField
(`adtName`,
JObject(List(JField("id",JString(id)),JField("formattedName",formattedName)))) :: _) => getADT(adt,id.toLong)
},
{
case x: A => {
adtName -> (
("formattedName" -> x.formattedName) ~
("id" -> x.id)
)
}
}
)
sealed abstract class Test(val id: Long,val formattedName:String)
case object Test1 extends Test(1,"Test 1")
case object Test2 extends Test(2,"Test 2")
object Test {
val all:Set[Test] = SealedContents.values[Test]
}
sealed abstract class Foo(val id:Long, val formattedName:String)
case object Foo1 extends Foo(1,"Foo 1")
object Foo {
val all:Set[Foo] = SealedContents.values[Foo]
}
class TestSerializer extends CustomSerializer[Test](format => jsonADTSerializer("Test",Test))
class FooSerializer extends CustomSerializer[Foo](format => jsonADTSerializer("Foo",Foo))
如您所见,我试图通过使用 jsonADTSerializer 函数序列化我的 ADT 来减少样板文件,但是在编译上述代码时我收到以下警告消息
42: abstract type pattern A is unchecked since it is eliminated by erasure
[warn] case x: A => {
毫不奇怪,我java.lang.NoSuchMethodException
在使用上述代码时遇到了错误(因为 A 被擦除,case:x
所以我得到了jsonADTSerializer
不符合类型的A
类。)
你如何确保A
's 里面的类型jsonADTSerializer
不会被删除,以便函数按预期工作?
另外,如果您想知道,我正在使用您可以在此处找到的密封内容宏https://stackoverflow.com/a/13672520/1519631
可以在此处找到有关 scalatra 的 json 序列化的信息http://www.scalatra.org/guides/formats/json.html
更新:
更改类型jsonADTSerializer
以private def jsonADTSerializer[A <: ADT : ClassTag](adtName:String,adt:ADTCompanion[A]):(PartialFunction[JValue, A], PartialFunction[Any,JValue]) = (
删除有关擦除的警告消息并修复问题,谢谢!