encode

module
v0.0.0-...-2d288aa Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2020 License: BSD-3-Clause

README

Context-aware JSON encoder/decoder

This library is an experiment to add a context to the standard json encoding/decoding library.

How to use

Encoding

Your struct should implement the following interface:

// ContextAwareMarshaler is the interface implemented by types that
// can marshal themselves into valid JSON. In addition, it provides
// an arbitrary context field that can be passed from MarshalWithContext.
type ContextAwareMarshaler interface {
	MarshalJSONWithContext(ctx interface{}) ([]byte, error)
}

To utilize the context, you should call MarshalWithContext instead of Marshal:

encoded, err := MarshalWithContext(obj, "your context data")
Decoding

Your struct should implement the following interface:

// ContextAwareUnmarshaler is the interface implemented by types
// that can unmarshal a JSON description of themselves.
// The input can be assumed to be a valid encoding of
// a JSON value. UnmarshalJSONWithContext must copy the JSON data
// if it wishes to retain the data after returning. In addition,
// it can use an extra argument with the arbitrary context data
// that can be passed via UnmarshalWithContext.
//
// By convention, to approximate the behavior of Unmarshal itself,
// Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
type ContextAwareUnmarshaler interface {
	UnmarshalJSONWithContext([]byte, interface{}) error
}

To utilize the context, you should call UnmarshalWithContext instead of Unmarshal:

err := UnmarshalWithContext(buf, &obj, "your context data")

Directories

Path Synopsis
Package json implements encoding and decoding of JSON as defined in RFC 7159.
Package json implements encoding and decoding of JSON as defined in RFC 7159.

Jump to

Keyboard shortcuts

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