jsonjs

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 7 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 jsonjs reads and writes JSON as JavaScript's JSON.parse and JSON.stringify do, for files this toolkit shares with the TypeScript CLI, such as cdk.context.json.

JavaScript differs from encoding/json in three ways that show in a file:

  • An object's keys come in JavaScript's order: integer-like keys first, in ascending numeric order, then the rest in insertion order.
  • A duplicate key keeps its first position and takes its last value.
  • Numbers are written in JavaScript's form, so 1.0 is written 1 and 1e2 is written 100.

Strings are written as JSON.stringify writes them, except that a lone UTF-16 surrogate escape becomes U+FFFD, where JavaScript keeps the escape.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(v []byte) ([]byte, error)

Format returns v as JSON.stringify(JSON.parse(v), undefined, 2) writes it, with no final newline.

func Merge

func Merge(dst, src jsontext.Value) (jsontext.Value, error)

Merge returns src merged into dst, as upstream's deepMerge merges a later settings layer into an earlier one. When both are objects, their members merge recursively; otherwise src replaces dst.

func Order

func Order(keys []string) []string

Order returns keys, given in insertion order, in the order JavaScript gives an object's keys: integer-like keys first, in ascending order, then the others in insertion order.

func Stringify

func Stringify(v jsontext.Value, indent string) ([]byte, error)

Stringify returns v, compact JSON whose objects already have their members in JavaScript's order, as JSON.stringify(JSON.parse(v), undefined, indent) writes it, with no final newline. An empty indent writes it compactly, as JSON.stringify(JSON.parse(v)) does.

func Truthy

func Truthy(v jsontext.Value) bool

Truthy reports whether v is truthy as JavaScript judges a value: anything but false, null, 0 and the empty string.

Types

type Object

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

Object is a JSON object, with its members in JavaScript's order. The zero value is an empty object.

func ParseObject

func ParseObject(b []byte) (*Object, error)

ParseObject parses b, which must hold a JSON object, as JSON.parse would.

func (*Object) Delete

func (o *Object) Delete(key string)

Delete removes the named member, if there is one.

func (*Object) Format

func (o *Object) Format(newline bool) ([]byte, error)

Format returns the object as JSON.stringify(o, undefined, 2) writes it, followed by a newline when newline is set.

func (*Object) Get

func (o *Object) Get(key string) (jsontext.Value, bool)

Get returns the value of the named member.

func (*Object) Keys

func (o *Object) Keys() []string

Keys returns the member names, in JavaScript's order.

func (*Object) Len

func (o *Object) Len() int

Len returns the number of members.

func (*Object) Set

func (o *Object) Set(key string, v jsontext.Value)

Set sets the named member: an existing one keeps its position, and a new one goes last among the members that are not integer-like.

Jump to

Keyboard shortcuts

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