ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - jershell/kbson: Mongo BSON support for kotlinx.serialization.
Mongo BSON support for kotlinx.serialization. Contribute to jershell/kbson development by creating an account on GitHub.
Visit Site

GitHub - jershell/kbson: Mongo BSON support for kotlinx.serialization.

GitHub - jershell/kbson: Mongo BSON support for kotlinx.serialization.

Maven Central Bintray

kbson

What is this?

This adapter adds BSON support to kotlinx.serialization.

BSON types supported:

  • Double
  • String
  • Array
  • Binary
  • UUID
  • ObjectId
  • Boolean
  • Date
  • Symbol
  • 32-bit integer
  • Long
  • Decimal128

Install

build.gradle.kts

dependencies {
    // kotlin 1.9.24 & kotlinx.serialization 1.6.3
    implementation("com.github.jershell:kbson:0.7.0")
    
    // kotlin 1.7.20 & kotlinx.serialization 1.4.1
    implementation("com.github.jershell:kbson:0.6.0")
    
    // kotlin 1.6.21 & kotlinx.serialization 1.3.3
    implementation("com.github.jershell:kbson:0.4.6")


    // kotlin 1.4.20 & kotlinx.serialization 1.0.1
    implementation("com.github.jershell:kbson:0.4.4")
    
    // kotlin 1.3
    implementation("com.github.jershell:kbson:0.3.0")
}

or

build.gradle

dependencies {
    // kotlin 1.9.24 & kotlinx.serialization 1.6.3
    implementation 'com.github.jershell:kbson:0.7.0'

    // kotlin 1.7.20 & kotlinx.serialization 1.4.1
    implementation 'com.github.jershell:kbson:0.5.0'
    
    // kotlin 1.7.20 & kotlinx.serialization 1.4.1
    implementation 'com.github.jershell:kbson:0.5.0'
    
    // kotlin 1.6.21 & kotlinx.serialization 1.3.3
    implementation 'com.github.jershell:kbson:0.4.6'
    
    
    // kotlin 1.4.20 & kotlinx.serialization 1.0.1
    implementation 'com.github.jershell:kbson:0.4.4'
    
    
    // kotlin 1.3
    implementation 'com.github.jershell:kbson:0.3.0'
}

Usage samples

Parsing from BSON to a Kotlin object
import kotlinx.serialization.Serializable
import com.github.jershell.kbson.ObjectIdSerializer

val kBson = KBson()


@Serializable
data class Simple (
        val valueString: String,

        val valueDouble: Double,

        val valueFloat: Float,

        val valueLong: Long,

        val valueChar: Char,

        val valueBool: Boolean,

        @NonEncodeNull
        @ContextualSerialization
        val _id: ObjectId? = null,

        @ContextualSerialization
        val uuid: UUID,

        val valueInt: Int
)

val simple = kBson.parse(Simple.serializer(), bsonDocFromMongoJavaDriver)
Serializing from a Kotlin object to BSON
import kotlinx.serialization.Serializable
import com.github.jershell.kbson.Configuration
import com.github.jershell.kbson.KBson
import com.github.jershell.kbson.ObjectIdSerializer

@Serializable
data class Simple (
        val valueString: String = "default",
        val valueDouble: Double,
        val valueFloat: Float,
        val valueLong: Long,
        val valueChar: Char,
        val valueBool: Boolean,
        @NonEncodeNull
        @ContextualSerialization 
        val _id: ObjectId? = null,
        val valueInt: Int
)

// Optional configuration
val kBson = KBson(Configuration(encodeDefaults = false))
val bsonDoc = kBson.stringify(Simple.serializer(), simpleModel)

// You can override default serializers or add your serializer  
val kBson = KBson(context = serializersModuleOf(mapOf(
            ObjectId::class to ObjectIdSerializer,
            Date::class to DateSerializer
    )))
    
// You can use load and dump with ByteArray 
val simple = kBson.load(Simple.serializer(), bsonDoc)
// also
val simple2 = kBson.load(Simple.serializer(), bsonDoc.toByteArray())
// !!!! The method load() use BsonDecoder and strict order of fields

See the tests for more examples

API

val kBson = KBson()

kBson.parse(deserializer: DeserializationStrategy<T>, doc: BsonDocument) :T
// !!!! The method load() use BsonDecoder and strict order of fields
// https://docs.mongodb.com/manual/tutorial/update-documents/#field-order
kBson.load(deserializer: DeserializationStrategy<T>, doc: ByteArray): T
kBson.load(deserializer: DeserializationStrategy<T>, doc: BsonDocument): T

kBson.stringify(): BsonDocument
kBson.dump(serializer: SerializationStrategy<T>, obj: T): ByteArray

// Extension functions 
BsonDocument.toByteArray(): ByteArray
BsonDocument.toDocument(): Document

Configuration

classDiscriminator = "___type" // name of the class descriptor property in polymorphic serialization.
encodeDefaults = "true" // specifies whether default values are encoded.
ps

@NonEncodeNull useful for item _id mongodb

The default enum class supported like string. You can also override it

kbson before 0.1.5 use kotlinx.serialization 0.11.x

kbson after 0.1.5 use kotlinx.serialization 0.13.x

kbson after 0.2.0 use kotlinx.serialization 0.14.x

kbson after 0.2.2 use kotlinx.serialization 0.20.0 and kotlin 1.3.71

kbson after 0.4.0 use kotlinx.serialization "1.0-M1-1.4.0-rc" and kotlin 1.4.0-rc

kbson after 0.4.1 use kotlinx.serialization "1.0.0" and kotlin 1.4.10

kbson after 0.4.3 use kotlinx.serialization "1.0.0" and kotlin 1.4.20

kbson after 0.4.5 use kotlinx.serialization "1.3.3" and kotlin 1.6.21

kbson after 0.4.6 use kotlinx.serialization "1.3.3" and kotlin 1.6.21

kbson after 0.5.0 use kotlinx.serialization "1.4.1" and kotlin 1.7.20

Contributing to kbson

Pull requests and bug reports are always welcome!

To run the tests: ./gradlew check

Reference links

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory