encoding

package
v2.6.3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 3 Imported by: 25

Documentation

Overview

Package encoding provides basic decoding implementations.

Decode decodes HTTP responses:

resp, _ := http.Get("http://api.example.com/")
...
var data map[string]interface{}
err := JSONDecoder(resp.Body, &data)

Index

Examples

Constants

View Source
const JSON = "json"

JSON is the key for the json encoding

View Source
const NOOP = "no-op"

NOOP is the key for the NoOp encoding

View Source
const SAFE_JSON = "safejson"

SAFE_JSON is the key for the json encoding

View Source
const STRING = "string"

STRING is the key for the string encoding

Variables

This section is empty.

Functions

func JSONCollectionDecoder

func JSONCollectionDecoder(r io.Reader, v *map[string]interface{}) error

JSONCollectionDecoder decodes a json collection and returns a map with the array at the 'collection' key

func JSONDecoder

func JSONDecoder(r io.Reader, v *map[string]interface{}) error

JSONDecoder decodes a json message into a map

func NewJSONDecoder

func NewJSONDecoder(isCollection bool) func(io.Reader, *map[string]interface{}) error

NewJSONDecoder returns the right JSON decoder

Example (Collection)
decoder := NewJSONDecoder(true)
original := strings.NewReader(`["foo", "bar", "supu"]`)
var result map[string]interface{}
if err := decoder(original, &result); err != nil {
	fmt.Println("Unexpected error:", err.Error())
}
fmt.Printf("%+v\n", result)
Output:

map[collection:[foo bar supu]]
Example (Map)
decoder := NewJSONDecoder(false)
original := strings.NewReader(`{"foo": "bar", "supu": false, "tupu": 4.20}`)
var result map[string]interface{}
if err := decoder(original, &result); err != nil {
	fmt.Println("Unexpected error:", err.Error())
}
fmt.Printf("%+v\n", result)
Output:

map[foo:bar supu:false tupu:4.20]

func NewSafeJSONDecoder

func NewSafeJSONDecoder(_ bool) func(io.Reader, *map[string]interface{}) error

NewSafeJSONDecoder returns the universal json decoder

Example
decoder := NewSafeJSONDecoder(true)
for _, body := range []string{
	`{"foo": "bar", "supu": false, "tupu": 4.20}`,
	`["foo", "bar", "supu"]`,
} {
	var result map[string]interface{}
	if err := decoder(strings.NewReader(body), &result); err != nil {
		fmt.Println("Unexpected error:", err.Error())
	}
	fmt.Printf("%+v\n", result)
}
Output:

map[foo:bar supu:false tupu:4.20]
map[collection:[foo bar supu]]

func NewStringDecoder

func NewStringDecoder(_ bool) func(io.Reader, *map[string]interface{}) error

NewStringDecoder returns a String decoder

func NoOpDecoder

func NoOpDecoder(_ io.Reader, _ *map[string]interface{}) error

NoOpDecoder is a decoder that does nothing

func SafeJSONDecoder

func SafeJSONDecoder(r io.Reader, v *map[string]interface{}) error

SafeJSONDecoder decodes both json objects and collections

func StringDecoder

func StringDecoder(r io.Reader, v *map[string]interface{}) error

StringDecoder returns a map with the content of the reader under the key 'content'

Types

type Decoder

type Decoder func(io.Reader, *map[string]interface{}) error

Decoder is a function that reads from the reader and decodes it into an map of interfaces

type DecoderFactory

type DecoderFactory func(bool) func(io.Reader, *map[string]interface{}) error

DecoderFactory is a function that returns CollectionDecoder or an EntityDecoder

type DecoderRegister

type DecoderRegister struct {
	// contains filtered or unexported fields
}

DecoderRegister is the struct responsible of registering the decoder factories

func GetRegister

func GetRegister() *DecoderRegister

GetRegister returns the package register

func (*DecoderRegister) Get

func (r *DecoderRegister) Get(name string) func(bool) func(io.Reader, *map[string]interface{}) error

Get returns a decoder factory from the register by name. If no factory is found, it returns a JSON decoder factory

func (*DecoderRegister) Register

func (r *DecoderRegister) Register(name string, dec func(bool) func(io.Reader, *map[string]interface{}) error) error

Register adds a decoder factory to the register

Jump to

Keyboard shortcuts

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