jason

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: MIT Imports: 1 Imported by: 2

README

Dynamic JSON in Go

Go Reference Go Report

Documentation

Overview

Package jason helps deal with dynamic JSON.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Is

func Is[T any](j Value) bool

Is checks if j can unmarshal into type T.

Example of testing if j can be converted into a time instant:

if jason.Is[time.Time](j) { ... }

func Maybe

func Maybe[T any](j Value) (v T, err error)

Maybe unmarshals j into a value of type T.

Example of converting j into a time instant:

if v, err := jason.Maybe[time.Time](j); err == nil { ... }

func To

func To[T any](j Value) (v T)

To unmarshals j into a value of type T, panics on error.

Example of converting j into a time instant:

jason.To[time.Time](j)

Types

type Array

type Array[T any] []T

Array is a JSON array: an ordered collection of values.

Example of an Array literal:

jason.Array[int]{1, 2, 3}

type Number

type Number = json.Number

Number is an encoded JSON number.

It has arbitrary precision, and is easily converted to a float64 or int64.

Example of a Number literal:

jason.Number("10")

type Object

type Object[T any] map[string]T

Object is a JSON object: an unordered set of name/value pairs.

Example of an Object literal:

jason.Object[bool]{"abc": true}

type Value

type Value []byte

Value is an encoded JSON value.

Like json.RawMessage, it implements json.Marshaler and json.Unmarshaler and can be used to delay/precompute JSON decoding/encoding.

Example of a Value literal:

jason.Value("false")

func From

func From(v any) Value

From marshals v into a Value, panics on error.

Example of creating a Value from a time instant:

jason.From(time.Now())

func (Value) GoString

func (j Value) GoString() string

GoString returns a Go representation of j.

func (Value) MarshalJSON

func (j Value) MarshalJSON() ([]byte, error)

MarshalJSON returns j as the JSON encoding of j.

func (Value) String

func (j Value) String() string

String returns j as a string.

func (*Value) UnmarshalJSON

func (j *Value) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *j to a copy of data.

Jump to

Keyboard shortcuts

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