EJson

sealed class EJson : StringFormat

Main entry point to work with EJSON serialization. EJSON is a JSON format that can be used to encode all BSON datatypes.

A default instance is provided via EJson.Default, but if you require an instance with certain registered serializers or different options you can instantiate it with EJson.

This string format also supports encoding to or from a BsonValue with the functions decodeFromBsonValue and encodeToBsonValue.

Example of usage:

@Serializable
class DataHolder(val id: Int, val data: String, val bsonValue: BsonValue)

val ejson = EJson
val instance = DataHolder(42, "some data", BsonObjectId() }

// Plain StringFormat usage
val stringOutput: String = ejson.encodeToString(instance)

// BsonValue serialization
val bsonValue: BsonValue = ejson.encodeToBsonValue(instance)

// Deserialize from string
val deserialized: DataHolder = ejson.decodeFromString<DataHolder>(stringOutput)

// Deserialize from BsonValue
val deserializedFromBsonValue: DataHolder = ejson.decodeFromBsonValue<DataHolder>(bsonValue)

// Deserialize from string to BsonValue
val deserializedToBsonValue: BsonValue = Bson(stringOutput)

It does not support polymorphic serializers yet.

Types

Link copied to clipboard
object Default : EJson

The default instance of EJson with default configuration.

Functions

Link copied to clipboard
fun <T> decodeFromBsonValue(deserializer: DeserializationStrategy<T>, value: BsonValue): T

Deserializes the given value into a value of type T using the given deserializer.

Link copied to clipboard
open override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T

Deserializes the given EJSON string into a value of type T using the given deserializer.

Link copied to clipboard
fun <T> encodeToBsonValue(serializer: SerializationStrategy<T>, value: T): BsonValue

Serializes the given value into an equivalent BsonValue using the given serializer

Link copied to clipboard
open override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String

Serializes the value into an equivalent EJSON using the given serializer.

Properties

Link copied to clipboard
val ignoreUnknownKeys: Boolean
Link copied to clipboard
open override val serializersModule: SerializersModule

Inheritors

Link copied to clipboard

Extensions

Link copied to clipboard
inline fun <T : Any> EJson.decodeFromBsonValue(value: BsonValue): T

Deserializes the given value element into a value of type T using a deserializer retrieved from reified type parameter.

Link copied to clipboard
inline fun <T : Any> EJson.encodeToBsonValue(value: T): BsonValue

Serializes the given value into an equivalent BsonValue using a serializer retrieved from reified type parameter.