Documentation ¶
Overview ¶
Package gjson provides convenient API for JSON/XML/INI/YAML/TOML data handling.
Example (ConversionNormalFormats) ¶
Output: JSON: {"users":{"array":["John","Ming"],"count":1}} ====================== XML: <users><array>John</array><array>Ming</array><count>1</count></users> ====================== YAML: users: array: - John - Ming count: 1 ====================== TOML: [users] array = ["John", "Ming"] count = 1.0
Example (DataSetCreate2) ¶
Output: [{"id":0,"name":"student-0"},{"id":1,"name":"student-1"},{"id":2,"name":"student-2"},{"id":3,"name":"student-3"},{"id":4,"name":"student-4"}]
Example (DataSetRuntimeEdit) ¶
Output: John Score: 100 {"users":{"count":2,"list":[{"name":"Ming","score":60},{"name":"John","score":100}]}}
Index ¶
- func Decode(data interface{}) (interface{}, error)
- func DecodeTo(data interface{}, v interface{}) error
- func Encode(value interface{}) ([]byte, error)
- func IsValidDataType(dataType string) bool
- func Valid(data interface{}) bool
- type Json
- func DecodeToJson(data interface{}, safe ...bool) (*Json, error)
- func Load(path string, safe ...bool) (*Json, error)
- func LoadContent(data interface{}, safe ...bool) (*Json, error)
- func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, error)
- func LoadIni(data interface{}, safe ...bool) (*Json, error)
- func LoadJson(data interface{}, safe ...bool) (*Json, error)
- func LoadToml(data interface{}, safe ...bool) (*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 NewWithTag(data interface{}, tags string, safe ...bool) *Json
- func (j *Json) Append(pattern string, value interface{}) error
- func (j *Json) Contains(pattern string) bool
- func (j *Json) Dump()
- func (j *Json) Export() string
- func (j *Json) Get(pattern string, def ...interface{}) interface{}
- func (j *Json) GetArray(pattern string, def ...interface{}) []interface{}
- func (j *Json) GetBool(pattern string, def ...interface{}) bool
- func (j *Json) GetBytes(pattern string, def ...interface{}) []byte
- func (j *Json) GetDuration(pattern string, def ...interface{}) time.Duration
- func (j *Json) GetFloat32(pattern string, def ...interface{}) float32
- func (j *Json) GetFloat64(pattern string, def ...interface{}) float64
- func (j *Json) GetFloats(pattern string, def ...interface{}) []float64
- func (j *Json) GetGTime(pattern string, format ...string) *gtime.Time
- func (j *Json) GetInt(pattern string, def ...interface{}) int
- func (j *Json) GetInt16(pattern string, def ...interface{}) int16
- func (j *Json) GetInt32(pattern string, def ...interface{}) int32
- func (j *Json) GetInt64(pattern string, def ...interface{}) int64
- func (j *Json) GetInt8(pattern string, def ...interface{}) int8
- func (j *Json) GetInterfaces(pattern string, def ...interface{}) []interface{}
- func (j *Json) GetInts(pattern string, def ...interface{}) []int
- 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) GetMap(pattern string, def ...interface{}) map[string]interface{}
- func (j *Json) GetMapStrStr(pattern string, def ...interface{}) map[string]string
- func (j *Json) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetMaps(pattern string, def ...interface{}) []map[string]interface{}
- func (j *Json) GetScan(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetScanDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetString(pattern string, def ...interface{}) string
- func (j *Json) GetStrings(pattern string, def ...interface{}) []string
- func (j *Json) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (j *Json) GetTime(pattern string, format ...string) time.Time
- func (j *Json) GetUint(pattern string, def ...interface{}) uint
- func (j *Json) GetUint16(pattern string, def ...interface{}) uint16
- func (j *Json) GetUint32(pattern string, def ...interface{}) uint32
- func (j *Json) GetUint64(pattern string, def ...interface{}) uint64
- func (j *Json) GetUint8(pattern string, def ...interface{}) uint8
- func (j *Json) GetVar(pattern string, def ...interface{}) *gvar.Var
- func (j *Json) GetVars(pattern string, def ...interface{}) []*gvar.Var
- func (j *Json) IsNil() bool
- func (j *Json) Len(pattern string) int
- func (j *Json) MarshalJSON() ([]byte, error)
- 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) 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) Set(pattern string, value interface{}) error
- func (j *Json) SetSplitChar(char byte)
- func (j *Json) SetViolenceCheck(enabled bool)
- func (j *Json) ToArray() []interface{}
- 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) ToMap() map[string]interface{}
- func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToScan(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToScanDeep(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]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) ToYamlString() (string, error)
- func (j *Json) UnmarshalJSON(b []byte) error
- func (j *Json) UnmarshalValue(value interface{}) error
- func (j *Json) Value() interface{}
Examples ¶
- Package (ConversionGetStruct)
- Package (ConversionNormalFormats)
- Package (ConversionToStruct)
- Package (DataSetCreate1)
- Package (DataSetCreate2)
- Package (DataSetRuntimeEdit)
- Package (LoadContent)
- Package (LoadJson)
- Package (LoadXml)
- Package (MapSliceChange)
- Package (NewFromJson)
- Package (NewFromStruct)
- Package (NewFromStructWithTag)
- Package (NewFromXml)
- Package (PatternCustomSplitChar)
- Package (PatternGet)
- Package (PatternViolenceCheck)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
func Decode(data interface{}) (interface{}, error)
Decode decodes json format <data> to golang variable. The parameter <data> can be either bytes or string type.
func DecodeTo ¶
func DecodeTo(data interface{}, v interface{}) error
Decode 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 IsValidDataType ¶ added in v1.13.7
IsValidDataType checks and returns whether given <dataType> a valid data type for loading.
Types ¶
type Json ¶
type Json struct {
// contains filtered or unexported fields
}
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 ¶ added in v1.13.4
LoadContentType creates a Json object from given type and content, supporting data content type as follows: JSON, XML, INI, YAML and TOML.
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 NewWithTag ¶ added in v1.11.5
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) 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 empty or string ".". 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.
func (*Json) GetArray ¶
GetArray retrieves the value by specified <pattern>, and converts it to a slice of []interface{}.
func (*Json) GetBool ¶
GetBool retrieves the value by specified <pattern>, converts and returns it as bool. It returns false when value is: "", 0, false, off, nil; or returns true instead.
func (*Json) GetBytes ¶
GetBytes retrieves the value by specified <pattern> and converts it to []byte.
func (*Json) GetDuration ¶
GetDuration retrieves the value by specified <pattern> and converts it to time.Duration.
func (*Json) GetFloat32 ¶
GetFloat32 retrieves the value by specified <pattern> and converts it to float32.
func (*Json) GetFloat64 ¶
GetFloat64 retrieves the value by specified <pattern> and converts it to float64.
func (*Json) GetFloats ¶
GetFloats retrieves the value by specified <pattern> and converts it to []float64.
func (*Json) GetGTime ¶
GetGTime retrieves the value by specified <pattern> and converts it to *gtime.Time.
func (*Json) GetInt16 ¶
GetInt16 retrieves the value by specified <pattern> and converts it to int16.
func (*Json) GetInt32 ¶
GetInt32 retrieves the value by specified <pattern> and converts it to int32.
func (*Json) GetInt64 ¶
GetInt64 retrieves the value by specified <pattern> and converts it to int64.
func (*Json) GetInterfaces ¶
GetInterfaces is alias of GetArray. See GetArray.
func (*Json) GetJson ¶
GetJson gets the value by specified <pattern>, and converts it to a 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) GetMap ¶
GetMap retrieves and returns the value by specified <pattern> as map[string]interface{}.
func (*Json) GetMapStrStr ¶ added in v1.10.1
GetMapStrStr retrieves and returns the value by specified <pattern> as map[string]string.
func (*Json) GetMapToMap ¶ added in v1.9.3
GetMapToMap retrieves the value by specified <pattern> and converts it to specified map variable. See gconv.MapToMap.
func (*Json) GetMapToMapDeep ¶ added in v1.9.3
func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetMapToMapDeep retrieves the value by specified <pattern> and converts it to specified map variable recursively. See gconv.MapToMapDeep.
func (*Json) GetMapToMaps ¶ added in v1.9.3
func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error
GetMapToMaps retrieves the value by specified <pattern> and converts it to specified map slice variable. See gconv.MapToMaps.
func (*Json) GetMapToMapsDeep ¶ added in v1.9.3
func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetMapToMapsDeep retrieves the value by specified <pattern> and converts it to specified map slice variable recursively. See gconv.MapToMapsDeep.
func (*Json) GetMaps ¶ added in v1.12.3
GetMaps retrieves and returns the value by specified <pattern> as []map[string]interface{}.
func (*Json) GetScan ¶ added in v1.13.2
GetScan automatically calls Struct or Structs function according to the type of parameter <pointer> to implement the converting..
func (*Json) GetScanDeep ¶ added in v1.13.2
GetScanDeep automatically calls StructDeep or StructsDeep function according to the type of parameter <pointer> to implement the converting..
func (*Json) GetString ¶
GetString retrieves the value by specified <pattern> and converts it to string.
func (*Json) GetStrings ¶
GetStrings retrieves the value by specified <pattern> and converts it to []string.
func (*Json) GetStruct ¶
GetStruct retrieves the value by specified <pattern> and converts it to specified object <pointer>. The <pointer> should be the pointer to an object.
func (*Json) GetStructDeep ¶
func (j *Json) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetStructDeep does GetStruct recursively.
func (*Json) GetStructs ¶
GetStructs converts any slice to given struct slice.
func (*Json) GetStructsDeep ¶
func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetStructsDeep converts any slice to given struct slice recursively.
func (*Json) GetTime ¶
GetTime retrieves the value by specified <pattern> and converts it to time.Time.
func (*Json) GetUint16 ¶
GetUint16 retrieves the value by specified <pattern> and converts it to uint16.
func (*Json) GetUint32 ¶
GetUint32 retrieves the value by specified <pattern> and converts it to uint32.
func (*Json) GetUint64 ¶
GetUint64 retrieves the value by specified <pattern> and converts it to uint64.
func (*Json) GetUint8 ¶
GetUint8 retrieves the value by specified <pattern> and converts it to uint8.
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) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*Json) MustToIniString ¶ added in v1.10.1
func (*Json) MustToJson ¶ added in v1.10.1
func (*Json) MustToJsonIndent ¶ added in v1.10.1
func (*Json) MustToJsonIndentString ¶ added in v1.10.1
func (*Json) MustToJsonString ¶ added in v1.10.1
func (*Json) MustToToml ¶ added in v1.10.1
func (*Json) MustToTomlString ¶ added in v1.10.1
func (*Json) MustToXmlIndent ¶ added in v1.10.1
func (*Json) MustToXmlIndentString ¶ added in v1.10.1
func (*Json) MustToXmlString ¶ added in v1.10.1
func (*Json) MustToYaml ¶ added in v1.10.1
func (*Json) MustToYamlString ¶ added in v1.10.1
func (*Json) Remove ¶
Remove deletes value with specified <pattern>. It supports hierarchical data access by char separator, which is '.' in default.
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) ToArray ¶
func (j *Json) ToArray() []interface{}
ToArray converts current Json object to []interface{}. It returns nil if fails.
func (*Json) ToIniString ¶
func (*Json) ToJsonIndent ¶
func (*Json) ToJsonIndentString ¶
func (*Json) ToJsonString ¶
func (*Json) ToMap ¶
ToMap converts current Json object to map[string]interface{}. It returns nil if fails.
func (*Json) ToMapToMap ¶ added in v1.9.3
ToMapToMap converts current Json object to specified map variable. The parameter of <pointer> should be type of *map.
func (*Json) ToMapToMapDeep ¶ added in v1.9.3
ToMapToMapDeep converts current Json object to specified map variable recursively. The parameter of <pointer> should be type of *map.
func (*Json) ToMapToMaps ¶ added in v1.9.3
ToMapToMaps converts current Json object to specified map variable slice. The parameter of <pointer> should be type of []map/*map.
func (*Json) ToMapToMapsDeep ¶ added in v1.9.3
ToMapToMapsDeep converts current Json object to specified map variable slice recursively. The parameter of <pointer> should be type of []map/*map.
func (*Json) ToScan ¶ added in v1.13.2
ToScan automatically calls Struct or Structs function according to the type of parameter <pointer> to implement the converting..
func (*Json) ToScanDeep ¶ added in v1.13.2
ToScanDeep automatically calls StructDeep or StructsDeep function according to the type of parameter <pointer> to implement the converting..
func (*Json) ToStruct ¶
ToStruct converts current Json object to specified object. The <pointer> should be a pointer type of *struct.
func (*Json) ToStructDeep ¶
ToStructDeep converts current Json object to specified object recursively. The <pointer> should be a pointer type of *struct.
func (*Json) ToStructs ¶
ToStructs converts current Json object to specified object slice. The <pointer> should be a pointer type of []struct/*struct.
func (*Json) ToStructsDeep ¶
ToStructsDeep converts current Json object to specified object slice recursively. The <pointer> should be a pointer type of []struct/*struct.
func (*Json) ToTomlString ¶
func (*Json) ToXmlIndentString ¶
func (*Json) ToYamlString ¶
func (*Json) UnmarshalJSON ¶ added in v1.11.5
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*Json) UnmarshalValue ¶ added in v1.11.5
UnmarshalValue is an interface implement which sets any type of value for Json.