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

lazyjson

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 9 Imported by: 0

README

lazyjson

CI Go Reference

A tiny JSON library to read and alter a JSON value. The data structure is lazy: it parses on read, so the parser can be swapped for a faster one where performance matters (see JSON.Raw).

Provenance

lazyjson is a Satellite module of wand. It is a snapshot of gson v0.7.3 (commit c60042b), imported without git history and maintained here by headlesslab; see NOTICE. The module path changed, the exported identifiers did not.

Replace github.com/ysmood/gson with github.com/headlesslab/lazyjson and gson. with lazyjson.; JSON, New, NewFrom, Int, ... keep their names. Error messages carry the lazyjson: prefix.

Usage

import "github.com/headlesslab/lazyjson"

The tests are the documentation: see lazyjson_test.go.

Versioning

v0.x: no breaking change without a major version bump. Releases are cut by hand when the module changes; wand pins each release exactly.

License

MIT, see LICENSE.

Documentation

Overview

Package lazyjson is a tiny JSON library to read and alter a JSON value.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v bool) *bool

Bool returns the pointer of the v

func Int

func Int(v int) *int

Int returns the pointer of the v

func Num

func Num(v float64) *float64

Num returns the pointer of the v

func Path

func Path(path string) []interface{}

Path from string

func Str

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/headlesslab/lazyjson"
)

func main() {
	obj := lazyjson.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

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

Del deletes the element at the path.

func (*JSON) Dels

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

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