tjson

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: Apache-2.0 Imports: 9 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.

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