jsonlayout

package
v0.146.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonlayout edits the top-level members of a JSON object in place, keeping the bytes and the layout (indentation, one line or many, the text between a name and its value) of everything it does not change. DataTug project files live in Git repositories: a write that changes one value must change only the lines of that value.

Index

Constants

View Source
const DefaultIndent = "\t"

DefaultIndent is the indentation of an object that has no member to take it from, as the project store writes JSON.

Variables

This section is empty.

Functions

func Equal

func Equal(a, b []byte) bool

Equal reports whether a and b, valid JSON, hold the same value: member order, whitespace, string escapes and the spelling of a number do not matter.

func IndentOf

func IndentOf(body []byte) string

IndentOf is the unit of indentation of body, valid JSON: the leading whitespace of its first indented line, or "" when body is on one line.

func Merge

func Merge(existing []byte, want map[string]json.RawMessage, order []string) ([]byte, bool)

Merge makes the object existing hold exactly the members of want, in place. A member whose value is Equal to the one wanted keeps its bytes; a changed one has only its value rewritten, in the object's layout; one that is not wanted is removed, and so is a second member of the same name. The members existing lacks are appended in the order of order (names not in want are skipped). It is false when existing is not an object.

func Splice

func Splice(body []byte, start, end int, with []byte) []byte

Splice replaces body[start:end] with with.

Types

type Layout

type Layout struct {
	Indent, Unit, Colon string
	// Newline is "\r\n" in a file that ends its lines so, else "\n".
	Newline string
}

Layout is how an object lays out its members: the whitespace before a member name ("" when the object is on one line), the unit of indentation of nested values, and the text between a name and its value.

func (Layout) Encode

func (l Layout) Encode(value []byte) []byte

Encode lays out value, valid JSON, as a member value of an object with this layout.

type Member

type Member struct {
	Name                 string
	KeyStart, KeyEnd     int // the quoted name
	ValueStart, ValueEnd int
}

Member is a top-level member of an Object.

type Object

type Object struct {
	Open, Close int // offsets of '{' and '}'
	Members     []Member
	Layout      Layout
}

Object is the top level of a JSON object as its bytes stand.

func Scan

func Scan(body []byte) (object Object, ok bool)

Scan locates the members of body, a valid JSON value. It is false when body is not an object.

func (Object) Append

func (o Object) Append(body []byte, name string, value []byte) []byte

Append adds the member name: value, valid JSON, after the last member, laid out as the others are.

func (Object) Remove

func (o Object) Remove(body []byte, i int) []byte

Remove takes the member at index i out of body with the separator that joins it to its neighbours: the one before it, or for the first member the one after it.

Jump to

Keyboard shortcuts

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