jsontypes

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package jsontypes supports decoding for interface types whose concrete implementations need to be stored as JSON. To do this, concrete values are packaged in wrapper objects having the form:

{
  "type": "<type-tag>",
  "value": <json-encoding-of-value>
}

This package provides a registry for type tag strings and functions to encode and decode wrapper objects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v Tagged) ([]byte, error)

Marshal marshals a JSON wrapper object containing v. If v == nil, Marshal returns the JSON "null" value without error.

func MustRegister

func MustRegister(v Tagged)

MustRegister adds v to the type registry. It will panic if the tag returned by v is already registered. This function is meant for use during program initialization.

func Unmarshal

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

Unmarshal unmarshals a JSON wrapper object into v. It reports an error if the data do not encode a valid wrapper object, if the wrapper's type tag is not registered with jsontypes, or if the resulting value is not compatible with the type of v.

Types

type Tagged

type Tagged interface {
	TypeTag() string
}

The Tagged interface must be implemented by a type in order to register it with the jsontypes package. The TypeTag method returns a string label that is used to distinguish objects of that type.

Jump to

Keyboard shortcuts

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