easyjson

package module
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: Apache-2.0 Imports: 7 Imported by: 23

README

Easy JSON

A Go package for ease and simple operations with JSON data.

Lint

Overview

Easy JSON is a Go package that provides a convenient way to work with JSON data. It offers functionality for creating, manipulating, and comparing JSON objects and arrays.

Installation

To use Easy JSON in your Go project, you can simply import it using Go modules:

import "github.com/foliagecp/easyjson"

Usage

Creating a JSON Object
obj := easyjson.NewJSONObject()
Creating a JSON Array
arr := easyjson.NewJSONArray()
Setting Values by Path
obj.SetByPath("user.name", easyjson.NewJSON("John"))
Getting Values by Path
name := obj.GetByPath("user.name").ToString()
Deep Merging JSON Objects
obj1 := easyjson.NewJSONObject()
obj2 := easyjson.NewJSONObject()

obj1.DeepMerge(obj2)
Comparing JSON Objects
isEqual := obj1.Equals(obj2)

Documentation

For more details and usage examples, please refer to the official documentation.

License

Unless otherwise noted, the easyjson source files are distributed under the Apache Version 2.0 license found in the LICENSE file.

Contribution

Contributions and bug reports are welcome! Please submit issues or pull requests to help improve this package.

Documentation

Overview

Package easyjson provides everything that is needed for ease and simple operating with JSON data structure. It offers a fluent API for JSON manipulation, path-based access, and builder patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

type JSON struct {
	Value interface{}
}

JSON represents a JSON value that can be manipulated using path-based operations.

func BuildArrayFromSlice added in v0.1.4

func BuildArrayFromSlice[T any](slice []T, transform func(T) map[string]interface{}) JSON

BuildArrayFromSlice creates JSON array from Go slice with transformation

func BuildFromTemplate added in v0.1.4

func BuildFromTemplate(template map[string]interface{}) JSON

BuildFromTemplate builds JSON from template with value substitution

func BuildInventoryPayLoadBuilder added in v0.1.4

func BuildInventoryPayLoadBuilder(ipAddress string, discoveryResponse interface{}) *JSON

NEW WAY 2: Using builder pattern

func BuildInventoryPayLoadBulk added in v0.1.4

func BuildInventoryPayLoadBulk(ipAddress string, discoveryResponse interface{}) *JSON

NEW WAY 1: Using bulk operations

func BuildInventoryPayLoadGeneric added in v0.1.4

func BuildInventoryPayLoadGeneric(ipAddress string, discoveryResponse interface{}) *JSON

NEW WAY 3: Using generic array builder

func BuildInventoryPayLoadOld added in v0.1.4

func BuildInventoryPayLoadOld(ipAddress string, discoveryResponse interface{}) *JSON

OLD WAY (from screenshot):

func JSONFromArray deprecated

func JSONFromArray[T comparable](array []T) JSON

Deprecated: JSONFromArray is no longer needed — use NewJSON instead, it now handles slices properly. JSONFromArray creates a JSON array from any comparable slice.

func JSONFromBytes

func JSONFromBytes(b []byte) (JSON, bool)

JSONFromBytes creates a JSON instance from byte slice by parsing it as JSON.

func JSONFromString

func JSONFromString(s string) (JSON, bool)

JSONFromString creates a JSON instance from string by parsing it as JSON.

func NewJSON

func NewJSON(value interface{}) JSON

NewJSON creates a new JSON instance from any Go value.

func NewJSONArray

func NewJSONArray() JSON

NewJSONArray creates a new empty JSON array.

func NewJSONBytes

func NewJSONBytes(value []byte) JSON

NewJSONBytes creates a new JSON instance from byte slice, encoding it as hex string.

func NewJSONNull

func NewJSONNull() JSON

NewJSONNull creates a new JSON instance with null value.

func NewJSONObject

func NewJSONObject() JSON

NewJSONObject creates a new empty JSON object.

func NewJSONObjectFromMap added in v0.1.4

func NewJSONObjectFromMap(data map[string]interface{}) JSON

NewJSONObjectFromMap creates JSON object directly from map

func NewJSONObjectWithKeyValue

func NewJSONObjectWithKeyValue(key string, value JSON) JSON

NewJSONObjectWithKeyValue creates a new JSON object with a single key-value pair.

func (*JSON) AddToArray

func (j *JSON) AddToArray(jElem JSON)

AddToArray adds an element to the JSON array.

func (JSON) ArrayElement

func (j JSON) ArrayElement(num int) JSON

ArrayElement returns the element at the specified index in the JSON array.

func (JSON) ArraySize

func (j JSON) ArraySize() int

ArraySize returns the size of the JSON array, or -1 if not an array.

func (JSON) AsArray

func (j JSON) AsArray() ([]interface{}, bool)

AsArray returns the JSON value as a slice if it's an array.

func (JSON) AsArrayString

func (j JSON) AsArrayString() ([]string, bool)

AsArrayString returns the JSON array as a string slice if all elements are strings.

func (JSON) AsBool

func (j JSON) AsBool() (bool, bool)

AsBool returns the JSON value as a boolean if it's a boolean.

func (JSON) AsBoolDefault

func (j JSON) AsBoolDefault(defaultValue bool) bool

AsBoolDefault returns the JSON value as boolean or defaultValue if not boolean.

func (JSON) AsBytes

func (j JSON) AsBytes() ([]byte, bool)

AsBytes returns the JSON value as bytes if it's a hex-encoded string.

func (JSON) AsNumeric

func (j JSON) AsNumeric() (float64, bool)

AsNumeric returns the JSON value as a float64 if it's numeric.

func (JSON) AsNumericDefault

func (j JSON) AsNumericDefault(defaultValue float64) float64

AsNumericDefault returns the JSON value as float64 or defaultValue if not numeric.

func (JSON) AsObject

func (j JSON) AsObject() (map[string]interface{}, bool)

AsObject returns the JSON value as a map if it's an object.

func (JSON) AsString

func (j JSON) AsString() (string, bool)

AsString returns the JSON value as a string if it's a string.

func (JSON) AsStringDefault

func (j JSON) AsStringDefault(defaultValue string) string

AsStringDefault returns the JSON value as string or defaultValue if not string.

func (JSON) Clone

func (j JSON) Clone() JSON

Clone creates a deep copy of the JSON value.

func (*JSON) DeepMerge

func (j *JSON) DeepMerge(v JSON)

DeepMerge merges another JSON value into this one recursively.

func (JSON) Equals

func (j1 JSON) Equals(j2 JSON) bool

Equals compares two JSON values for deep equality.

func (JSON) GetByPath

func (j JSON) GetByPath(p string, delimiter ...string) JSON

func (JSON) GetByPathPtr

func (j JSON) GetByPathPtr(p string) *JSON

GetByPathPtr returns a pointer to the JSON value at the specified path.

func (JSON) GetPtr

func (j JSON) GetPtr() *JSON

GetPtr returns a pointer to this JSON instance.

func (JSON) IsArray

func (j JSON) IsArray() bool

IsArray checks if the JSON value is an array.

func (JSON) IsBool

func (j JSON) IsBool() bool

IsBool checks if the JSON value is a boolean.

func (JSON) IsNonEmptyArray

func (j JSON) IsNonEmptyArray() bool

IsNonEmptyArray checks if the JSON value is a non-empty array.

func (JSON) IsNonEmptyObject

func (j JSON) IsNonEmptyObject() bool

IsNonEmptyObject checks if the JSON value is a non-empty object.

func (JSON) IsNull

func (j JSON) IsNull() bool

IsNull checks if the JSON value is null.

func (JSON) IsNumeric

func (j JSON) IsNumeric() bool

IsNumeric checks if the JSON value is a number.

func (JSON) IsObject

func (j JSON) IsObject() bool

IsObject checks if the JSON value is an object.

func (JSON) IsString

func (j JSON) IsString() bool

IsString checks if the JSON value is a string.

func (JSON) KeysCount

func (j JSON) KeysCount() int

KeysCount returns the number of keys in the JSON object.

func (*JSON) Normalize added in v0.1.7

func (j *JSON) Normalize()

Normalize converts JSON to a canonical form: - all numbers -> float64 - arrays are sorted deterministically by the canonical string of elements - objects are normalized recursively (for the string canon, keys are sorted)

func (JSON) NormalizedClone added in v0.1.7

func (j JSON) NormalizedClone() JSON

(optional) Returns a clone in canonical form.

func (JSON) ObjectKeys

func (j JSON) ObjectKeys() []string

ObjectKeys returns all keys of the JSON object.

func (JSON) PathExists

func (j JSON) PathExists(p string, delimiter ...string) bool

func (*JSON) RemoveByPath

func (j *JSON) RemoveByPath(p string, delimiter ...string) bool

RemoveByPath removes a value at the specified path.

func (*JSON) SetByPath

func (j *JSON) SetByPath(p string, v JSON, delimiter ...string) bool

SetByPath sets a value at the specified path in the JSON structure. Creates intermediate objects/arrays as needed.

func (*JSON) SetByPathCustomDelimiter

func (j *JSON) SetByPathCustomDelimiter(p string, v JSON, delimiter string) bool

SetByPathCustomDelimiter sets a value using a custom path delimiter.

func (*JSON) SetByPaths added in v0.1.4

func (j *JSON) SetByPaths(pathValues map[string]interface{})

SetByPaths sets multiple paths at once for better DX

func (JSON) ToBytes

func (j JSON) ToBytes() []byte

ToBytes converts the JSON value to byte slice.

func (JSON) ToString

func (j JSON) ToString() string

ToString converts the JSON value to string representation.

type JSONBuilder added in v0.1.4

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

JSONBuilder provides fluent interface for JSON construction. It allows method chaining for building complex JSON structures efficiently.

func NewJSONBuilder added in v0.1.4

func NewJSONBuilder() *JSONBuilder

NewJSONBuilder creates a new JSON builder

func (*JSONBuilder) AddToArray added in v0.1.4

func (b *JSONBuilder) AddToArray(path string, value interface{}) *JSONBuilder

AddToArray adds value to array at path

func (*JSONBuilder) Build added in v0.1.4

func (b *JSONBuilder) Build() JSON

Build returns the final JSON object

func (*JSONBuilder) Set added in v0.1.4

func (b *JSONBuilder) Set(path string, value interface{}) *JSONBuilder

Set sets a path value and returns builder for chaining

func (*JSONBuilder) SetIfNotEmpty added in v0.1.4

func (b *JSONBuilder) SetIfNotEmpty(path string, value interface{}) *JSONBuilder

SetIfNotEmpty sets value only if it's not empty/zero

type Link struct {
	From string
	To   string
	Name string
}

type Schema added in v0.1.4

type Schema struct {
	Type       string
	Attributes interface{}
	Links      []Link
}

Mock types for example (these would be defined elsewhere)

func ConvertDiscoveryToInventorySchemas added in v0.1.4

func ConvertDiscoveryToInventorySchemas(discoveryResponse interface{}) []Schema

Jump to

Keyboard shortcuts

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