qjson

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2021 License: MIT Imports: 13 Imported by: 0

README

qjson

gjson 进行增强

  • 使用

go get -v -u github.com/lhlyu/qjson

  • 例子
func main() {
    s := `{"a":1,"b":"joke","c":1.234,"d":true,"e":-10,"f":["f1","f2","f3",false,1.2,3],"g":[1,2,3],"h":[],"i":{"i1":"a","i2":123,"i3":false,"i4":null},"j":null}`
    ok := qjson.Valid(s)
    fmt.Println("vaild:", ok)            // vaild: true
    r := qjson.Parse(s)
    P(r.GetAny("a").ToInt32())           // int32 - 1
    P(r.GetAny("b").ToString())          // string - joke
    P(r.GetAny("c").ToFloat32())         // float32 - 1.234
    P(r.GetAny("d").ToBool())            // bool - true
    P(r.GetAny("e").ToInt64())           // int64 - -10
    P(r.Get("f").GetAny("1").ToString()) // string - f2
    P(r.Get("g.#").GetAny("").ToInt())   // int - 0
    P(r.GetAny("h").ToSlice())           // []interface {} - []
    P(r.GetAny("i.i2").ToString())       // string - 123
}


func P(v interface{}) {
	fmt.Printf("%T - %v \n", v, v)
}

更多

Documentation

Overview

对gjson源码进行了部分改造

Index

Constants

This section is empty.

Variables

View Source
var DisableModifiers = false

DisableModifiers will disable the modifier syntax

Functions

func AddModifier

func AddModifier(name string, fn func(json, arg string) string)

AddModifier binds a custom modifier command to the GJSON syntax. This operation is not thread safe and should be executed prior to using all other gjson function.

func ForEachLine

func ForEachLine(json string, iterator func(line Result) bool)

ForEachLine iterates through lines of JSON as specified by the JSON Lines format (http://jsonlines.org/). Each line is returned as a GJSON Result.

func ModifierExists

func ModifierExists(name string, fn func(json, arg string) string) bool

ModifierExists returns true when the specified modifier exists.

func StringToDate added in v1.0.1

func StringToDate(s string) (time.Time, error)

StringToDate attempts to parse a string into a time.Time type using a predefined list of formats. If no suitable format is found, an error is returned.

func ToBool added in v1.0.1

func ToBool(i interface{}) bool

ToBool casts an interface to a bool type.

func ToBoolE added in v1.0.1

func ToBoolE(i interface{}) (bool, error)

ToBoolE casts an interface to a bool type.

func ToBoolSlice added in v1.0.1

func ToBoolSlice(i interface{}) []bool

ToBoolSlice casts an interface to a []bool type.

func ToBoolSliceE added in v1.0.1

func ToBoolSliceE(i interface{}) ([]bool, error)

ToBoolSliceE casts an interface to a []bool type.

func ToDuration added in v1.0.1

func ToDuration(i interface{}) time.Duration

ToDuration casts an interface to a time.Duration type.

func ToDurationE added in v1.0.1

func ToDurationE(i interface{}) (d time.Duration, err error)

ToDurationE casts an interface to a time.Duration type.

func ToDurationSlice added in v1.0.1

func ToDurationSlice(i interface{}) []time.Duration

ToDurationSlice casts an interface to a []time.Duration type.

func ToDurationSliceE added in v1.0.1

func ToDurationSliceE(i interface{}) ([]time.Duration, error)

ToDurationSliceE casts an interface to a []time.Duration type.

func ToFloat32 added in v1.0.1

func ToFloat32(i interface{}) float32

ToFloat32 casts an interface to a float32 type.

func ToFloat32E added in v1.0.1

func ToFloat32E(i interface{}) (float32, error)

ToFloat32E casts an interface to a float32 type.

func ToFloat64 added in v1.0.1

func ToFloat64(i interface{}) float64

ToFloat64 casts an interface to a float64 type.

func ToFloat64E added in v1.0.1

func ToFloat64E(i interface{}) (float64, error)

ToFloat64E casts an interface to a float64 type.

func ToInt added in v1.0.1

func ToInt(i interface{}) int

ToInt casts an interface to an int type.

func ToInt16 added in v1.0.1

func ToInt16(i interface{}) int16

ToInt16 casts an interface to an int16 type.

func ToInt16E added in v1.0.1

func ToInt16E(i interface{}) (int16, error)

ToInt16E casts an interface to an int16 type.

func ToInt32 added in v1.0.1

func ToInt32(i interface{}) int32

ToInt32 casts an interface to an int32 type.

func ToInt32E added in v1.0.1

func ToInt32E(i interface{}) (int32, error)

ToInt32E casts an interface to an int32 type.

func ToInt64 added in v1.0.1

func ToInt64(i interface{}) int64

ToInt64 casts an interface to an int64 type.

func ToInt64E added in v1.0.1

func ToInt64E(i interface{}) (int64, error)

ToInt64E casts an interface to an int64 type.

func ToInt8 added in v1.0.1

func ToInt8(i interface{}) int8

ToInt8 casts an interface to an int8 type.

func ToInt8E added in v1.0.1

func ToInt8E(i interface{}) (int8, error)

ToInt8E casts an interface to an int8 type.

func ToIntE added in v1.0.1

func ToIntE(i interface{}) (int, error)

ToIntE casts an interface to an int type.

func ToIntSlice added in v1.0.1

func ToIntSlice(i interface{}) []int

ToIntSlice casts an interface to a []int type.

func ToIntSliceE added in v1.0.1

func ToIntSliceE(i interface{}) ([]int, error)

ToIntSliceE casts an interface to a []int type.

func ToSlice added in v1.0.1

func ToSlice(i interface{}) []interface{}

ToSlice casts an interface to a []interface{} type.

func ToSliceE added in v1.0.1

func ToSliceE(i interface{}) ([]interface{}, error)

ToSliceE casts an interface to a []interface{} type.

func ToString added in v1.0.1

func ToString(i interface{}) string

ToString casts an interface to a string type.

func ToStringE added in v1.0.1

func ToStringE(i interface{}) (string, error)

ToStringE casts an interface to a string type.

func ToStringMap added in v1.0.1

func ToStringMap(i interface{}) map[string]interface{}

ToStringMap casts an interface to a map[string]interface{} type.

func ToStringMapBool added in v1.0.1

func ToStringMapBool(i interface{}) map[string]bool

ToStringMapBool casts an interface to a map[string]bool type.

func ToStringMapBoolE added in v1.0.1

func ToStringMapBoolE(i interface{}) (map[string]bool, error)

ToStringMapBoolE casts an interface to a map[string]bool type.

func ToStringMapE added in v1.0.1

func ToStringMapE(i interface{}) (map[string]interface{}, error)

ToStringMapE casts an interface to a map[string]interface{} type.

func ToStringMapInt added in v1.0.1

func ToStringMapInt(i interface{}) map[string]int

ToStringMapInt casts an interface to a map[string]int type.

func ToStringMapInt64 added in v1.0.1

func ToStringMapInt64(i interface{}) map[string]int64

ToStringMapInt64 casts an interface to a map[string]int64 type.

func ToStringMapInt64E added in v1.0.1

func ToStringMapInt64E(i interface{}) (map[string]int64, error)

ToStringMapInt64E casts an interface to a map[string]int64{} type.

func ToStringMapIntE added in v1.0.1

func ToStringMapIntE(i interface{}) (map[string]int, error)

ToStringMapIntE casts an interface to a map[string]int{} type.

func ToStringMapString added in v1.0.1

func ToStringMapString(i interface{}) map[string]string

ToStringMapString casts an interface to a map[string]string type.

func ToStringMapStringE added in v1.0.1

func ToStringMapStringE(i interface{}) (map[string]string, error)

ToStringMapStringE casts an interface to a map[string]string type.

func ToStringMapStringSlice added in v1.0.1

func ToStringMapStringSlice(i interface{}) map[string][]string

ToStringMapStringSlice casts an interface to a map[string][]string type.

func ToStringMapStringSliceE added in v1.0.1

func ToStringMapStringSliceE(i interface{}) (map[string][]string, error)

ToStringMapStringSliceE casts an interface to a map[string][]string type.

func ToStringSlice added in v1.0.1

func ToStringSlice(i interface{}) []string

ToStringSlice casts an interface to a []string type.

func ToStringSliceE added in v1.0.1

func ToStringSliceE(i interface{}) ([]string, error)

ToStringSliceE casts an interface to a []string type.

func ToTime added in v1.0.1

func ToTime(i interface{}) time.Time

ToTime casts an interface to a time.Time type.

func ToTimeE added in v1.0.1

func ToTimeE(i interface{}) (tim time.Time, err error)

ToTimeE casts an interface to a time.Time type.

func ToUint added in v1.0.1

func ToUint(i interface{}) uint

ToUint casts an interface to a uint type.

func ToUint16 added in v1.0.1

func ToUint16(i interface{}) uint16

ToUint16 casts an interface to a uint16 type.

func ToUint16E added in v1.0.1

func ToUint16E(i interface{}) (uint16, error)

ToUint16E casts an interface to a uint16 type.

func ToUint32 added in v1.0.1

func ToUint32(i interface{}) uint32

ToUint32 casts an interface to a uint32 type.

func ToUint32E added in v1.0.1

func ToUint32E(i interface{}) (uint32, error)

ToUint32E casts an interface to a uint32 type.

func ToUint64 added in v1.0.1

func ToUint64(i interface{}) uint64

ToUint64 casts an interface to a uint64 type.

func ToUint64E added in v1.0.1

func ToUint64E(i interface{}) (uint64, error)

ToUint64E casts an interface to a uint64 type.

func ToUint8 added in v1.0.1

func ToUint8(i interface{}) uint8

ToUint8 casts an interface to a uint8 type.

func ToUint8E added in v1.0.1

func ToUint8E(i interface{}) (uint8, error)

ToUint8E casts an interface to a uint type.

func ToUintE added in v1.0.1

func ToUintE(i interface{}) (uint, error)

ToUintE casts an interface to a uint type.

func Valid

func Valid(json string) bool

Valid returns true if the input is valid json.

if !gjson.Valid(json) {
	return errors.New("invalid json")
}
value := gjson.Get(json, "name.last")

func ValidBytes

func ValidBytes(json []byte) bool

ValidBytes returns true if the input is valid json.

if !gjson.Valid(json) {
	return errors.New("invalid json")
}
value := gjson.Get(json, "name.last")

If working with bytes, this method preferred over ValidBytes(string(data))

Types

type Any

type Any struct {
	// contains filtered or unexported fields
}

func NewAny added in v1.0.2

func NewAny(val interface{}) Any

func (Any) ToBool

func (a Any) ToBool() bool

func (Any) ToBoolSlice

func (a Any) ToBoolSlice() []bool

func (Any) ToDuration

func (a Any) ToDuration() time.Duration

func (Any) ToDurationSlice

func (a Any) ToDurationSlice() []time.Duration

func (Any) ToFloat32

func (a Any) ToFloat32() float32

func (Any) ToFloat64

func (a Any) ToFloat64() float64

func (Any) ToInt

func (a Any) ToInt() int

func (Any) ToInt16

func (a Any) ToInt16() int16

func (Any) ToInt32

func (a Any) ToInt32() int32

func (Any) ToInt64

func (a Any) ToInt64() int64

func (Any) ToInt8

func (a Any) ToInt8() int8

func (Any) ToIntSlice

func (a Any) ToIntSlice() []int

func (Any) ToSlice

func (a Any) ToSlice() []interface{}

func (Any) ToString

func (a Any) ToString() string

func (Any) ToStringMap

func (a Any) ToStringMap() map[string]interface{}

func (Any) ToStringMapBool

func (a Any) ToStringMapBool() map[string]bool

func (Any) ToStringMapInt

func (a Any) ToStringMapInt() map[string]int

func (Any) ToStringMapInt64

func (a Any) ToStringMapInt64() map[string]int64

func (Any) ToStringMapString

func (a Any) ToStringMapString() map[string]string

func (Any) ToStringMapStringSlice

func (a Any) ToStringMapStringSlice() map[string][]string

func (Any) ToStringSlice

func (a Any) ToStringSlice() []string

func (Any) ToTime

func (a Any) ToTime() time.Time

func (Any) ToUint

func (a Any) ToUint() uint

func (Any) ToUint16

func (a Any) ToUint16() uint16

func (Any) ToUint32

func (a Any) ToUint32() uint32

func (Any) ToUint64

func (a Any) ToUint64() uint64

func (Any) ToUint8

func (a Any) ToUint8() uint8

type Result

type Result struct {
	Any
	// Type is the json type
	Type Type
	// Raw is the raw json
	Raw string
	// Str is the json string
	Str string
	// Num is the json number
	Num float64
	// Index of raw value in original json, zero means index unknown
	Index int
}

Result represents a json value that is returned from Get().

func Get

func Get(json, path string) Result

Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". When the value is found it's returned immediately.

A path is a series of keys separated by a dot. A key may contain special wildcard characters '*' and '?'. To access an array value use the index as the key. To get the number of elements in an array or to access a child path, use the '#' character. The dot and wildcard character can be escaped with '\'.

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "friends": [
    {"first": "James", "last": "Murphy"},
    {"first": "Roger", "last": "Craig"}
  ]
}
"name.last"          >> "Anderson"
"age"                >> 37
"children"           >> ["Sara","Alex","Jack"]
"children.#"         >> 3
"children.1"         >> "Alex"
"child*.2"           >> "Jack"
"c?ildren.0"         >> "Sara"
"friends.#.first"    >> ["James","Roger"]

This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first.

func GetBytes

func GetBytes(json []byte, path string) Result

GetBytes searches json for the specified path. If working with bytes, this method preferred over Get(string(data), path)

func GetMany

func GetMany(json string, path ...string) []Result

GetMany searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths.

func GetManyBytes

func GetManyBytes(json []byte, path ...string) []Result

GetManyBytes searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths.

func Parse

func Parse(json string) Result

Parse parses the json and returns a result.

This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first.

func ParseBytes

func ParseBytes(json []byte) Result

ParseBytes parses the json and returns a result. If working with bytes, this method preferred over Parse(string(data))

func (Result) Array

func (t Result) Array() []Result

Array returns back an array of values. If the result represents a non-existent value, then an empty array will be returned. If the result is not a JSON array, the return value will be an array containing one result.

func (Result) Bool

func (t Result) Bool() bool

Bool returns an boolean representation.

func (Result) Exists

func (t Result) Exists() bool

Exists returns true if value exists.

 if gjson.Get(json, "name.last").Exists(){
		println("value exists")
 }

func (Result) Float

func (t Result) Float() float64

Float returns an float64 representation.

func (Result) ForEach

func (t Result) ForEach(iterator func(key, value Result) bool)

ForEach iterates through values. If the result represents a non-existent value, then no values will be iterated. If the result is an Object, the iterator will pass the key and value of each item. If the result is an Array, the iterator will only pass the value of each item. If the result is not a JSON array or object, the iterator will pass back one value equal to the result.

func (Result) Get

func (t Result) Get(path string) Result

Get searches result for the specified path. The result should be a JSON array or object.

func (Result) GetAny

func (t Result) GetAny(path string) Result

func (Result) Int

func (t Result) Int() int64

Int returns an integer representation.

func (Result) IsArray

func (t Result) IsArray() bool

IsArray returns true if the result value is a JSON array.

func (Result) IsObject

func (t Result) IsObject() bool

IsObject returns true if the result value is a JSON object.

func (Result) Less

func (t Result) Less(token Result, caseSensitive bool) bool

Less return true if a token is less than another token. The caseSensitive paramater is used when the tokens are Strings. The order when comparing two different type is:

Null < False < Number < String < True < JSON

func (Result) Map

func (t Result) Map() map[string]Result

Map returns back an map of values. The result should be a JSON array.

func (Result) String

func (t Result) String() string

String returns a string representation of the value.

func (Result) Time

func (t Result) Time() time.Time

Time returns a time.Time representation.

func (Result) Uint

func (t Result) Uint() uint64

Uint returns an unsigned integer representation.

func (Result) Value

func (t Result) Value() interface{}

Value returns one of these types:

bool, for JSON booleans
float64, for JSON numbers
Number, for JSON numbers
string, for JSON string literals
nil, for JSON null
map[string]interface{}, for JSON objects
[]interface{}, for JSON arrays

type Type

type Type int

Type is Result type

const (
	// Null is a null json value
	Null Type = iota
	// False is a json false boolean
	False
	// Number is json number
	Number
	// String is a json string
	String
	// True is a json true boolean
	True
	// JSON is a raw block of JSON
	JSON
)

func (Type) String

func (t Type) String() string

String returns a string representation of the type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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