js

package
v0.24.5 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package js provides some simple json utilities: MapSlice, for preserving map key order when unmarshaling, and Builder for writing json-like data without marshaling.

Index

Constants

View Source
const Colon = ':'
View Source
const Comma = ','
View Source
const False = "false"
View Source
const Null = "null"
View Source
const Percent = '%'
View Source
const Quote = '"'
View Source
const Score = '_'
View Source
const Space = ' '
View Source
const True = "true"

Variables

View Source
var Array = [2]rune{'[', ']'}
View Source
var Obj = [2]rune{'{', '}'}
View Source
var Quotes = [2]rune{Quote, Quote}

Functions

func Embrace

func Embrace(style [2]rune, cb func(*Builder)) string

Types

type Builder

type Builder struct{ strings.Builder }

wraps strings builder with some json helper functions. Useful for cases where writing json directly is easier than first composing go data structures and then marshaling via package json/encoding.

func (*Builder) B

func (out *Builder) B(b bool) *Builder

write a bool as a string

func (*Builder) Brace

func (out *Builder) Brace(style [2]rune, cb func(*Builder)) *Builder

func (*Builder) F

func (out *Builder) F(f float64) *Builder

write a float; not particularly json compliant

func (*Builder) If

func (out *Builder) If(b bool, cb func(*Builder)) *Builder

func (*Builder) Kv

func (out *Builder) Kv(k, v string) *Builder

func (*Builder) N

func (out *Builder) N(n int) *Builder

write an integer

func (*Builder) Q

func (out *Builder) Q(el string) *Builder

write a string surrounding it with quotes

func (*Builder) R

func (out *Builder) R(els ...rune) *Builder

write one ore more runes as-is, with no special character encoding checks.

func (*Builder) Raw

func (out *Builder) Raw(el string) *Builder

output the passed string exactly as is, when no special checks for special characters is necessary

func (*Builder) Str

func (out *Builder) Str(s string) *Builder

write the inner bits of a json friendly string ( no surrounding quotes ) see: https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/encoding/json/encode.go;l=1036

type MapItem

type MapItem struct {
	Key string
	Msg json.RawMessage
}

type MapSlice

type MapSlice []MapItem

used when reading json to preserve the specified order of keys "no way to preserve the order of map keys" https://github.com/golang/go/issues/27179

func (MapSlice) Find

func (om MapSlice) Find(k string) (ret *MapItem, okay bool)

returns a valid pointer into the slice, or nil if not found

func (MapSlice) FindIndex

func (om MapSlice) FindIndex(k string) (ret int)

returns the index of the item or -1 if not found

func (*MapSlice) UnmarshalJSON

func (om *MapSlice) UnmarshalJSON(data []byte) (err error)

expects we're unmarshaling a valid json object.

Jump to

Keyboard shortcuts

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