tjson

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package tjson provides converters from/to JSON with JSON Schema for built-in and `types` types.

See contributing guidelines and documentation for package `types` for details. See https://docs.tigrisdata.com/overview/schema#data-types for more details about JSON types.

Mapping

Composite types

*types.Document       {"$k": ["<key 1>", "<key 2>", ...], "<key 1>": <value 1>, "<key 2>": <value 2>, ...}
TODO *types.Array     JSON array

Scalar types

float64               JSON number (double format)
string                JSON string
types.Binary          {"$b": "<base 64 string>", "s": <subtype number>}
types.ObjectID	      JSON string (byte format, length is 12 bytes)
bool                  JSON true|false values
time.Time        	  JSON string (date-time RFC3339 format)
TODO types.NullType   JSON null
types.Regex           {"$r": "<string without terminating 0x0>", "o": "<string without terminating 0x0>"}
int32                 JSON number (int32 format)
types.Timestamp  	  {"$t": "<number as string>"}
int64                 JSON number (int64 format)
TODO Decimal128       {"$n": "<number as string>"}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal encodes given built-in or types' package value into tjson.

func Unmarshal

func Unmarshal(data []byte, schema *Schema) (any, error)

Unmarshal decodes the given tjson-encoded data.

Types

type Schema

type Schema struct {
	Title       string             `json:"title,omitempty"`
	Description string             `json:"description,omitempty"`
	Type        SchemaType         `json:"type,omitempty"`
	Format      SchemaFormat       `json:"format,omitempty"`
	Properties  map[string]*Schema `json:"properties,omitempty"`
	Items       *Schema            `json:"items,omitempty"`
	PrimaryKey  []string           `json:"primary_key,omitempty"`
}

Schema represents a supported subset of JSON Schema.

func DocumentSchema

func DocumentSchema(doc *types.Document) (*Schema, error)

DocumentSchema returns a JSON Schema for the given top-level document. Top-level documents are documents that must have _id which will be used as primary key.

func (*Schema) Equal

func (s *Schema) Equal(other *Schema) bool

Equal returns true if the schemas are equal. For composite types schemas are equal if their types and subschemas are equal. For scalar types schemas are equal if their types and formats are equal.

func (*Schema) Marshal

func (s *Schema) Marshal() ([]byte, error)

Marshal returns the JSON encoding of the schema.

func (*Schema) Unmarshal

func (s *Schema) Unmarshal(b []byte) error

Unmarshal parses the JSON-encoded schema.

type SchemaFormat

type SchemaFormat string

SchemaFormat represents additional information about JSON value type in JSON Schema.

const (
	EmptyFormat SchemaFormat = ""

	// For Number.
	Double SchemaFormat = "double"
	Float  SchemaFormat = "float"

	// For Integer.
	Int64 SchemaFormat = "int64"
	Int32 SchemaFormat = "int32"

	// For String.
	Byte     SchemaFormat = "byte"
	UUID     SchemaFormat = "uuid"
	DateTime SchemaFormat = "date-time"
)

JSON value formats.

type SchemaType

type SchemaType string

SchemaType represents JSON value type in JSON Schema.

const (
	Integer SchemaType = "integer"
	Number  SchemaType = "number"
	String  SchemaType = "string"
	Boolean SchemaType = "boolean"
	Array   SchemaType = "array"
	Object  SchemaType = "object"
)

JSON value types defined by the JSON Schema.

Jump to

Keyboard shortcuts

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