sjson

package
v1.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package sjson provides converters from/to jsonb with some extensions for built-in and `types` types.

See contributing guidelines and documentation for package `types` for details.

Mapping

SJSON uses schema to map values to data types. Schema is stored in the `$s` field of the document and contains information about the fields. A document with schema looks like this:

{
   "$s": {
     "$k": ["field1", "field2", ...],
     "p": {
       "field1": {<schema>},
       "field2": {<schema>},
       ...
   }
   "field1": <json representation>,
   "field2": <json representation>,
   ...
}

Composite types

Alias      types package    sjson package        sjson schema                                             JSON representation

object     *types.Document  *sjson.documentType  {"t":"object", "$s": {"$k":[<keys>], "p":{<properties>}} JSON object
array      *types.Array     *sjson.arrayType     {"t":"array", "i": [<item 1>, <item 2>]}                 JSON array

Scalar types

Alias      types package    sjson package         sjson schema                           JSON representation

double     float64          *sjson.doubleType     {"t":"double"}                         JSON number
string     string           *sjson.stringType     {"t":"string"}                         JSON string
binData    types.Binary     *sjson.binaryType     {"t":"binData",
                                                   "s":<subtype number>}                 "<base 64 string>"
objectId   types.ObjectID   *sjson.objectIDType   {"t":"objectId"}                       "<ObjectID as 24 character hex string>"
bool       bool             *sjson.boolType       {"t":"bool"}                           JSON true / false values
date       time.Time        *sjson.dateTimeType   {"t":"date"}                           milliseconds since epoch as JSON number
null       types.NullType   *sjson.nullType       {"t":"null"}                           JSON null
regex      types.Regex      *sjson.regexType      {"t":"regex",
                                                   "o": "<string w/o terminating 0x0>"}  "<string w/o terminating 0x0>"
int        int32            *sjson.int32Type      {"t":"int"}                            JSON number
timestamp  types.Timestamp  *sjson.timestampType  {"t":"timestamp"}                      JSON number
long       int64            *sjson.int64Type      {"t":"long"}                           JSON number

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTypeOfValue

func GetTypeOfValue(v any) string

GetTypeOfValue returns sjson type of supported value. It is used as internal type representation, and shouldn't be used to represent the BSON type.

func Marshal

func Marshal(d *types.Document) ([]byte, error)

Marshal encodes given document fields and set its schema in the field $s. Use it when you need to encode a document with schema, for example, when you want to store it in a database.

func MarshalSingleValue

func MarshalSingleValue(v any) ([]byte, error)

MarshalSingleValue encodes given built-in or types' package value into sjson. Use it when you need to encode a single value, for example in a where clause.

func Unmarshal

func Unmarshal(data []byte) (*types.Document, error)

Unmarshal decodes the top-level document. It decodes document's schema from the `$s` field and uses it to decode the data of the document.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL