MongoDB Scala Driver - Rendering BSON Documents

别等时光非礼了梦想. 提交于 2019-12-05 21:28:05

you could use something like this to fix the issue

class SomeCodec extends Codec[Some[_]] {
  override def encode(writer: BsonWriter, value: Some[_], encoderContext: EncoderContext): Unit = value match {
    case Some(v: String) ⇒ writer.writeString(v)
    case Some(v: Int) ⇒ writer.writeInt32(v)
    case Some(v: Long) ⇒ writer.writeInt64(v)
    case Some(v: Boolean) ⇒ writer.writeBoolean(v)
  }

  override def getEncoderClass: Class[Some[_]] = classOf[Some[_]]

  override def decode(reader: BsonReader, decoderContext: DecoderContext): Some[_] = {
    reader.getCurrentBsonType match {
      case BsonType.BOOLEAN ⇒ Some(reader.readBoolean())
      case BsonType.STRING ⇒ Some(reader.readString())
      case BsonType.INT64 ⇒ Some(reader.readInt64())
      case BsonType.INT32 ⇒ Some(reader.readInt32())
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!