gson

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 4 Imported by: 1

README

Package gson provides searching for json strings and setting json values.

See gson_test and Go docs for usage.

Documentation

Overview

Package gson provides searching for json strings and setting json values

It's just a simple wrapper for the gjson and sjson

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrettyPrint added in v0.2.3

func PrettyPrint(v any)

PrettyPrint print any struct or raw json string

Types

type Gson

type Gson struct {
	gjson.Result
}

Gson Inherits from gjson.Result

Note that when using the Get method, the returned result is gjson.Result instead of Gson

You may need to use the Gson.GetGson method

func New

func New() *Gson

New Create an empty json object

func NewArray

func NewArray() *Gson

NewArray Create an empty json array

func Parse

func Parse(json string) *Gson

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) *Gson

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

func (*Gson) Delete

func (gson *Gson) Delete(path string) (json *Gson, err error)

Delete deletes a value from json for the specified path.

func (*Gson) GetGson added in v0.1.1

func (gson *Gson) GetGson(path string) *Gson

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

func (*Gson) MarshalJSON

func (gson *Gson) MarshalJSON() ([]byte, error)

MarshalJSON Serialize gson to byte slice

func (*Gson) Print

func (gson *Gson) Print()

Print Pretty print for debug purpose

func (*Gson) Set

func (gson *Gson) Set(path string, value interface{}) (json *Gson, err error)

Set sets a json value for the specified path. A path is in dot syntax, such as "name.last" or "age". A value should be a simple JSON value and not a JSON string. 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. An error is returned if the path is not valid.

A path is a series of keys separated by a dot.

{
  "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.1"         >> "Alex"

func (*Gson) SetRaw

func (gson *Gson) SetRaw(path, value string) (json *Gson, err error)

SetRaw sets a raw json value for the specified path. This function works the same as Set except that the value is set as a raw block of json. This allows for setting premarshalled json objects.

func (*Gson) UnmarshalJSON

func (gson *Gson) UnmarshalJSON(data []byte) error

UnmarshalJSON Deserialize data to gson

Jump to

Keyboard shortcuts

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