jsontypes

package
v0.37.0-dev Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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