Documentation
¶
Overview ¶
Package c14n provides canonical JSON encoding and decoding.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanonicalJSON ¶
CanonicalJSON performs the unmarshal and marshal commands in one go.
Example ¶
package main import ( "fmt" "strings" "github.com/invopop/gobl/c14n" ) func main() { d := `{ "foo":"bar", "c": 123.4, "a": 56, "b": 0.0, "y":null}` r := strings.NewReader(d) res, err := c14n.CanonicalJSON(r) if err != nil { panic(err.Error()) } fmt.Printf("%v\n", string(res)) }
Output: {"a":56,"b":0.0E0,"c":1.234E2,"foo":"bar"}
Types ¶
type Array ¶
type Array struct {
Values []Canonicalable
}
Array contains a list of canonicable values, as opposed to the objects key-value pairs.
func (*Array) MarshalJSON ¶
MarshalJSON recursively marshals all of the arrays items and joins the results together to form a JSON byte array.
type Attribute ¶
type Attribute struct { Key string Value Canonicalable }
Attribute represents a key-value pair used in objects. Using an array guarantees ordering of keys, which is one of the fundamental requirements for canonicalization.
func (*Attribute) MarshalJSON ¶
MarshalJSON creates a key-value pair in JSON format. A null value in an attribute will return an empty byte array.
type Bool ¶
type Bool bool
Bool handles binary true or false.
func (Bool) MarshalJSON ¶
MarshalJSON provides the JSON standard true or false response.
type Canonicalable ¶
Canonicalable defines what we expect from objects that need to be converted into our standardized JSON. All structures that comply with this interface are expected to contain data that was already sourced from a JSON document, so we don't need to worry too much about the conversion process.
func UnmarshalJSON ¶
func UnmarshalJSON(src io.Reader) (Canonicalable, error)
UnmarshalJSON expects an io Reader whose data will be parsed using a streaming JSON decoder and converted into a "Canonicalable" set of structures. The resulting objects can then be re-encoded back into canonical JSON suitable for sending to a hashing algorithm.
type Float ¶
type Float float64
Float numbers must be represented by a 64-bit signed integer and exponential that reflects the position of the decimal place. We're not going to support numbers whose signifcant digits do not fit inside an int64, for big numbers, use an alternative method of serialization such as Base64.
func (Float) MarshalJSON ¶
MarshalJSON for floats uses the strconv library with some output hacks to ensure we're in a valid JSON format. This is also actually what the fmt package and Printf related methods do to get around all the complexities of float conversion.
type Integer ¶
type Integer int64
Integer numbers have no decimal places and are limited to 64 bits.
func (Integer) MarshalJSON ¶
MarshalJSON provides string representation of integer.
type Null ¶
type Null struct{}
Null wraps around a null value
func (Null) MarshalJSON ¶
MarshalJSON provides the null string.
type Object ¶
type Object struct {
Attributes []*Attribute
}
Object contains a simple list of items, which are in essence key-value pairs. The item array means that attributes can be ordered by their key.
func (*Object) MarshalJSON ¶
MarshalJSON combines all the objects elements into an ordered key-value list of marshalled attributes.