Help us shape the future of Go! Take the 2026 Go Developer Survey

gson

package module
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 9 Imported by: 305

README

Overview

Go Reference

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Path

func Path(path string) []interface{}

Path from string

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)
	fmt.Println(">", obj.JSON("> ", "  "))

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

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

func (JSON) Bool

func (j JSON) Bool() bool

Bool value

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

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

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

func (JSON) Raw

func (j JSON) Raw() interface{}

Raw underlaying 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) UnmarshalJSON

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

UnmarshalJSON interface

func (JSON) Val

func (j JSON) Val() interface{}

Val of the underlaying json value

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