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 ¶
- Constants
- func JSONCollectionDecoder(r io.Reader, v *map[string]interface{}) error
- func JSONDecoder(r io.Reader, v *map[string]interface{}) error
- func NewJSONDecoder(isCollection bool) func(io.Reader, *map[string]interface{}) error
- func NewSafeJSONDecoder(_ bool) func(io.Reader, *map[string]interface{}) error
- func NewStringDecoder(_ bool) func(io.Reader, *map[string]interface{}) error
- func NoOpDecoder(_ io.Reader, _ *map[string]interface{}) error
- func SafeJSONDecoder(r io.Reader, v *map[string]interface{}) error
- func StringDecoder(r io.Reader, v *map[string]interface{}) error
- type Decoder
- type DecoderFactory
- type DecoderRegister
Examples ¶
Constants ¶
const JSON = "json"
JSON is the key for the json encoding
const NOOP = "no-op"
NOOP is the key for the NoOp encoding
const SAFE_JSON = "safejson"
SAFE_JSON is the key for the json encoding
const STRING = "string"
STRING is the key for the string encoding
Variables ¶
This section is empty.
Functions ¶
func JSONCollectionDecoder ¶
JSONCollectionDecoder decodes a json collection and returns a map with the array at the 'collection' key
func JSONDecoder ¶
JSONDecoder decodes a json message into a map
func NewJSONDecoder ¶
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 ¶
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 ¶
NewStringDecoder returns a String decoder
func NoOpDecoder ¶
NoOpDecoder is a decoder that does nothing
func SafeJSONDecoder ¶
SafeJSONDecoder decodes both json objects and collections
Types ¶
type Decoder ¶
Decoder is a function that reads from the reader and decodes it into an map of interfaces
type DecoderFactory ¶
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