module vbson
February 26, 2023 ยท View on GitHub
Contents
- encode
- map_to_bson
- bson_to_json
- bson_to_map
- json_to_bson
- map[string]Any
- Any
- []Any
- Decimal128
- Binary
- MaxKey
- MinKey
- Null
- ObjectID
- Regex
- JSCode
encode
fn encode[T](data T) !string
encode takes only struct as input and returns BSON or
returns error for failed encoding.
Use attribute bsonskip to skip encoding of any field from a struct.
Use attribute bson_oid to specify a string field as mongo-style object id.
TODO: Use attribute bson:custom_name to replace field name with a custom name.
NOTE: It uses x.json2 raw encoding/decoding. So fields having json related attributes result in unexpected behaviour.
map_to_bson
fn map_to_bson(m map[string]Any) string
bson_to_json
fn bson_to_json(b_data string) !string
bson_to_map
fn bson_to_map(data string) !map[string]Any
bson_to_map takes in bson string input and returns
map[string]Any as output.
It returns error if encoded data is incorrect.
json_to_bson
fn json_to_bson(j_data string) !string
map[string]Any
to_json
fn (doc map[string]Any) to_json() !string
TODO may not be correct
Any
type Any = Binary
| Decimal128
| JSCode
| MaxKey
| MinKey
| Null
| ObjectID
| Regex
| []Any
| bool
| f64
| i64
| int
| map[string]Any
| string
| time.Time
| u64
Any consists of only the types supported by bson
[]Any
to_json
fn (arr []Any) to_json() !string
Decimal128
struct Decimal128 {
bytes []u8
}
Binary
struct Binary {
mut:
b_type u8
data []u8
}
Binary is a wrapper for binary data as per specs in bsonspec.org.
MaxKey
struct MaxKey {
}
MinKey
struct MinKey {
}
Null
struct Null {
is_null bool = true
}
Null is placeholder for null/nil values.
ObjectID
struct ObjectID {
id string
}
ObjectID is a wrapper for mongo-style objectID.
NOTE: Object id should be only 12 bytes long.
Regex
struct Regex {
mut:
pattern string
options string
}
JSCode
struct JSCode {
code string
}