(依次,我需要对上述两个问题进行某种综合(1 ,<2/a/a>,但Imartm 并不够合)。
我有一组日本宇宙航空研究开发机构的代表,如:
abstract class Representation {
def marshalToXml(): String = {
val context = JAXBContext.newInstance(this.getClass())
val writer = new StringWriter
context.createMarshaller.marshal(this, writer)
writer.toString()
}
}
class Order extends Representation {
@BeanProperty
var name: String = _
...
}
class Invoice extends Representation { ... }
我的问题是我的“施工者”方法:
def unmarshalFromJson(marshalledData: String): {{My Representation Subclass}} = {
val mapper = new ObjectMapper()
mapper.getDeserializationConfig().withAnnotationIntrospector(new JaxbAnnotationIntrospector())
mapper.readValue(marshalledData, this.getClass())
}
def unmarshalFromXml(marshalledData: String): {{My Representation Subclass}} = {
val context = JAXBContext.newInstance(this.getClass())
val representation = context.createUnmarshaller().unmarshal(
new StringReader(marshalledData)
).asInstanceOf[{{Type of My Representation Subclass}}]
representation // Return the representation
}
具体来说,我可以指出,如何把这些不区分的方法以安全的方式和分国家的方式传给我的每一个班子,然后从Schala(希望有时只使用抽象的类型信息)。 换言之,我要这样做:
val newOrder = Order.unmarshalFromJson(someJson)
更有雄心的是:
class Resource[R <: Representation] {
getRepresentation(marshalledData: String): R =
{{R s Singleton}}.unmarshalFromXml(marshalledData)
}
就我的特殊 blocks脚石而言:
- I can t figure out whether I should define my
unmarshalFrom*()
constructors once in theRepresentation
class, or in a singletonRepresentation
object - if the latter, I don t see how I can automatically inherit that down through the class hierarchy ofOrder
,Invoice
etc. - I can t get
this.type
(as per this answer) to work as a way of self-typingunmarshalFromJson()
- I get a compile errortype mismatch; found: ?0 where type ?0 required: Representation.this.type
on thereadValue()
call - I can t figure out how to use the implicit
Default[A]
pattern (as per this answer) to work down my Representation class hierarchy to call the singleton unmarshalling constructors using type information only
I know this is a bit of a mammoth question touching on various different (but related) issues - any help gratefully received!
* A/63/150。