简体   繁体   English

使用 scala mongo 驱动程序序列化为对象?

[英]Serialize to object using scala mongo driver?

I'm new to the scala mongo driver and am trying to understand how to map a class from a Document?我是 scala mongo 驱动程序的新手,想了解如何从文档映射类? None of the documentation seems to show how this is done.似乎没有任何文档显示这是如何完成的。 In the .net driver, its as easy as passing a generic and having fields auto mapped.在 .net 驱动程序中,它就像传递泛型和自动映射字段一样简单。 Is there nothing similar in scala? Scala 中没有类似的东西吗?

They don't make it easy.他们并不容易。 Digging through the java, I came up with this solution:通过java挖掘,我想出了这个解决方案:

import org.bson.codecs.DecoderContext
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
import org.bson.codecs.configuration.CodecRegistry
import org.bson.{BsonDocumentReader, BsonDocumentWrapper}
import org.mongodb.scala.bson.codecs.{DEFAULT_CODEC_REGISTRY, Macros}
import org.mongodb.scala.bson.collection.mutable.Document

import scala.reflect.classTag

case class Person(firstName: String, lastName: String)

object MongoTest extends App {

  val personCodecProvider = Macros.createCodecProvider[Person]()
  val codecRegistry: CodecRegistry = fromRegistries(fromProviders(personCodecProvider), DEFAULT_CODEC_REGISTRY)

  val document = Document("firstName" -> "first", "lastName" -> "last")
  val bsonDocument = BsonDocumentWrapper.asBsonDocument(document, DEFAULT_CODEC_REGISTRY)

  val bsonReader = new BsonDocumentReader(bsonDocument)
  val decoderContext = DecoderContext.builder.build
  val codec = codecRegistry.get(classTag[Person].runtimeClass)
  val person: Person = codec.decode(bsonReader, decoderContext).asInstanceOf[Person]

  println(s"person: $person")
}

Example for serializing and deserializing object using mongo macro handler.使用 mongo 宏处理程序序列化和反序列化对象的示例。

import reactivemongo.api.bson.{BSON, BSONDocument, Macros}

case class Person(name:String = "SomeName", age:Int = 20)

implicit val personHandler = Macros.handler[Person]

//Serialize
val bsonPerson = BSON.writeDocument[Person](Person())

println(s"${BSONDocument.pretty(bsonPerson.getOrElse(BSONDocument.empty))}")

//Deserialize

val bsonDocumentPerson = BSONDocument("name"-> "MyNameHere", "age"->35)

val scalaObjPerson: Person = BSON.read[Person](bsonDocumentPerson).getOrElse(Person())

printf(s"Scala person obj = $scalaObjPerson")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Scala的ReactiveMongo驱动程序将对象插入Mongo数据库 - Inserting an object into Mongo database using Scala's ReactiveMongo driver 使用mongo C#驱动程序,如何序列化自定义对象数组以便存储它? - Using the mongo C# driver, how to serialize an array of custom object in order to store it? Scala Mongo驱动程序使用Future获得结果 - Scala Mongo driver getting results using Future 使用反应式 Mongo 驱动程序时 Scala 控制台应用程序不会退出 - Scala Console App Does Not Exit When Using Reactive Mongo Driver Mongo DB使用Mongo C#驱动程序更新嵌套对象 - Mongo DB update nested object using Mongo C# Driver 使用C#MongoDB驱动程序,如何序列化对象引用的集合? - Using C# MongoDB Driver, how to serialize a collection of object referements? 使用 mongo-scala-driver 在 Scala 中打印来自 Mongodb 的查询结果 - Printing query results from Mongodb in Scala using mongo-scala-driver 使用 Rust Mongo 驱动程序原型时,如何将 chrono::DateTime 字段序列化为 ISODate? - How do I serialize chrono::DateTime fields as ISODate when using the Rust Mongo driver prototype? 如何使用Mongo C#驱动程序的BSONClassMap将所有属性序列化和反序列化为String - How do I Serialize and DeSerialize all properties to String using Mongo C# Driver's BSONClassMap 使用Java mongo驱动程序从集合中删除对象 - Removing object from collection, using the Java mongo driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM