package jsoncode

import "golazy.dev/lazycode/jsoncode"

Package jsoncode provides structured JSON object edits as lazycode operations.

For example, an installer can plan a package dependency update:

workspace, err := lazycode.Load(".", "package.json")
if err != nil {
	return err
}

_, err = workspace.Plan(
	jsoncode.Dependency(
		"package.json",
		"dependencies",
		"@hotwired/turbo",
		"8.0.13",
	),
)
if err != nil {
	return err
}
if result.Changed() {
	fmt.Println("package.json would change")
}

JSON indentation and the final newline are preserved. Object key order is normalized through encoding/json.

Functions

func Set

Set returns an operation that assigns value at an object-key path in name.

func Set(name string, path []string, value any) lazycode.Operation

Types

type Document

Document is a parsed JSON object with its detected indentation and final newline style.

type Document struct {
	// contains filtered or unexported fields
}
func Parse

Parse parses exactly one JSON object from data.

func Parse(data []byte) (*Document, error)
func (d *Document) Bytes

Bytes encodes the document using its detected indentation and final newline.

func (d *Document) Bytes() ([]byte, error)
func (d *Document) EnsureDependency

EnsureDependency sets name to version in a supported package.json dependency group.

func (d *Document) EnsureDependency(group, name, version string) (bool, error)
func (d *Document) Remove

Remove deletes the value at an object-key path and reports whether it existed.

func (d *Document) Remove(path []string) (bool, error)
func (d *Document) RemoveDependency

RemoveDependency removes name from a supported package.json dependency group.

func (d *Document) RemoveDependency(group, name string) (bool, error)
func (d *Document) Set

Set assigns value at an object-key path, creating intermediate objects when needed, and reports whether the document changed.

func (d *Document) Set(path []string, value any) (bool, error)