dyjson

package module
v1.0.1-0...-2602f2d Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2021 License: Unlicense Imports: 2 Imported by: 0

README

DyJSON

License go.dev Travis CI Codecov.io Go Report Card

A small and simple package to help you handle dynamic and unknown JSON elegantly.

Why

DyJSON lets you handle dynamic JSON in a different way. It is perfect for cases where you have to deal differently based on a field type or structure, e.g. call a function recursivelly if it's an object or array until you find a string or number value (something similar to what I needed).

If, while dealing with your JSON, you know the specific value path, or just want to validate it, I suggest you to use Gabs.

Example

func main() {

    const json = `
        {
            "key": {
                "key": [
                    "test",
                    1,
                    true
                ]
            }
        }
    `

    handle(dyjson.ParseString(json))

    // The output is:
    //   found string: test
    //   found number: 1.00
}

func handle(v *dyjson.JSONValue) {
    switch {
    case v.IsArray():
        for _, av := range v.Array() {
            handle(av)
        }
    case v.IsObject():
        handle(v.Object()["key"])
    case v.IsString():
        fmt.Printf("found string: %s\n", v.String())
    case v.IsNumber():
        fmt.Printf("found number: %.2f\n", v.Number())
    }
}

License

This project code is in the public domain. See the LICENSE file.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be in the public domain, without any additional terms or conditions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONValue

type JSONValue struct {
	json.RawMessage
	// contains filtered or unexported fields
}

JSONValue represents a JSON value, independently of its data type.

func Parse

func Parse(value []byte) *JSONValue

Parse parses the value into a new JSONValue.

func ParseString

func ParseString(value string) *JSONValue

ParseString parses the value (as a string) into a new JSONValue.

func (*JSONValue) Array

func (v *JSONValue) Array() []*JSONValue

Array returns the value parsed as a JSON array (JSONValue array).

func (*JSONValue) Boolean

func (v *JSONValue) Boolean() bool

Boolean returns the value parsed as a boolean (bool).

func (*JSONValue) IsArray

func (v *JSONValue) IsArray() bool

IsArray checks if the value is a JSON array.

func (*JSONValue) IsBoolean

func (v *JSONValue) IsBoolean() bool

IsBoolean checks if the value is a boolean.

func (*JSONValue) IsNull

func (v *JSONValue) IsNull() bool

IsNull checks if the value is null.

func (*JSONValue) IsNumber

func (v *JSONValue) IsNumber() bool

IsNumber checks if the value is a number.

func (*JSONValue) IsObject

func (v *JSONValue) IsObject() bool

IsObject checks if the value is a JSON object.

func (*JSONValue) IsString

func (v *JSONValue) IsString() bool

IsString checks if the value is a string.

func (*JSONValue) Number

func (v *JSONValue) Number() float64

Number returns the value parsed as a number (float64).

func (*JSONValue) Object

func (v *JSONValue) Object() map[string]*JSONValue

Object returns the value parsed as a JSON object (JSONValue).

func (*JSONValue) Set

func (v *JSONValue) Set()

Set sets the value as the internal JSON value. Useful to update itself when any child's value changes.

func (*JSONValue) SetArray

func (v *JSONValue) SetArray(val []*JSONValue)

SetArray sets the value as a JSON array.

func (*JSONValue) SetBoolean

func (v *JSONValue) SetBoolean(val bool)

SetBoolean sets the value as a JSON boolean.

func (*JSONValue) SetNull

func (v *JSONValue) SetNull()

SetNull sets the value as a JSON null.

func (*JSONValue) SetNumber

func (v *JSONValue) SetNumber(val float64)

SetNumber sets the value as a JSON number.

func (*JSONValue) SetObject

func (v *JSONValue) SetObject(val map[string]*JSONValue)

SetObject sets the value as a JSON object.

func (*JSONValue) SetString

func (v *JSONValue) SetString(val string)

SetString sets the value as a JSON string.

func (*JSONValue) String

func (v *JSONValue) String() string

String returns the value parsed as a string.

Jump to

Keyboard shortcuts

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