Documentation ¶
Overview ¶
Fast and space efficiency JSON serialization golang library. It is a schema oriented design which leverages schema definition to encode JSON document into compact binary encoded format, and decodes back into JSON document.
Introduction ¶
When we want to exchange data between services or over network, the JSON is most popular format to do it. In golang world, the most convenient way is using official `encoding/json` package to marshal and unmarshal JSON document from/to struct or map. For usual RESTful web service scenario, JSON format is quite convenience and representative, but for real-time message exchanging or other scenarios that has small footprint data and low-letency requirement, JSON is a bit too heavyweight, not only data footprint is not space saving, but also has heavy loading in encode/decode procedure.
So if we want to a compact, small footprint data for exchanging over network, and also leverages the convenience of JSON, we need an encoding format that removes "property name" and other notations likes ':', '[', '{'...etc from original JSON document, and leaves "value" only.
To achieve this goal, we need a schematic to define our JSON document and provide enough information for serialization engine to know sequence and data type of every properties in document. it's the reason why jsonpack is a schema oriented design library.
Key Features ¶
* Similar Marshal / Unmarshal API to standard `encoding/json` package.
* Space saving encoded format, the size of encoded data is similar to Protocol Buffers, can be 30-80% of original JSON document, depends on data.
* Blazing fast, provides about 3.x decoding speed compared to `protobuf` and many times than other JSON packages.
* Memory saving design, avoids any un-neccessary memory allocations, suitable for embedded environment.
* Has production ready javascript implementation https://github.com/arloliu/buffer-plus, can be used in node.js and Web browser environment.
* No need to write schema definition by hand, `jsonpack` will generate schema definition from golang struct automatically.
Index ¶
- type ByteOrder
- type CompileError
- type DecodeError
- type EncodeError
- type InvalidPropValueError
- type JSONPack
- func (p *JSONPack) AddSchema(schemaName string, v ...interface{}) (*Schema, error)
- func (p *JSONPack) Decode(schemaName string, data []byte, v interface{}) error
- func (p *JSONPack) Encode(schemaName string, v interface{}) ([]byte, error)
- func (p *JSONPack) EncodeTo(schemaName string, v interface{}, dataPtr *[]byte) error
- func (p *JSONPack) GetAllSchemaDefTexts() map[string][]byte
- func (p *JSONPack) GetAllSchemaDefs() map[string]*SchemaDef
- func (p *JSONPack) GetAllSchemas() map[string]*Schema
- func (p *JSONPack) GetSchema(schemaName string) *Schema
- func (p *JSONPack) GetSchemaDef(schemaName string) (*SchemaDef, error)
- func (p *JSONPack) GetSchemaDefText(schemaName string) ([]byte, error)
- func (p *JSONPack) Marshal(schemaName string, v interface{}) ([]byte, error)
- func (p *JSONPack) RemoveSchema(schemaName string) error
- func (p *JSONPack) Reset()
- func (p *JSONPack) Unmarshal(schemaName string, data []byte, v interface{}) error
- type NotImplementedError
- type Schema
- func (s *Schema) Decode(data []byte, v interface{}) (err error)
- func (s *Schema) Encode(d interface{}) ([]byte, error)
- func (s *Schema) EncodeTo(d interface{}, dataPtr *[]byte) (err error)
- func (s *Schema) GetSchemaDef() (*SchemaDef, error)
- func (s *Schema) GetSchemaDefText() []byte
- func (s *Schema) Marshal(d interface{}) ([]byte, error)
- func (s *Schema) SetDecodeBufSize(size int64)
- func (s *Schema) SetEncodeBufSize(size int64)
- func (s *Schema) Unmarshal(data []byte, v interface{}) (err error)
- type SchemaDef
- type SchemaNonExistError
- type StructFieldNonExistError
- type TypeAssertionError
- type UnknownTypeError
- type WrongTypeError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompileError ¶
type CompileError struct { Err error // contains filtered or unexported fields }
CompileError indicates an error that occurred while attempting to compile schema definition
func (*CompileError) Error ¶
func (e *CompileError) Error() string
func (*CompileError) Unwrap ¶
func (e *CompileError) Unwrap() error
type DecodeError ¶
type DecodeError struct { Err error // contains filtered or unexported fields }
DecodeError indicates an error that occurred while attempting to decode packed binary data with pre-compiled schema definition
func (*DecodeError) Error ¶
func (e *DecodeError) Error() string
func (*DecodeError) Unwrap ¶
func (e *DecodeError) Unwrap() error
type EncodeError ¶
type EncodeError struct { Err error // contains filtered or unexported fields }
EncodeError indicates an error that occurred while attempting to encode data with pre-compiled schema definition
func (*EncodeError) Error ¶
func (e *EncodeError) Error() string
func (*EncodeError) Unwrap ¶
func (e *EncodeError) Unwrap() error
type InvalidPropValueError ¶
type InvalidPropValueError struct {
// contains filtered or unexported fields
}
InvalidPropValueError indicates an error the that occurred when reading or writing property got invalid value
func (*InvalidPropValueError) Error ¶
func (e *InvalidPropValueError) Error() string
type JSONPack ¶
type JSONPack struct {
// contains filtered or unexported fields
}
JSONPack JSON packer structure
func (*JSONPack) AddSchema ¶
AddSchema will compile schema definition and stores compiled result in internal schema manager.
It's a variadic function which accept two types of input parameters in the following.
AddSchema(schemaName string, v interface{})
The v is schema definition which want to compile. The value of v can be a JSON format of text data with []byte/string type, a map represents JSON format of schema definition, or a SchemaDef struct represents schema definition.
Example of add new schema from JSON text string:
schDef := ` { "type": "object", "properties": { "name": {"type": "string"}, "area": {"type": "uint32le"} }, "order": ["name", "area"] } ` jsonPack := jsonpack.NewJSONPack() sch, err := jsonPack.AddSchema("info", schDef)
Example of adding new schema from map of schema definition:
schDef := map[string]interface{}{ "type": "object", "properties": map[string]interface{}{ "name": map[string]interface{}{"type": "string"}, "area": map[string]interface{}{"type": "uint32le"}, }, "order": []string{"name", "area"}, } jsonPack := jsonpack.NewJSONPack() sch, err := jsonPack.AddSchema("info", schDef)
Example of adding new schema from SchemaDef struct:
schDef := jsonpack.SchemaDef{ Type: "object", Properties: map[string]*jsonpack.SchemaDef{ "name": {Type: "string"}, "area": {Type: "uint32le"}, }, Order: []string{"name", "area"}, } jsonPack := jsonpack.NewJSONPack() sch, err := jsonPack.AddSchema("info", schDef)
AddSchema(schemaName string, v interface{}, byteOrder jsonpack.ByteOrder)
For fast prototyping, AddSchema method supports generate schema definition from existing struct without writing schema definition by hand.
In this scenario, the value of v is the source struct which to generated, and byteOrder parameter indicates the byte order, can be either jsonpack.LittleEndian or jsonpack.BigEndian, it defaults to little-endian if not specified.
This method supports struct tag, use the same format as standard encoding/json excepts "omitempty" option, the "omitempty" option will be ignored.
Example of adding new schema and build schema definition from struct:
type Info struct { Name string `json:"name"` // "omitempty" option will be ignore, so this field will be not be omitted Area uint32 `json:"area,omitempty"` ExcludeField string `-` // this field is ignored } jsonPack := jsonpack.NewJSONPack() sch, err := jsonPack.AddSchema("Info", Info{}, jsonpack.BigEndian)
func (*JSONPack) Decode ¶
Decode is a wrapper of Schema.Decode, it returns *SchemaNonExistError error if schema not found.
func (*JSONPack) Encode ¶
Encode is a wrapper of Schema.Encode, it returns *SchemaNonExistError error if schema not found.
func (*JSONPack) EncodeTo ¶
EncodeTo is a wrapper of Schema.EncodeTo, it returns *SchemaNonExistError error if schema not found.
func (*JSONPack) GetAllSchemaDefTexts ¶
GetAllSchemaDefTexts returns a map which contains all existed schema text definitions, key of map it schema name, and value of map is text format of schema defintion which presented as []byte.
func (*JSONPack) GetAllSchemaDefs ¶
GetAllSchemaDefs returns a map which contains all existed schema definitions, key of map it schema name, and value of map is *SchemaDef.
func (*JSONPack) GetAllSchemas ¶
GetAllSchemas returns a map which contains all existed schema instances, key of map it schema name, and value of map is *Schema.
func (*JSONPack) GetSchema ¶
GetSchema returns schema instance by schemaName, returns nil if schema not found.
func (*JSONPack) GetSchemaDef ¶
GetSchemaDef is a wrapper of Schema.GetSchemaDef, it gets a Schema instance by schemaName, it returns *SchemaNonExistError error if schema not found.
func (*JSONPack) GetSchemaDefText ¶
GetSchemaDefText is a wrapper of Schema.GetSchemaDefText, it returns *SchemaNonExistError error if schema not found.
func (*JSONPack) Marshal ¶
Marshal is an alias to Encode function, provides familiar interface of json package
func (*JSONPack) RemoveSchema ¶
RemoveSchema removes schema by schemaName, it returns *SchemaNonExistError error if schema not found.
type NotImplementedError ¶
type NotImplementedError struct {
// contains filtered or unexported fields
}
NotImplemented means this method/operation is not implemented
func (*NotImplementedError) Error ¶
func (e *NotImplementedError) Error() string
type Schema ¶
type Schema struct { // schema name Name string // contains filtered or unexported fields }
Schema of jsonpack, each Schema instance represents a schema added by AddSchema function
func (*Schema) Decode ¶
Decode reads encoded data with compiled schema definition and stores the result in the value pointed to v.
If type of v is not a pointer type that pointed to a map or struct, Decode function will return DecodeError.
The valid type of v is either a *map[string]interface{} or a pointer to the struct which added by AddSchema function.
func (*Schema) Encode ¶
Encode returns packed binary data of v with compiled schema definition. The compiled schema definition is specified by schemaName and return data will be nil if error occurs.
The type of v can be a map[string]interface{} which represents valid JSON data, or a struct instance which added by AddSchema function.
The return data contains encoded binary data that can then be decoded by Decode function.
Example of encoding a map
data := map[string]interface{} { "name": "example name", "area": uint32(888), } jsonPack := jsonpack.NewJSONPack() // call jsonPack.AddSchema to register schema sch := jsonPack.GetSchema("info") result, err := sch.Encode(data)
Example of encoding struct
type Info struct
func (*Schema) EncodeTo ¶
EncodeTo is similar to Encode function, but passing a pointer to []byte to store encoded data instead of returning new allocated []byte encoded data.
func (*Schema) GetSchemaDef ¶
GetSchemaDef returns schema definition instance, returns nil and error if error occurs.
func (*Schema) GetSchemaDefText ¶
GetSchemaDefText returns JSON format document of schema definition, which presetned as []byte.
func (*Schema) Marshal ¶
Marshal is an alias to Encode function, provides familiar interface of json package
func (*Schema) SetDecodeBufSize ¶
SetDecodeBufSize set default decode buffer allocation bytes
func (*Schema) SetEncodeBufSize ¶
SetEncodeBufSize set default encode buffer allocation bytes
type SchemaDef ¶
type SchemaDef struct { Type string `json:"type"` Properties map[string]*SchemaDef `json:"properties,omitempty"` Items *SchemaDef `json:"items,omitempty"` Order []string `json:"order,omitempty"` }
The schema defintion represents the definition that define the structure of JSON data.
type SchemaNonExistError ¶
type SchemaNonExistError struct {
// contains filtered or unexported fields
}
SchemaNonExistError indicates an error that occurred while pre-compiled schema defintion does not exist
func (*SchemaNonExistError) Error ¶
func (e *SchemaNonExistError) Error() string
type StructFieldNonExistError ¶
type StructFieldNonExistError struct {
// contains filtered or unexported fields
}
StructFieldNonExistError indicates an error the that occurred when structure doesn't contain required field
func (*StructFieldNonExistError) Error ¶
func (e *StructFieldNonExistError) Error() string
type TypeAssertionError ¶
type TypeAssertionError struct {
// contains filtered or unexported fields
}
TypeAssertionError indicates an error the that occurred when doing data data type asserrion
func (*TypeAssertionError) Error ¶
func (e *TypeAssertionError) Error() string
type UnknownTypeError ¶
type UnknownTypeError struct {
// contains filtered or unexported fields
}
UnknownTypeError means there has a un-supported data type when compiling schema definition
func (*UnknownTypeError) Error ¶
func (e *UnknownTypeError) Error() string
type WrongTypeError ¶
type WrongTypeError struct {
// contains filtered or unexported fields
}
WrongTypeError means there has a wrong data type when encoding or decoding data with pre-compiled schema definition
func (*WrongTypeError) Error ¶
func (e *WrongTypeError) Error() string