gson

package module
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2022 License: MIT Imports: 9 Imported by: 150

README

Overview

Go Reference

The tests is the doc.

A tiny JSON lib to read and alter a JSON value. The data structure is lazy, it's parse-on-read so that you can replace the parser with a faster one if performance is critical, use method JSON.Raw to do it.

Documentation

Overview

Package gson A tiny JSON lib to read and alter a JSON value.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool added in v0.7.1

func Bool(v bool) *bool

Bool returns the pointer of the v

func Int added in v0.7.1

func Int(v int) *int

Int returns the pointer of the v

func Num added in v0.7.1

func Num(v float64) *float64

Num returns the pointer of the v

func Path

func Path(path string) []interface{}

Path from string

func Str added in v0.7.1

func Str(v string) *string

Str returns the pointer of the v

Types

type JSON

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

JSON represent a JSON value

Example
package main

import (
	"fmt"

	"github.com/ysmood/gson"
)

func main() {
	obj := gson.NewFrom(`{"a": {"b": [1, 2]}}`)

	fmt.Println(obj.Get("a.b.0").Int())

	obj.Set("a.b.1", "ok").Set("c", 2)
	obj.Del("c")
	fmt.Println(">", obj.JSON("> ", "  "))

}
Output:

1
> {
>   "a": {
>     "b": [
>       1,
>       "ok"
>     ]
>   }
> }

func New

func New(v interface{}) JSON

New JSON from []byte, io.Reader, or raw value.

func NewFrom

func NewFrom(s string) JSON

NewFrom json encoded string

func (JSON) Arr

func (j JSON) Arr() []JSON

Arr of JSON returns empty array if underlying JSON is not an array.

func (JSON) Bool

func (j JSON) Bool() bool

Bool value returns zero value for type if underlying JSON type is not boolean

func (*JSON) Del added in v0.7.0

func (j *JSON) Del(path string) *JSON

Del deletes the element at the path.

func (*JSON) Dels added in v0.7.0

func (j *JSON) Dels(sections ...interface{}) bool

Dels deletes the element at the path sections. Return true if it's deleted.

func (JSON) Get

func (j JSON) Get(path string) JSON

Get by json path. It's a shortcut for Gets.

func (JSON) Gets

func (j JSON) Gets(sections ...interface{}) (JSON, bool)

Gets element by path sections. If a section is not string, int, or func, it will be ignored. If it's a func, the value will be passed to it, the result of it will the next level. The last return value will be false if not found.

func (JSON) Has

func (j JSON) Has(path string) bool

Has an element is found on the path

func (JSON) Int

func (j JSON) Int() int

Int value returns zero value for type if underlying JSON type is not convertible.

func (JSON) JSON

func (j JSON) JSON(prefix, indent string) string

JSON string

func (JSON) Join

func (j JSON) Join(sep string) string

Join elements

func (JSON) Map

func (j JSON) Map() map[string]JSON

Map of JSON returns empty map if underlying JSON object is not a map.

func (JSON) MarshalJSON

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

MarshalJSON interface

func (JSON) Nil

func (j JSON) Nil() bool

Nil or not

func (JSON) Num

func (j JSON) Num() float64

Num value returns zero value for type if underlying JSON type is not convertible.

func (JSON) Raw

func (j JSON) Raw() interface{}

Raw underlying value

func (*JSON) Set

func (j *JSON) Set(path string, val interface{}) *JSON

Set by json path. It's a shortcut for Sets.

func (*JSON) Sets

func (j *JSON) Sets(target interface{}, sections ...interface{}) *JSON

Sets element by path sections. If a section is not string or int, it will be ignored.

func (JSON) Str

func (j JSON) Str() string

Str value

func (JSON) String

func (j JSON) String() string

String implements fmt.Stringer interface

func (JSON) Unmarshal added in v0.7.3

func (j JSON) Unmarshal(v interface{}) error

Unmarshal is the same as json.Unmarshal for the underlying raw value. It should be called before other operations.

func (*JSON) UnmarshalJSON

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

UnmarshalJSON interface

func (JSON) Val

func (j JSON) Val() interface{}

Val of the underlying json value. The first time it's called, it will try to parse the underlying data.

type Query

type Query func(interface{}) (val interface{}, has bool)

Query section

Jump to

Keyboard shortcuts

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