Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Exists ¶
Exists checks whether the value that decoder points to (in the JSON bytes) exists and is the expected type
func Map ¶
Map decodes separate JSON fields into separate values, using separate decoders, all from the same JSON.
For example, if you have the following JSON:
json := `{"field1": 123, "field2": "456"}`
And you have the following decoders:
dcode1 := Field("field1", Int()) dcode2 := Field("field2", String())
You can do the following:
stucco := struct{ field1 int field2 string } // check the error here! Map( []byte(json), Pair(dcode1, &stucco.field1), Pair(dcode2, &stucco.field2), )
Note: you can decode JSON into structs using this function, but if you're trying to decode tons of fields from JSON to struct fields, you might be better off using encoding/json!
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is a convenience struct that lets you write decoders as a chain, rather than in the "pure functional" style. Instead of this:
dcoder := Field("first", Field("second", Field("third", Int())))
You can write this:
dcoder := First("first").Then("second").Then("third").Into(Int())
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is the core type in dcode. This is basically a pure function that can decode ANY JSON value into a particular type.
You can't implement one of these directly. Instead use one of the built-in ones like Int(), String(), etc... and build them up with Field(...) or First(...).Then(...).Into(...).
Check out the documentation for Field() or Builder for more information
func Field ¶
Field builds up a traversal. Here's an example that starts with "field1", then goes down to "field2", and then into "field3", and decodes that value into an int:
dcoder := Field("field1", Field("field2", Field("field3", Int())))
If you have the following JSON:
{ "field1": { "field2": { "field3": 123 } } }
Then the following code would decode the integer 123 into the variable i
var i int Decode(dcoder, jsonBytes, &i) // make sure to check the error returned!
type ErrWrongType ¶
type ErrWrongType struct {
// contains filtered or unexported fields
}
func (ErrWrongType) Error ¶
func (e ErrWrongType) Error() string