json

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2025 License: MIT Imports: 7 Imported by: 3

README

github.com/faroedev/go-json

A JSON parser and encoder.

Example

Objects
package main

import (
    "fmt"
    "github.com/faroedev/go-json"
)

func main() {
    jsonObject, err := json.Parse(data)
    if err != nil {
        panic(err)
    }

    name, err := jsonObject.GetString("name")
    if err != nil {
        // Key doesn't exist or the value isn't a string.
        panic(err)
    }
    fmt.Println(name)

    jsonObject.SetString(name, "faroe")
    fmt.Println(jsonObject.String())
}
Arrays
package main

import (
    "fmt"
    "github.com/faroedev/go-json"
)

func main() {
    jsonArray, err := json.ParseArray(data)
    if err != nil {
        panic(err)
    }

    name, err := jsonArray.GetString(0)
    if err != nil {
        // Item doesn't exist or the value isn't a string.
        panic(err)
    }
    fmt.Println(name)

    jsonArray.SetString(0, "faroe")
    fmt.Println(jsonArray.String())
}
Builder
package main

import (
    "fmt"
    "github.com/faroedev/go-json"
)

func main() {
    jsonObjectBuilder := json.NewObjectBuilder()
    jsonObjectBuilder.AddString("name", "faroe")
    s := jsonObjectBuilder.Done()
    fmt.Println(s)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayBuilderStruct

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

Use NewArrayBuilder().

func NewArrayBuilder

func NewArrayBuilder() *ArrayBuilderStruct

func (*ArrayBuilderStruct) AddBool

func (arrayBuilder *ArrayBuilderStruct) AddBool(value bool)

Encodes the value to a JSON boolean and adds it as a new array element.

func (*ArrayBuilderStruct) AddInt

func (arrayBuilder *ArrayBuilderStruct) AddInt(value int)

Encodes the value to a JSON number and adds it as a new array element.

func (*ArrayBuilderStruct) AddInt32

func (arrayBuilder *ArrayBuilderStruct) AddInt32(key string, value int32)

Encodes the value to a JSON number and adds it as a new array element.

func (*ArrayBuilderStruct) AddInt64

func (arrayBuilder *ArrayBuilderStruct) AddInt64(value int64)

Encodes the value to a JSON number and adds it as a new array element.

func (*ArrayBuilderStruct) AddJSON

func (arrayBuilder *ArrayBuilderStruct) AddJSON(value string)

Adds the JSON value as a new array element. The value is assumed to be valid JSON.

func (*ArrayBuilderStruct) AddNull

func (arrayBuilder *ArrayBuilderStruct) AddNull()

Adds null to the array.

func (*ArrayBuilderStruct) AddString

func (arrayBuilder *ArrayBuilderStruct) AddString(value string)

Encodes the value to a JSON string and adds it as a new array element. Control characters not allowed in JSON strings are ignored when encoding.

func (*ArrayBuilderStruct) Done

func (arrayBuilder *ArrayBuilderStruct) Done() string

Returns the built JSON. The builder can no longer be used.

type ArrayStruct

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

Represents a JSON array. The zero value is a ready-to-use empty array.

func ParseArray

func ParseArray(s string) (ArrayStruct, error)

Parses a JSON array. Ignores any leading and trailing whitespace. Returns an error if the string is an invalid JSON array or an object has duplicate member names.

JSON object member names are compared after resolving any escaped characters.

func (*ArrayStruct) AddBool

func (array *ArrayStruct) AddBool(value bool)

Appends a JSON boolean value at the end of the array.

func (*ArrayStruct) AddInt

func (array *ArrayStruct) AddInt(value int)

Appends a JSON number value at the end of the array.

func (*ArrayStruct) AddInt32

func (array *ArrayStruct) AddInt32(value int32)

Appends a JSON number value at the end of the array.

func (*ArrayStruct) AddInt64

func (array *ArrayStruct) AddInt64(value int64)

Appends a JSON number value at the end of the array.

func (*ArrayStruct) AddJSONArray

func (array *ArrayStruct) AddJSONArray(value ArrayStruct)

Appends a JSON array value at the end of the array.

func (*ArrayStruct) AddJSONObject

func (array *ArrayStruct) AddJSONObject(value ObjectStruct)

Appends a JSON object value at the end of the array.

func (*ArrayStruct) AddNull

func (array *ArrayStruct) AddNull()

Appends a JSON null value at the end of the array.

func (*ArrayStruct) AddNumber

func (array *ArrayStruct) AddNumber(value string)

Appends a JSON number value at the end of the array.

func (*ArrayStruct) AddString

func (array *ArrayStruct) AddString(value string)

Appends a JSON string value at the end of the array.

func (*ArrayStruct) ExistsAndIsNull

func (array *ArrayStruct) ExistsAndIsNull(index int) bool

Returns true if the value at the index is null.

func (*ArrayStruct) GetBool

func (array *ArrayStruct) GetBool(index int) (bool, error)

Returns an error if an item doesn't exist in the index or the value isn't a JSON boolean.

func (*ArrayStruct) GetInt

func (array *ArrayStruct) GetInt(key int) (int, error)

Returns an error if an item doesn't exist in the index, the value isn't a JSON number, or the JSON number cannot be represented as an int.

func (*ArrayStruct) GetInt32

func (array *ArrayStruct) GetInt32(key int) (int32, error)

Returns an error if an item doesn't exist in the index, the value isn't a JSON number, or the JSON number cannot be represented as an int32.

func (*ArrayStruct) GetInt64

func (array *ArrayStruct) GetInt64(key int) (int64, error)

Returns an error if an item doesn't exist in the index, the value isn't a JSON number, or the JSON number cannot be represented as an int64.

func (*ArrayStruct) GetJSONArray

func (array *ArrayStruct) GetJSONArray(index int) (ArrayStruct, error)

Returns an error if an item doesn't exist in the index or the value isn't a JSON array.

func (*ArrayStruct) GetJSONObject

func (array *ArrayStruct) GetJSONObject(index int) (ObjectStruct, error)

Returns an error if an item doesn't exist in the index or the value isn't a JSON object.

func (*ArrayStruct) GetNumber

func (array *ArrayStruct) GetNumber(key int) (string, error)

Returns an error if an item doesn't exist in the index or the value isn't a JSON number.

func (*ArrayStruct) GetString

func (array *ArrayStruct) GetString(index int) (string, error)

Returns an error if an item doesn't exist in the index or the value isn't a JSON string.

func (*ArrayStruct) IsNull

func (array *ArrayStruct) IsNull(index int) (bool, error)

Returns an error if an item doesn't exist in the index.

func (*ArrayStruct) Length

func (array *ArrayStruct) Length() int

func (*ArrayStruct) SetBool

func (array *ArrayStruct) SetBool(index int, value bool)

Sets a JSON boolean value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetInt

func (array *ArrayStruct) SetInt(index int, value int)

Sets a JSON number value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetInt32

func (array *ArrayStruct) SetInt32(index int, value int32)

Sets a JSON number value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetInt64

func (array *ArrayStruct) SetInt64(index int, value int64)

Sets a JSON number value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetJSONArray

func (array *ArrayStruct) SetJSONArray(index int, value ArrayStruct)

Sets a JSON array value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetJSONObject

func (array *ArrayStruct) SetJSONObject(index int, value ObjectStruct)

Sets a JSON object value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetNull

func (array *ArrayStruct) SetNull(index int)

Sets a JSON null value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetNumber

func (array *ArrayStruct) SetNumber(index int, value string)

Sets a JSON number value at index. Panics if the index is out of bounds.

func (*ArrayStruct) SetString

func (array *ArrayStruct) SetString(index int, value string)

Sets a JSON string value at index. Panics if the index is out of bounds.

func (*ArrayStruct) String

func (array *ArrayStruct) String() string

Encodes the array using ArrayBuilderStruct. Embedded objects are encoded with ObjectStruct.String(). Embedded arrays are encoded with ArrayStruct.String().

type ObjectBuilderStruct

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

Use NewObjectBuilder().

func NewObjectBuilder

func NewObjectBuilder() *ObjectBuilderStruct

func (*ObjectBuilderStruct) AddBool

func (objectBuilder *ObjectBuilderStruct) AddBool(name string, value bool)

Encodes the name to a JSON string and value to a JSON boolean, and adds a new object member. Succeeds even if a member with the same name already exists.

Control characters not allowed in JSON strings are ignored when encoding values to JSON strings.

func (*ObjectBuilderStruct) AddInt

func (objectBuilder *ObjectBuilderStruct) AddInt(name string, value int)

Encodes the name to a JSON string and value to a JSON number, and adds a new object member. Succeeds even if a member with the same name already exists.

Control characters not allowed in JSON strings are ignored when encoding values to JSON strings.

func (*ObjectBuilderStruct) AddInt32

func (objectBuilder *ObjectBuilderStruct) AddInt32(name string, value int32)

Encodes the name to a JSON string and value to a JSON number, and adds a new object member. Succeeds even if a member with the same name already exists.

Control characters not allowed in JSON strings are ignored when encoding values to JSON strings.

func (*ObjectBuilderStruct) AddInt64

func (objectBuilder *ObjectBuilderStruct) AddInt64(name string, value int64)

Encodes the name to a JSON string and value to a JSON number, and adds a new object member. Succeeds even if a member with the same name already exists.

Control characters not allowed in JSON strings are ignored when encoding values to JSON strings.

func (*ObjectBuilderStruct) AddJSON

func (objectBuilder *ObjectBuilderStruct) AddJSON(name string, value string)

Encodes the name to a JSON string and adds a new object member with the value untouched. The value is assumed to be valid JSON. Succeeds even if a member with the same name already exists.

Control characters not allowed in JSON strings are ignored when encoding values to JSON strings.

func (*ObjectBuilderStruct) AddNull

func (objectBuilder *ObjectBuilderStruct) AddNull(name string)

Encodes the name to a JSON string and adds a new object member with a null value. Succeeds even if a member with the same name already exists.

Control characters not allowed in JSON strings are ignored when encoding values to JSON strings.

func (*ObjectBuilderStruct) AddString

func (objectBuilder *ObjectBuilderStruct) AddString(name string, value string)

Encodes the name and value to JSON strings, and adds a new object member. Succeeds even if a member with the same name already exists.

Control characters not allowed in JSON strings are ignored when encoding values to JSON strings.

func (*ObjectBuilderStruct) Done

func (objectBuilder *ObjectBuilderStruct) Done() string

Returns the built JSON. The builder can no longer be used.

type ObjectStruct

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

Represents a JSON object. The zero value is a ready-to-use empty object.

func ParseObject

func ParseObject(s string) (ObjectStruct, error)

Parses a JSON object. Ignores any leading and trailing whitespace. Returns an error if the string is an invalid JSON object or an object has duplicate member names.

JSON object member names are compared after resolving any escaped characters.

func (*ObjectStruct) ExistsAndIsNull

func (object *ObjectStruct) ExistsAndIsNull(key string) bool

Returns true if the key exists and the value is null.

func (*ObjectStruct) GetBool

func (object *ObjectStruct) GetBool(key string) (bool, error)

Returns an error if the key doesn't exist or the value isn't a JSON boolean.

func (*ObjectStruct) GetInt

func (object *ObjectStruct) GetInt(key string) (int, error)

Returns an error if the key doesn't exist, the value isn't a JSON number, or the JSON number cannot be represented as an int.

func (*ObjectStruct) GetInt32

func (object *ObjectStruct) GetInt32(key string) (int32, error)

Returns an error if the key doesn't exist, the value isn't a JSON number, or the JSON number cannot be represented as an int32.

func (*ObjectStruct) GetInt64

func (object *ObjectStruct) GetInt64(key string) (int64, error)

Returns an error if the key doesn't exist, the value isn't a JSON number, or the JSON number cannot be represented as an int64.

func (*ObjectStruct) GetJSONArray

func (object *ObjectStruct) GetJSONArray(key string) (ArrayStruct, error)

Returns an error if the key doesn't exist or the value isn't a JSON array.

func (*ObjectStruct) GetJSONObject

func (object *ObjectStruct) GetJSONObject(key string) (ObjectStruct, error)

Returns an error if the key doesn't exist or the value isn't a JSON object.

func (*ObjectStruct) GetNumber

func (object *ObjectStruct) GetNumber(key string) (string, error)

Returns an error if the key doesn't exist or the value isn't a JSON number

func (*ObjectStruct) GetString

func (object *ObjectStruct) GetString(key string) (string, error)

Returns an error if the key doesn't exist or the value isn't a JSON string.

func (*ObjectStruct) Has

func (object *ObjectStruct) Has(key string) bool

func (*ObjectStruct) IsNull

func (object *ObjectStruct) IsNull(key string) (bool, error)

Returns an error if the key doesn't exist.

func (*ObjectStruct) SetBool

func (object *ObjectStruct) SetBool(key string, value bool)

Set a member with a JSON boolean value. Overrides any member with the same name.

func (*ObjectStruct) SetInt

func (object *ObjectStruct) SetInt(key string, value int)

Set a member with a JSON number value. Overrides any member with the same name.

func (*ObjectStruct) SetInt32

func (object *ObjectStruct) SetInt32(key string, value int32)

Set a member with a JSON number value. Overrides any member with the same name.

func (*ObjectStruct) SetInt64

func (object *ObjectStruct) SetInt64(key string, value int64)

Set a member with a JSON number value. Overrides any member with the same name.

func (*ObjectStruct) SetJSONArray

func (object *ObjectStruct) SetJSONArray(key string, value ArrayStruct)

Set a member with a JSON array value. Overrides any member with the same name.

func (*ObjectStruct) SetJSONObject

func (object *ObjectStruct) SetJSONObject(key string, value ObjectStruct)

Set a member with a JSON object value. Overrides any member with the same name.

func (*ObjectStruct) SetNull

func (object *ObjectStruct) SetNull(key string)

Set a member with a JSON null value. Overrides any member with the same name.

func (*ObjectStruct) SetNumber

func (object *ObjectStruct) SetNumber(key string, value string)

Set a member with a JSON number value. Overrides any member with the same name.

func (*ObjectStruct) SetString

func (object *ObjectStruct) SetString(key string, value string)

Set a member with a JSON string value. Overrides any member with the same name.

func (*ObjectStruct) String

func (object *ObjectStruct) String() string

Encodes the object using ObjectBuilderStruct. Embedded objects are encoded with ObjectStruct.String(). Embedded arrays are encoded with ArrayStruct.String().

Jump to

Keyboard shortcuts

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