项目作者: agcom

项目描述 :
BSON format implementation for Kotlinx serialization
高级语言: Kotlin
项目地址: git://github.com/agcom/bson.git
创建时间: 2020-08-11T09:21:01Z
项目社区:https://github.com/agcom/bson

开源协议:Apache License 2.0

下载


:warning: Archived in favor of a future project (agcom/kbson) :warning:

Bson serialization

Bson serialization format implementation for Kotlinx serialization, based on The BSON library (org.mongodb.bson).

JVM only.

Also, provides useful tools to integrate with MongoDB Java driver. For example, the out of box SerializationCodecRegistry.

Setup

Currently, only supports Kotlinx serialization runtime 0.20.0.

Gradle

  • *.gradle:

    1. repositories {
    2. jcenter() // Make sure jcenter is added to your project repositories
    3. }
    4. dependencies {
    5. implementation 'com.github.agcom.bson:bson-serialization:0.5.0' // The bson serialization library
    6. implementation 'com.github.agcom.bson:bson-mongodb:0.5.0' // MongoDB driver extensions
    7. }
  • *.gradle.kts: Same as *.gradle, with some small tweaks.

Usage

Here is a small example,

  1. import kotlinx.serialization.*
  2. import com.github.agcom.bson.serialization.*
  3. import org.bson.*
  4. @Serializable
  5. data class Project(val name: String, val language: String)
  6. val bson = Bson()
  7. fun main() {
  8. val data = Project("com.github.agcom.bson", "Kotlin")
  9. // Serializing
  10. val bsonValue = bson.toBson(Project.serializer(), data) // A `BsonValue` child, in this case a `BsonDocument`
  11. println(bsonValue) // {"name": "com.github.agcom.bson", "language": "Kotlin"}
  12. // Deserializing
  13. println(
  14. bson.fromBson(Project.serializer(), bsonValue)
  15. ) // Project(name=com.github.agcom.bson, language=Kotlin)
  16. }

Serialization functions

The following functions can be found in the com.github.agcom.bson.serialization.Bson class.

  • toBson and fromBson: The above example :point_up:.

  • dump and load:

    1. import kotlinx.serialization.*
    2. import com.github.agcom.bson.serialization.*
    3. import org.bson.*
    4. @Serializable
    5. data class Project(val name: String, val language: String)
    6. val bson = Bson()
    7. fun main() {
    8. val data = Project("com.github.agcom.bson", "Kotlin")
    9. // Dump
    10. val bytes = bson.dump(Project.serializer(), data)
    11. // Load
    12. println(
    13. bson.load(Project.serializer(), bytes)
    14. ) // Project(name=com.github.agcom.bson, language=Kotlin)
    15. }

    Doesn’t support loading/dumping primitive types.

Serializers

Various bson types adapter serializers can be found under com.github.agcom.bson.serialization.serializers package.

Those are all registered as default contextual serializers, so you can use @ContextualSerializer safely.

For example, BsonValueSerializer, TemporalSerializer and RegexSerializer.

MongoDB driver extensions

Provides extensions to integrate with MongoDB Java driver.

  • Serialization codec

    An adapter between KSerializer and Codec.

    1. import kotlinx.serialization.*
    2. import com.github.agcom.bson.serialization.*
    3. import org.bson.codecs.Codec
    4. import com.github.agcom.bson.mongodb.codecs.*
    5. @Serializable
    6. data class Project(val name: String, val language: String)
    7. val bson = Bson()
    8. fun main() {
    9. val codec: Codec<Project> serializer= SerializationCodec(bson, Project.serializer()) // Look here
    10. ...
    11. }
  • Serialization codec registry

    An adapter between Bson and CodecRegistry.

    1. import kotlinx.serialization.*
    2. import com.github.agcom.bson.serialization.*
    3. import com.github.agcom.bson.mongodb.codecs.*
    4. import org.bson.codecs.configuration.*
    5. import com.mongodb.MongoClientSettings
    6. @Serializable
    7. data class Project(val name: String, val language: String)
    8. val bson = Bson()
    9. fun main() {
    10. // Composing two registries
    11. val registry: CodecRegistry = CodecRegistries.fromRegistries(
    12. MongoClientSettings.getDefaultCodecRegistry(), // The driver's default codec registry
    13. SerializationCodecRegistry(bson) // Serialization registry
    14. )
    15. ...
    16. }

    It’s recommended to compose the serialization registry after the default registry. This reduces hip-hops (better performance) when working with simple bson types.