safejson

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause Imports: 6 Imported by: 61

Documentation

Overview

Package safejson provides functions that allows JSON to be marshaled and unmarshaled in a safe and consistent manner. This package exists to standardize and normalize some of the default behavior of the json package that is unintuitive.

Marshal:

The default encoder returned by json.NewEncoder has "SetEscapeHTML" set to "true", which makes sense for HTML environments, but results in output that is hard to read in non-HTML environments. The default behavior of json.Marshal also appends a newline to the end of the generated JSON which, although this is technically legal from a JSON perspective, is often unexpected.

Unmarshal:

The default decoder returned by json.NewDecoder does not have the "UseNumber" behavior enabled. This means that all numeric values are unmarshaled as a float64. This behavior is generally less flexible, so safejson sets "UseNumber" to "true", which ensures that all numbers are unmarshaled as a json.Number.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decoder

func Decoder(r io.Reader) *json.Decoder

Decoder returns a new *json.Decoder with UseNumber enabled.

func Encoder

func Encoder(w io.Writer) *json.Encoder

Encoder returns a new *json.Encoder with SetEscapeHTML(false).

func FromYAMLValue

func FromYAMLValue(y interface{}) (interface{}, error)

FromYAMLValue returns a version of the provided input where all nested map[interface{}]interface{} values are converted to map[string]interface{}. The input should be the representation of an object in map[interface{}]interface{} form (as opposed to a string or []byte of the YAML itself). Returns an error if the conversion fails because any of the object keys are not strings.

Assumes that the input consists of only maps, slices, arrays, primitives and pointers to these types. Structs are assumed to have been converted into a map representation -- if a struct value is encountered, it will be treated as a primitive and left as-is (in particular, any maps in the struct will not be converted).

Many YAML libraries unmarshal YAML content as map[interface{}]interface{}, but the Go JSON library requires JSON maps to be represented as map[string]interface{}, so this function is helpful in converting the former to the latter.

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the JSON encoding of v encoded using the "safe" encoder. Unlike json.Marshal, the returned JSON bytes will not have a trailing newline.

func MarshalIndent

func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

MarshalIndent is like Marshal but applies Indent to format the output.

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal unmarshals the provided bytes (which should be valid JSON) into "v" using safejson.Decoder.

Types

This section is empty.

Jump to

Keyboard shortcuts

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