gjson

package
v2.4.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: MIT Imports: 22 Imported by: 454

Documentation

Overview

Package gjson provides convenient API for JSON/XML/INI/YAML/TOML data handling.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(data interface{}, options ...Options) (interface{}, error)

Decode decodes json format `data` to golang variable. The parameter `data` can be either bytes or string type.

Example
Output:

map[name:john score:100]

func DecodeTo

func DecodeTo(data interface{}, v interface{}, options ...Options) (err error)

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.

Example
Output:

{Name:john Score:100}

func Encode

func Encode(value interface{}) ([]byte, error)

Encode encodes any golang variable `value` to JSON bytes.

Example
Output:

{"Name":"John","Age":18}

func EncodeString

func EncodeString(value interface{}) (string, error)

EncodeString encodes any golang variable `value` to JSON string.

Example
Output:

{"Name":"John","Age":18}

func IsValidDataType

func IsValidDataType(dataType ContentType) bool

IsValidDataType checks and returns whether given `dataType` a valid data type for loading.

Example
Output:

true
true
true
false
false
false
false
true
true

func Marshal

func Marshal(v interface{}) (marshaledBytes []byte, err error)

Marshal is alias of Encode in order to fit the habit of json.Marshal/Unmarshal functions.

Example
Output:

{"name":"john","score":100}
{"Name":"Guo Qiang","Age":18}

func MarshalIndent

func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error)

MarshalIndent is alias of json.MarshalIndent in order to fit the habit of json.MarshalIndent function.

Example
Output:

{
	"Name": "John",
	"Age": 18
}

func MustEncode

func MustEncode(value interface{}) []byte

MustEncode performs as Encode, but it panics if any error occurs.

Example
Output:

{"Name":"John","Age":18}

func MustEncodeString

func MustEncodeString(value interface{}) string

MustEncodeString encodes any golang variable `value` to JSON string. It panics if any error occurs.

Example
Output:

{"Name":"John","Age":18}

func Unmarshal

func Unmarshal(data []byte, v interface{}) (err error)

Unmarshal is alias of DecodeTo in order to fit the habit of json.Marshal/Unmarshal functions.

Example
Output:

{Name:john Score:100}

func Valid

func Valid(data interface{}) bool

Valid checks whether `data` is a valid JSON data type. The parameter `data` specifies the json format data, which can be either bytes or string type.

Example
Output:

true
false

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

func DecodeToJson(data interface{}, options ...Options) (*Json, error)

DecodeToJson codes json format `data` to a Json object. The parameter `data` can be either bytes or string type.

Example
Output:

func Load

func Load(path string, safe ...bool) (*Json, error)

Load loads content from specified file `path`, and creates a Json object from its content.

Example
Output:

john
100

func LoadContent

func LoadContent(data interface{}, safe ...bool) (*Json, error)

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.

Example
Output:

john
100

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.

Example
Output:

john
100
john
100

func LoadIni

func LoadIni(data interface{}, safe ...bool) (*Json, error)

LoadIni creates a Json object from given INI format content.

Example
Output:

john
100

func LoadJson

func LoadJson(data interface{}, safe ...bool) (*Json, error)

LoadJson creates a Json object from given JSON format content.

Example
Output:

john
100

func LoadProperties added in v2.1.0

func LoadProperties(data interface{}, safe ...bool) (*Json, error)

LoadProperties creates a Json object from given TOML format content.

func LoadToml

func LoadToml(data interface{}, safe ...bool) (*Json, error)

LoadToml creates a Json object from given TOML format content.

Example
Output:

john
100

func LoadWithOptions added in v2.1.0

func LoadWithOptions(data interface{}, options Options) (*Json, error)

LoadWithOptions creates a Json object from given JSON format content and options.

func LoadXml

func LoadXml(data interface{}, safe ...bool) (*Json, error)

LoadXml creates a Json object from given XML format content.

Example
Output:

john
100

func LoadYaml

func LoadYaml(data interface{}, safe ...bool) (*Json, error)

LoadYaml creates a Json object from given YAML format content.

Example
Output:

john
100

func New

func New(data interface{}, safe ...bool) *Json

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.

Example
Output:

john
100

func NewWithOptions

func NewWithOptions(data interface{}, options Options) *Json

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.

Example
Output:

john
100
engineer

func NewWithTag

func NewWithTag(data interface{}, tags string, safe ...bool) *Json

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.

Example
Output:

john
100
engineer

func (*Json) Append

func (j *Json) Append(pattern string, value interface{}) error

Append appends value to the value by specified `pattern`. The target value by `pattern` should be type of slice.

Example
Output:

[John Ming Lily]

func (*Json) Array

func (j *Json) Array() []interface{}

Array converts current Json object to []interface{}. It returns nil if fails.

Example
Output:

["John","Ming"]

func (*Json) Contains

func (j *Json) Contains(pattern string) bool

Contains checks whether the value by specified `pattern` exist.

Example
Output:

true
false

func (*Json) Dump

func (j *Json) Dump()

Dump prints current Json object with more manually readable.

Example
Output:

func (*Json) Get

func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var

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

func (j *Json) GetJson(pattern string, def ...interface{}) *Json

GetJson gets the value by specified `pattern`, and converts it to an un-concurrent-safe Json object.

Example
Output:

[John Ming]

func (*Json) GetJsonMap

func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json

GetJsonMap gets the value by specified `pattern`, and converts it to a map of un-concurrent-safe Json object.

Example
Output:

func (*Json) GetJsons

func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json

GetJsons gets the value by specified `pattern`, and converts it to a slice of un-concurrent-safe Json object.

Example
Output:

map[Age:18 Name:John]
map[Age:20 Name:Tom]

func (*Json) Interface

func (j *Json) Interface() interface{}

Interface returns the json value.

Example
Output:

map[Age:18 Name:John]
<nil>

func (*Json) Interfaces

func (j *Json) Interfaces() []interface{}

Interfaces implements interface function Interfaces().

Example
Output:

[{John 18} {Tom 20}]

func (*Json) IsNil

func (j *Json) IsNil() bool

IsNil checks whether the value pointed by `j` is nil.

Example
Output:

false
true

func (*Json) Len

func (j *Json) Len(pattern string) int

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.

Example
Output:

2
3

func (*Json) Map

func (j *Json) Map() map[string]interface{}

Map converts current Json object to map[string]interface{}. It returns nil if fails.

Example
Output:

map[addr:ChengDu age:18 name:John]

func (*Json) MapStrAny

func (j *Json) MapStrAny() map[string]interface{}

MapStrAny implements interface function MapStrAny().

Example
Output:

map[Age:18 Name:John]

func (Json) MarshalJSON

func (j Json) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
Output:

{"Age":18,"Name":"John"}

func (*Json) MustAppend

func (j *Json) MustAppend(pattern string, value interface{})

MustAppend performs as Append, but it panics if any error occurs.

Example
Output:

[John Ming Lily]

func (*Json) MustRemove

func (j *Json) MustRemove(pattern string)

MustRemove performs as Remove, but it panics if any error occurs.

Example
Output:

{"Name":"John"}

func (*Json) MustSet

func (j *Json) MustSet(pattern string, value interface{})

MustSet performs as Set, but it panics if any error occurs.

Example
Output:

{"Addr":"ChengDu","Age":18,"Name":"John"}

func (*Json) MustToIni

func (j *Json) MustToIni() []byte
Example
Output:

Name=John

func (*Json) MustToIniString

func (j *Json) MustToIniString() string

MustToIniString .

Example
Output:

Name=John

func (*Json) MustToJson

func (j *Json) MustToJson() []byte
Example
Output:

{"Age":18,"Name":"John"}

func (*Json) MustToJsonIndent

func (j *Json) MustToJsonIndent() []byte
Example
Output:

{
	"Age": 18,
	"Name": "John"
}

func (*Json) MustToJsonIndentString

func (j *Json) MustToJsonIndentString() string
Example
Output:

{
	"Age": 18,
	"Name": "John"
}

func (*Json) MustToJsonString

func (j *Json) MustToJsonString() string
Example
Output:

{"Age":18,"Name":"John"}

func (*Json) MustToProperties added in v2.1.0

func (j *Json) MustToProperties() []byte
Example
Output:

name = John

func (*Json) MustToPropertiesString added in v2.1.0

func (j *Json) MustToPropertiesString() string

MustTopropertiesString

Example
Output:

name = John

func (*Json) MustToToml

func (j *Json) MustToToml() []byte
Example
Output:

Age = 18
Name = "John"

func (*Json) MustToTomlString

func (j *Json) MustToTomlString() string
Example
Output:

Age = 18
Name = "John"

func (*Json) MustToXml

func (j *Json) MustToXml(rootTag ...string) []byte
Example
Output:

<doc><Age>18</Age><Name>John</Name></doc>

func (*Json) MustToXmlIndent

func (j *Json) MustToXmlIndent(rootTag ...string) []byte
Example
Output:

<doc>
	<Age>18</Age>
	<Name>John</Name>
</doc>

func (*Json) MustToXmlIndentString

func (j *Json) MustToXmlIndentString(rootTag ...string) string
Example
Output:

<doc>
	<Age>18</Age>
	<Name>John</Name>
</doc>

func (*Json) MustToXmlString

func (j *Json) MustToXmlString(rootTag ...string) string
Example
Output:

<doc><Age>18</Age><Name>John</Name></doc>

func (*Json) MustToYaml

func (j *Json) MustToYaml() []byte
Example
Output:

Age: 18
Name: John

func (*Json) MustToYamlString

func (j *Json) MustToYamlString() string
Example
Output:

Age: 18
Name: John

func (*Json) Remove

func (j *Json) Remove(pattern string) error

Remove deletes value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default.

Example
Output:

{"Name":"John"}

func (*Json) Scan

func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error

Scan automatically calls Struct or Structs function according to the type of parameter `pointer` to implement the converting.

Example
Output:

func (*Json) Set

func (j *Json) Set(pattern string, value interface{}) error

Set sets value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default.

Example
Output:

{"Addr":"ChengDu","Age":18,"Friends":["Tom"],"Name":"John"}

func (*Json) SetSplitChar

func (j *Json) SetSplitChar(char byte)

SetSplitChar sets the separator char for hierarchical data access.

Example
Output:

John Score: 99.5

func (*Json) SetViolenceCheck

func (j *Json) SetViolenceCheck(enabled bool)

SetViolenceCheck enables/disables violence check for hierarchical data access.

Example
Output:

Users Count: 100
Users Count: 101

func (*Json) String added in v2.2.6

func (j *Json) String() string

String returns current Json object as string.

func (*Json) ToIni

func (j *Json) ToIni() ([]byte, error)

ToIni json to ini

Example

======================================================================== INI ========================================================================

Output:

func (*Json) ToIniString

func (j *Json) ToIniString() (string, error)

ToIniString ini to string

Example
Output:

Name=John

func (*Json) ToJson

func (j *Json) ToJson() ([]byte, error)
Example

======================================================================== JSON ========================================================================

Output:

{"Age":18,"Name":"John"}

func (*Json) ToJsonIndent

func (j *Json) ToJsonIndent() ([]byte, error)
Example
Output:

{
	"Age": 18,
	"Name": "John"
}

func (*Json) ToJsonIndentString

func (j *Json) ToJsonIndentString() (string, error)
Example
Output:

{
	"Age": 18,
	"Name": "John"
}

func (*Json) ToJsonString

func (j *Json) ToJsonString() (string, error)
Example
Output:

{"Age":18,"Name":"John"}

func (*Json) ToProperties added in v2.1.0

func (j *Json) ToProperties() ([]byte, error)

======================================================================== properties ======================================================================== Toproperties json to properties

Example

======================================================================== Properties ========================================================================

Output:

func (*Json) ToPropertiesString added in v2.1.0

func (j *Json) ToPropertiesString() (string, error)

TopropertiesString properties to string

Example
Output:

name = John

func (*Json) ToToml

func (j *Json) ToToml() ([]byte, error)
Example

======================================================================== TOML ========================================================================

Output:

Age = 18
Name = "John"

func (*Json) ToTomlString

func (j *Json) ToTomlString() (string, error)
Example
Output:

Age = 18
Name = "John"

func (*Json) ToXml

func (j *Json) ToXml(rootTag ...string) ([]byte, error)
Example

======================================================================== XML ========================================================================

Output:

<doc><Age>18</Age><Name>John</Name></doc>

func (*Json) ToXmlIndent

func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error)
Example
Output:

<doc>
	<Age>18</Age>
	<Name>John</Name>
</doc>

func (*Json) ToXmlIndentString

func (j *Json) ToXmlIndentString(rootTag ...string) (string, error)
Example
Output:

<doc>
	<Age>18</Age>
	<Name>John</Name>
</doc>

func (*Json) ToXmlString

func (j *Json) ToXmlString(rootTag ...string) (string, error)
Example
Output:

<doc><Age>18</Age><Name>John</Name></doc>

func (*Json) ToYaml

func (j *Json) ToYaml() ([]byte, error)
Example

======================================================================== YAML ========================================================================

Output:

Age: 18
Name: John

func (*Json) ToYamlIndent

func (j *Json) ToYamlIndent(indent string) ([]byte, error)
Example
Output:

Age: 18
Name: John

func (*Json) ToYamlString

func (j *Json) ToYamlString() (string, error)
Example
Output:

Age: 18
Name: John

func (*Json) UnmarshalJSON

func (j *Json) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
Output:

map[Age:18 Name:John]

func (*Json) UnmarshalValue

func (j *Json) UnmarshalValue(value interface{}) error

UnmarshalValue is an interface implement which sets any type of value for Json.

func (*Json) Var

func (j *Json) Var() *gvar.Var

Var returns the json value as *gvar.Var.

Example
Output:

{"Age":18,"Name":"John"}
map[Age:18 Name:John]

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.

Jump to

Keyboard shortcuts

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