Help us shape the future of Go! Take the 2026 Go Developer Survey

strictjson

package module
v0.0.0-...-2f3c0fe Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2016 License: MIT Imports: 4 Imported by: 0

README

strictjson

Validate JSON objects against Go structs.

Build Status GoDoc

strictjson.Check allows you to check if all of the fields in a struct are satisfied by the incoming JSON message.

Example:

type Foo struct {
	A int    `json:"a"`
	B string `json:"b"`
}

func (f *Foo) UnmarshalJSON(b []byte) error {
	type __ Foo
	var g __
	if err := Check(b, f); err != nil {
		return err
	}
	if err := json.Unmarshal(b, &g); err != nil {
		return err
	}
	*f = Foo(g)
	return nil
}

In the above example, if a user tries to supply a json message that doesn't contain keys a and b, strictjson.Check will return an error.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(b []byte, obj interface{}, omit ...string) error

Check checks that all of `obj`'s struct fields are provided by the JSON object encoded in `b`.

If there are any fields indicated by a `json` tag that are not present in the object encoded in `b`, an error will be returned indicating the missing fields.

If obj is not a struct, an error will be returned.

If a struct field has an empty `json` tag, or an omitempty tag, the absence of a corresponding key in b will not be considered an error.

The omit variadic argument can be used to omit fields from checking, so that only certain fields in a struct are checked.

Example:

type Foo struct {
	A int    `json:"a"`
	B string `json:"b"`
}

func (f *Foo) UnmarshalJSON(b []byte) error {
	type __ Foo
	var g __
	if err := Check(b, f); err != nil {
		return err
	}
	if err := json.Unmarshal(b, &g); err != nil {
		return err
	}
	*f = Foo(g)
	return nil
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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