Pickling a complex type as String #305
Description
Disclaimer: I'm not sure if what I am trying to do could in theory work, because I don't know enough about pickle theory.
I'm trying to pickle a Java class (org.bson.types.ObjectId
) so that its JSON representation yields a String. That works. However, unpickling does not quite work as well because, I think, the field is recognised as String and the JSONPickleReader
does not know what to do (I get a MatchError
). Hinting it with a different type tag than FastTypeTags.String
yields an empty result at pickle-time.
My belief is that the unpickling strategy would be influenced by the knowledge of a type encoded (in the case of JSON) in the $type
field of the pickled format, hence if at unpickle time we know that a field should be of a certain type then there should be a means by which to call for the adequate unpickler. Or is my belief nonsense?
For reference, the working pickling and broken unpickling:
def pickle(picklee: ObjectId, builder: PBuilder): Unit = {
builder.hintStaticallyElidedType()
// this probably needs to hint at ObjectId but then the stringPickler isn't thrilled and returns nothing
builder.hintTag(FastTypeTag.String)
stringPickler.pickle(picklee.toString, builder)
}
def unpickle(tag: String, reader: PReader): Any = {
val oid = stringPickler.unpickle(tag, reader).asInstanceOf[String]
new ObjectId(oid)
}