j2n

package
v0.0.0-...-f1100e8 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package j2n allows arbitrary JSON to be marshaled into structs. Any JSON fields that are not marshaled directly into the fields of the struct are put into a field with type UnknownFields

map[string]*json_helpers.RawMessage

This means that fields that are not explicitly named in the struct will survive an Unmarshal/Marshal round trip.

To avoid recursive calls to MarshalJSON/UnmarshalJSON, use the following pattern:

type CatData struct {
	Name string        `json_helpers:"name"`
	Rest UnknownFields `json_helpers:"-"`
}

type Cat struct {
	CatData
}

func (c *Cat) UnmarshalJSON(data []byte) error {
	return j2n.UnmarshalJSON(data, &c.CatData)
}

func (c Cat) MarshalJSON() ([]byte, error) {
	return j2n.MarshalJSON(c.CatData)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSON

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

MarshalJSON returns the JSON encoding of v, which must be a struct.

This behaves exactly like json.Marshal, but ensures that any extra fields mentioned in v.Overflow are output alongside the explicitly named struct fields.

It expects v to contain a field named 'Overflow' of type

map[string]*json.RawMessage

func UnmarshalJSON

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

UnmarshalJSON parses the JSON-encoded data into the struct pointed to by v.

This behaves exactly like json.Unmarshal, but any extra JSON fields that are not explicitly named in the struct are unmarshalled in the 'Overflow' field.

The struct v must contain a field 'Overflow' of type

map[string]*json.RawMessage

Types

type UnknownFields

type UnknownFields map[string]*json.RawMessage

Jump to

Keyboard shortcuts

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