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 ¶
- type JSON
- func BuildArrayFromSlice[T any](slice []T, transform func(T) map[string]interface{}) JSON
- func BuildFromTemplate(template map[string]interface{}) JSON
- func BuildInventoryPayLoadBuilder(ipAddress string, discoveryResponse interface{}) *JSON
- func BuildInventoryPayLoadBulk(ipAddress string, discoveryResponse interface{}) *JSON
- func BuildInventoryPayLoadGeneric(ipAddress string, discoveryResponse interface{}) *JSON
- func BuildInventoryPayLoadOld(ipAddress string, discoveryResponse interface{}) *JSON
- func JSONFromArray[T comparable](array []T) JSONdeprecated
- func JSONFromBytes(b []byte) (JSON, bool)
- func JSONFromString(s string) (JSON, bool)
- func NewJSON(value interface{}) JSON
- func NewJSONArray() JSON
- func NewJSONBytes(value []byte) JSON
- func NewJSONNull() JSON
- func NewJSONObject() JSON
- func NewJSONObjectFromMap(data map[string]interface{}) JSON
- func NewJSONObjectWithKeyValue(key string, value JSON) JSON
- func (j *JSON) AddToArray(jElem JSON)
- func (j JSON) ArrayElement(num int) JSON
- func (j JSON) ArraySize() int
- func (j JSON) AsArray() ([]interface{}, bool)
- func (j JSON) AsArrayString() ([]string, bool)
- func (j JSON) AsBool() (bool, bool)
- func (j JSON) AsBoolDefault(defaultValue bool) bool
- func (j JSON) AsBytes() ([]byte, bool)
- func (j JSON) AsInt64() (int64, bool)
- func (j JSON) AsInt64Default(defaultValue int64) int64
- func (j JSON) AsNumeric() (float64, bool)
- func (j JSON) AsNumericDefault(defaultValue float64) float64
- func (j JSON) AsObject() (map[string]interface{}, bool)
- func (j JSON) AsString() (string, bool)
- func (j JSON) AsStringDefault(defaultValue string) string
- func (j JSON) Clone() JSON
- func (j *JSON) DeepMerge(v JSON)
- func (j1 JSON) Equals(j2 JSON) bool
- func (j JSON) GetByPath(p string, delimiter ...string) JSON
- func (j JSON) GetByPathPtr(p string) *JSON
- func (j JSON) GetPtr() *JSON
- func (j JSON) IsArray() bool
- func (j JSON) IsBool() bool
- func (j JSON) IsNonEmptyArray() bool
- func (j JSON) IsNonEmptyObject() bool
- func (j JSON) IsNull() bool
- func (j JSON) IsNumeric() bool
- func (j JSON) IsObject() bool
- func (j JSON) IsString() bool
- func (j JSON) KeysCount() int
- func (j *JSON) Normalize()
- func (j JSON) NormalizedClone() JSON
- func (j JSON) ObjectKeys() []string
- func (j JSON) PathExists(p string, delimiter ...string) bool
- func (j *JSON) RemoveByPath(p string, delimiter ...string) bool
- func (j1 JSON) SemanticallyEquals(j2 JSON) bool
- func (j *JSON) SetByPath(p string, v JSON, delimiter ...string) bool
- func (j *JSON) SetByPathCustomDelimiter(p string, v JSON, delimiter string) bool
- func (j *JSON) SetByPaths(pathValues map[string]interface{})
- func (j JSON) ToBytes() []byte
- func (j JSON) ToString() string
- type JSONBuilder
- type Link
- type Schema
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
BuildArrayFromSlice creates JSON array from Go slice with transformation
func BuildFromTemplate ¶ added in v0.1.4
BuildFromTemplate builds JSON from template with value substitution
func BuildInventoryPayLoadBuilder ¶ added in v0.1.4
NEW WAY 2: Using builder pattern
func BuildInventoryPayLoadBulk ¶ added in v0.1.4
NEW WAY 1: Using bulk operations
func BuildInventoryPayLoadGeneric ¶ added in v0.1.4
NEW WAY 3: Using generic array builder
func BuildInventoryPayLoadOld ¶ added in v0.1.4
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 ¶
JSONFromBytes creates a JSON instance from byte slice by parsing it as JSON.
func JSONFromString ¶
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 NewJSONBytes ¶
NewJSONBytes creates a new JSON instance from byte slice, encoding it as hex string.
func NewJSONObjectFromMap ¶ added in v0.1.4
NewJSONObjectFromMap creates JSON object directly from map
func NewJSONObjectWithKeyValue ¶
NewJSONObjectWithKeyValue creates a new JSON object with a single key-value pair.
func (*JSON) AddToArray ¶
AddToArray adds an element to the JSON array.
func (JSON) ArrayElement ¶
ArrayElement returns the element at the specified index in the JSON array.
func (JSON) AsArrayString ¶
AsArrayString returns the JSON array as a string slice if all elements are strings.
func (JSON) AsBoolDefault ¶
AsBoolDefault returns the JSON value as boolean or defaultValue if not boolean.
func (JSON) AsInt64 ¶ added in v0.1.11
AsInt64 returns the JSON value as an int64 without precision loss through float64.
func (JSON) AsInt64Default ¶ added in v0.1.11
AsInt64Default returns the JSON value as int64 or defaultValue if not numeric.
func (JSON) AsNumericDefault ¶
AsNumericDefault returns the JSON value as float64 or defaultValue if not numeric.
func (JSON) AsStringDefault ¶
AsStringDefault returns the JSON value as string or defaultValue if not string.
func (JSON) GetByPathPtr ¶
GetByPathPtr returns a pointer to the JSON value at the specified path.
func (JSON) IsNonEmptyArray ¶
IsNonEmptyArray checks if the JSON value is a non-empty array.
func (JSON) IsNonEmptyObject ¶
IsNonEmptyObject checks if the JSON value is a non-empty 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
(optional) Returns a clone in canonical form.
func (JSON) ObjectKeys ¶
ObjectKeys returns all keys of the JSON object.
func (*JSON) RemoveByPath ¶
RemoveByPath removes a value at the specified path.
func (JSON) SemanticallyEquals ¶ added in v0.1.14
SemanticallyEquals compares two JSON values by their JSON meaning. Object key order is ignored, array order is significant, and equivalent number representations such as 1, 1.0, and 1e0 compare equal without converting integers to float64.
Typed nil maps and slices are treated as JSON null even though IsObject and IsArray report their Go container type; nil pointers are also treated as null. Values that cannot be represented in JSON (NaN, infinities, invalid json.Number values, or cyclic pointer chains) compare unequal to every value, including themselves. Cycles through maps and slices are detected to guarantee termination, but cyclic containers remain outside the JSON model.
func (*JSON) SetByPath ¶
SetByPath sets a value at the specified path in the JSON structure. Creates intermediate objects/arrays as needed.
func (*JSON) SetByPathCustomDelimiter ¶
SetByPathCustomDelimiter sets a value using a custom path delimiter.
func (*JSON) SetByPaths ¶ added in v0.1.4
SetByPaths sets multiple paths at once for better DX
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