Documentation
¶
Overview ¶
Package gjson provides convenient API for JSON/XML/INI/YAML/TOML data handling.
Index ¶
- func Decode(data interface{}, options ...Options) (interface{}, error)
- func DecodeTo(data interface{}, v interface{}, options ...Options) (err error)
- func Encode(value interface{}) ([]byte, error)
- func EncodeString(value interface{}) (string, error)
- func IsValidDataType(dataType ContentType) bool
- func Marshal(v interface{}) (marshaledBytes []byte, err error)
- func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error)
- func MustEncode(value interface{}) []byte
- func MustEncodeString(value interface{}) string
- func Unmarshal(data []byte, v interface{}) (err error)
- func Valid(data interface{}) bool
- type ContentType
- type Json
- func DecodeToJson(data interface{}, options ...Options) (*Json, error)
- func Load(path string, safe ...bool) (*Json, error)
- func LoadContent(data interface{}, safe ...bool) (*Json, error)
- func LoadContentType(dataType ContentType, data interface{}, safe ...bool) (*Json, error)
- func LoadIni(data interface{}, safe ...bool) (*Json, error)
- func LoadJson(data interface{}, safe ...bool) (*Json, error)
- func LoadProperties(data interface{}, safe ...bool) (*Json, error)
- func LoadToml(data interface{}, safe ...bool) (*Json, error)
- func LoadWithOptions(data interface{}, options Options) (*Json, error)
- func LoadXml(data interface{}, safe ...bool) (*Json, error)
- func LoadYaml(data interface{}, safe ...bool) (*Json, error)
- func New(data interface{}, safe ...bool) *Json
- func NewWithOptions(data interface{}, options Options) *Json
- func NewWithTag(data interface{}, tags string, safe ...bool) *Json
- func (j *Json) Append(pattern string, value interface{}) error
- func (j *Json) Array() []interface{}
- func (j *Json) Contains(pattern string) bool
- func (j *Json) Dump()
- func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var
- func (j *Json) GetJson(pattern string, def ...interface{}) *Json
- func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json
- func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json
- func (j *Json) Interface() interface{}
- func (j *Json) Interfaces() []interface{}
- func (j *Json) IsNil() bool
- func (j *Json) Len(pattern string) int
- func (j *Json) Map() map[string]interface{}
- func (j *Json) MapStrAny() map[string]interface{}
- func (j Json) MarshalJSON() ([]byte, error)
- func (j *Json) MustAppend(pattern string, value interface{})
- func (j *Json) MustRemove(pattern string)
- func (j *Json) MustSet(pattern string, value interface{})
- func (j *Json) MustToIni() []byte
- func (j *Json) MustToIniString() string
- func (j *Json) MustToJson() []byte
- func (j *Json) MustToJsonIndent() []byte
- func (j *Json) MustToJsonIndentString() string
- func (j *Json) MustToJsonString() string
- func (j *Json) MustToProperties() []byte
- func (j *Json) MustToPropertiesString() string
- func (j *Json) MustToToml() []byte
- func (j *Json) MustToTomlString() string
- func (j *Json) MustToXml(rootTag ...string) []byte
- func (j *Json) MustToXmlIndent(rootTag ...string) []byte
- func (j *Json) MustToXmlIndentString(rootTag ...string) string
- func (j *Json) MustToXmlString(rootTag ...string) string
- func (j *Json) MustToYaml() []byte
- func (j *Json) MustToYamlString() string
- func (j *Json) Remove(pattern string) error
- func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) Set(pattern string, value interface{}) error
- func (j *Json) SetSplitChar(char byte)
- func (j *Json) SetViolenceCheck(enabled bool)
- func (j *Json) String() string
- func (j *Json) ToIni() ([]byte, error)
- func (j *Json) ToIniString() (string, error)
- func (j *Json) ToJson() ([]byte, error)
- func (j *Json) ToJsonIndent() ([]byte, error)
- func (j *Json) ToJsonIndentString() (string, error)
- func (j *Json) ToJsonString() (string, error)
- func (j *Json) ToProperties() ([]byte, error)
- func (j *Json) ToPropertiesString() (string, error)
- func (j *Json) ToToml() ([]byte, error)
- func (j *Json) ToTomlString() (string, error)
- func (j *Json) ToXml(rootTag ...string) ([]byte, error)
- func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error)
- func (j *Json) ToXmlIndentString(rootTag ...string) (string, error)
- func (j *Json) ToXmlString(rootTag ...string) (string, error)
- func (j *Json) ToYaml() ([]byte, error)
- func (j *Json) ToYamlIndent(indent string) ([]byte, error)
- func (j *Json) ToYamlString() (string, error)
- func (j *Json) UnmarshalJSON(b []byte) error
- func (j *Json) UnmarshalValue(value interface{}) error
- func (j *Json) Var() *gvar.Var
- type Options
Examples ¶
- Decode
- DecodeTo
- DecodeToJson
- Encode
- EncodeString
- IsValidDataType
- Json.Append
- Json.Array
- Json.Contains
- Json.Dump
- Json.Get
- Json.GetJson
- Json.GetJsonMap
- Json.GetJsons
- Json.Interface
- Json.Interfaces
- Json.IsNil
- Json.Len
- Json.Map
- Json.MapStrAny
- Json.MarshalJSON
- Json.MustAppend
- Json.MustRemove
- Json.MustSet
- Json.MustToIni
- Json.MustToIniString
- Json.MustToJson
- Json.MustToJsonIndent
- Json.MustToJsonIndentString
- Json.MustToJsonString
- Json.MustToProperties
- Json.MustToPropertiesString
- Json.MustToToml
- Json.MustToTomlString
- Json.MustToXml
- Json.MustToXmlIndent
- Json.MustToXmlIndentString
- Json.MustToXmlString
- Json.MustToYaml
- Json.MustToYamlString
- Json.Remove
- Json.Scan
- Json.Set
- Json.SetSplitChar
- Json.SetViolenceCheck
- Json.ToIni
- Json.ToIniString
- Json.ToJson
- Json.ToJsonIndent
- Json.ToJsonIndentString
- Json.ToJsonString
- Json.ToProperties
- Json.ToPropertiesString
- Json.ToToml
- Json.ToTomlString
- Json.ToXml
- Json.ToXmlIndent
- Json.ToXmlIndentString
- Json.ToXmlString
- Json.ToYaml
- Json.ToYamlIndent
- Json.ToYamlString
- Json.UnmarshalJSON
- Json.Var
- Load
- LoadContent
- LoadContentType
- LoadIni
- LoadJson
- LoadToml
- LoadXml
- LoadYaml
- Marshal
- MarshalIndent
- MustEncode
- MustEncodeString
- New
- NewWithOptions
- NewWithTag
- Unmarshal
- Valid
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode decodes json format `data` to golang variable. The parameter `data` can be either bytes or string type.
func DecodeTo ¶
DecodeTo decodes json format `data` to specified golang variable `v`. The parameter `data` can be either bytes or string type. The parameter `v` should be a pointer type.
func EncodeString ¶
EncodeString encodes any golang variable `value` to JSON string.
func IsValidDataType ¶
func IsValidDataType(dataType ContentType) bool
IsValidDataType checks and returns whether given `dataType` a valid data type for loading.
func Marshal ¶
Marshal is alias of Encode in order to fit the habit of json.Marshal/Unmarshal functions.
func MarshalIndent ¶
MarshalIndent is alias of json.MarshalIndent in order to fit the habit of json.MarshalIndent function.
func MustEncode ¶
func MustEncode(value interface{}) []byte
MustEncode performs as Encode, but it panics if any error occurs.
func MustEncodeString ¶
func MustEncodeString(value interface{}) string
MustEncodeString encodes any golang variable `value` to JSON string. It panics if any error occurs.
Types ¶
type ContentType ¶ added in v2.2.0
type ContentType string
const ( ContentTypeJson ContentType = `json` ContentTypeJs ContentType = `js` ContentTypeXml ContentType = `xml` ContentTypeIni ContentType = `ini` ContentTypeYaml ContentType = `yaml` ContentTypeYml ContentType = `yml` ContentTypeToml ContentType = `toml` ContentTypeProperties ContentType = `properties` )
type Json ¶
type Json struct {
// contains filtered or unexported fields
}
Json is the customized JSON struct.
func DecodeToJson ¶
DecodeToJson codes json format `data` to a Json object. The parameter `data` can be either bytes or string type.
func Load ¶
Load loads content from specified file `path`, and creates a Json object from its content.
func LoadContent ¶
LoadContent creates a Json object from given content, it checks the data type of `content` automatically, supporting data content type as follows: JSON, XML, INI, YAML and TOML.
func LoadContentType ¶
func LoadContentType(dataType ContentType, data interface{}, safe ...bool) (*Json, error)
LoadContentType creates a Json object from given type and content, supporting data content type as follows: JSON, XML, INI, YAML and TOML.
func LoadProperties ¶ added in v2.1.0
LoadProperties creates a Json object from given TOML format content.
func LoadWithOptions ¶ added in v2.1.0
LoadWithOptions creates a Json object from given JSON format content and options.
func New ¶
New creates a Json object with any variable type of `data`, but `data` should be a map or slice for data access reason, or it will make no sense.
The parameter `safe` specifies whether using this Json object in concurrent-safe context, which is false in default.
func NewWithOptions ¶
NewWithOptions creates a Json object with any variable type of `data`, but `data` should be a map or slice for data access reason, or it will make no sense.
func NewWithTag ¶
NewWithTag creates a Json object with any variable type of `data`, but `data` should be a map or slice for data access reason, or it will make no sense.
The parameter `tags` specifies priority tags for struct conversion to map, multiple tags joined with char ','.
The parameter `safe` specifies whether using this Json object in concurrent-safe context, which is false in default.
func (*Json) Append ¶
Append appends value to the value by specified `pattern`. The target value by `pattern` should be type of slice.
func (*Json) Array ¶
func (j *Json) Array() []interface{}
Array converts current Json object to []interface{}. It returns nil if fails.
func (*Json) Dump ¶
func (j *Json) Dump()
Dump prints current Json object with more manually readable.
func (*Json) Get ¶
Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given ".". It returns nil if no value found by `pattern`.
We can also access slice item by its index number in `pattern` like: "list.10", "array.0.name", "array.0.1.id".
It returns a default value specified by `def` if value for `pattern` is not found.
Example ¶
Output: {"users":{"array":["John","Ming"],"count":1}} {"array":["John","Ming"],"count":1} 1 ["John","Ming"]
func (*Json) GetJson ¶
GetJson gets the value by specified `pattern`, and converts it to an un-concurrent-safe Json object.
func (*Json) GetJsonMap ¶
GetJsonMap gets the value by specified `pattern`, and converts it to a map of un-concurrent-safe Json object.
func (*Json) GetJsons ¶
GetJsons gets the value by specified `pattern`, and converts it to a slice of un-concurrent-safe Json object.
func (*Json) Interfaces ¶
func (j *Json) Interfaces() []interface{}
Interfaces implements interface function Interfaces().
func (*Json) Len ¶
Len returns the length/size of the value by specified `pattern`. The target value by `pattern` should be type of slice or map. It returns -1 if the target value is not found, or its type is invalid.
func (*Json) Map ¶
Map converts current Json object to map[string]interface{}. It returns nil if fails.
func (Json) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*Json) MustAppend ¶
MustAppend performs as Append, but it panics if any error occurs.
func (*Json) MustRemove ¶
MustRemove performs as Remove, but it panics if any error occurs.
func (*Json) MustToIniString ¶
MustToIniString .
func (*Json) MustToJson ¶
func (*Json) MustToJsonIndent ¶
func (*Json) MustToJsonIndentString ¶
func (*Json) MustToJsonString ¶
func (*Json) MustToProperties ¶ added in v2.1.0
func (*Json) MustToPropertiesString ¶ added in v2.1.0
MustTopropertiesString
func (*Json) MustToToml ¶
func (*Json) MustToTomlString ¶
func (*Json) MustToXmlIndent ¶
func (*Json) MustToXmlIndentString ¶
func (*Json) MustToXmlString ¶
func (*Json) MustToYamlString ¶
func (*Json) Remove ¶
Remove deletes value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default.
func (*Json) Scan ¶
Scan automatically calls Struct or Structs function according to the type of parameter `pointer` to implement the converting.
func (*Json) Set ¶
Set sets value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default.
func (*Json) SetSplitChar ¶
SetSplitChar sets the separator char for hierarchical data access.
func (*Json) SetViolenceCheck ¶
SetViolenceCheck enables/disables violence check for hierarchical data access.
func (*Json) ToIni ¶
ToIni json to ini
Example ¶
======================================================================== INI ========================================================================
Output:
func (*Json) ToIniString ¶
ToIniString ini to string
func (*Json) ToJson ¶
Example ¶
======================================================================== JSON ========================================================================
Output: {"Age":18,"Name":"John"}
func (*Json) ToJsonIndent ¶
func (*Json) ToJsonIndentString ¶
func (*Json) ToJsonString ¶
func (*Json) ToProperties ¶ added in v2.1.0
======================================================================== properties ======================================================================== Toproperties json to properties
Example ¶
======================================================================== Properties ========================================================================
Output:
func (*Json) ToPropertiesString ¶ added in v2.1.0
TopropertiesString properties to string
func (*Json) ToToml ¶
Example ¶
======================================================================== TOML ========================================================================
Output: Age = 18 Name = "John"
func (*Json) ToTomlString ¶
func (*Json) ToXml ¶
Example ¶
======================================================================== XML ========================================================================
Output: <doc><Age>18</Age><Name>John</Name></doc>
func (*Json) ToXmlIndent ¶
func (*Json) ToXmlIndentString ¶
func (*Json) ToXmlString ¶
func (*Json) ToYaml ¶
Example ¶
======================================================================== YAML ========================================================================
Output: Age: 18 Name: John
func (*Json) ToYamlIndent ¶
func (*Json) ToYamlString ¶
func (*Json) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*Json) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for Json.
type Options ¶
type Options struct { Safe bool // Mark this object is for in concurrent-safe usage. This is especially for Json object creating. Tags string // Custom priority tags for decoding, eg: "json,yaml,MyTag". This is especially for struct parsing into Json object. Type ContentType // Type specifies the data content type, eg: json, xml, yaml, toml, ini. StrNumber bool // StrNumber causes the Decoder to unmarshal a number into an interface{} as a string instead of as a float64. }
Options for Json object creating/loading.