std_json

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Doc = types.StdlibDoc{
	Name:        "json",
	Description: "Encode and decode JSON data as strings.",
}
View Source
var Index = map[string]Value{
	"F/decode": &Func{Executor: func(ctx *FuncContext) *Return {
		if len(ctx.Args) < 1 {
			ctx.Interp.Throw("json.decode(s): expected at least 1 argument")
			return &Return{}
		}

		s := ctx.Args[0]
		if str, ok := s.(*String); ok {
			var v any
			err := json.Unmarshal([]byte(str.Value), &v)
			if err != nil {
				ctx.Interp.Throw("json.decode(s): " + err.Error())
				return &Return{}
			}
			wrapped := Wrap(v)
			return NewReturn(&wrapped)
		} else {
			ctx.Interp.Throw("json.decode(s): expected string")
			return &Return{}
		}
	}},
	"F/encode": &Func{Executor: func(ctx *FuncContext) *Return {
		if len(ctx.Args) < 1 {
			ctx.Interp.Throw("json.encode(v): expected at least 1 argument")
			return &Return{}
		}

		v := ctx.Args[0]
		data, err := json.Marshal(v.Unwrap())
		if err != nil {
			ctx.Interp.Throw("json.encode(v): " + err.Error())
			return &Return{}
		}
		return NewReturn(NewString(string(data)))
	}},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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