Documentation
¶
Index ¶
- Variables
- func Marshal(v interface{}) ([]byte, error)
- func MarshalWithOptions(v interface{}, options EncoderOptions) ([]byte, error)
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalWithOptions(data []byte, v interface{}, options DecoderOptions) error
- type DecoderOptions
- type EncoderOptions
Constants ¶
This section is empty.
Variables ¶
var JSONNumberType = reflect.TypeOf(json.Number(""))
Functions ¶
func Marshal ¶
Marshal returns the Hjson encoding of v using default options.
See MarshalWithOptions.
func MarshalWithOptions ¶
func MarshalWithOptions(v interface{}, options EncoderOptions) ([]byte, error)
MarshalWithOptions returns the Hjson encoding of v.
The value v is traversed recursively.
Boolean values are written as true or false.
Floating point, integer, and json.Number values are written as numbers (with decimals only if needed, using . as decimals separator).
String values encode as Hjson strings (quoteless, multiline or JSON).
Array and slice values encode as arrays, surrounded by []. Unlike json.Marshal, hjson.Marshal will encode a nil-slice as [] instead of null.
Map values encode as objects, surrounded by {}. The map's key type must be possible to print to a string using fmt.Sprintf("%v", key), or implement encoding.TextMarshaler. The map keys are sorted alphabetically and used as object keys. Unlike json.Marshal, hjson.Marshal will encode a nil-map as {} instead of null.
Struct values also encode as objects, surrounded by {}. Only the exported fields are encoded to Hjson. The fields will appear in the same order as in the struct.
The encoding of each struct field can be customized by the format string stored under the "json" key in the struct field's tag. The format string gives the name of the field, possibly followed by a comma and "omitempty". The name may be empty in order to specify "omitempty" without overriding the default field name.
The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.
As a special case, if the field tag is "-", the field is always omitted. Note that a field with name "-" can still be generated using the tag "-,".
Comments can be set on struct fields using the "comment" key in the struct field's tag. The comment will be written on the line before the field key, prefixed with #. Or possible several lines prefixed by #, if there are line breaks (\n) in the comment text.
If both the "json" and the "comment" tag keys are used on a struct field they should be separated by whitespace.
Examples of struct field tags and their meanings:
// Field appears in Hjson as key "myName". Field int `json:"myName"` // Field appears in Hjson as key "myName" and the field is omitted from // the object if its value is empty, as defined above. Field int `json:"myName,omitempty"` // Field appears in Hjson as key "Field" (the default), but the field is // skipped if empty. Note the leading comma. Field int `json:",omitempty"` // Field is ignored by this package. Field int `json:"-"` // Field appears in Hjson as key "-". Field int `json:"-,"` // Field appears in Hjson preceded by a line just containing `# A comment.` Field int `comment:"A comment."` // Field appears in Hjson as key "myName" preceded by a line just // containing `# A comment.` Field int `json:"myName" comment:"A comment."`
Anonymous struct fields are usually marshaled as if their inner exported fields were fields in the outer struct, subject to the usual Go visibility rules amended as described in the next paragraph. An anonymous struct field with a name given in its JSON tag is treated as having that name, rather than being anonymous. An anonymous struct field of interface type is treated the same as having that type as its name, rather than being anonymous.
The Go visibility rules for struct fields are amended for JSON when deciding which field to marshal or unmarshal. If there are multiple fields at the same level, and that level is the least nested (and would therefore be the nesting level selected by the usual Go rules), the following extra rules apply:
1) Of those fields, if any are JSON-tagged, only tagged fields are considered, even if there are multiple untagged fields that would otherwise conflict.
2) If there is exactly one field (tagged or not according to the first rule), that is selected.
3) Otherwise there are multiple fields, and all are ignored; no error occurs.
Pointer values encode as the value pointed to. A nil pointer encodes as the null JSON value.
Interface values encode as the value contained in the interface. A nil interface value encodes as the null JSON value.
If an encountered value implements the json.Marshaler interface then the function MarshalJSON() is called on it. The JSON is then converted to Hjson using the current indentation and options given in the call to json.Marshal().
If an encountered value implements the encoding.TextMarshaler interface but not the json.Marshaler interface, then the function MarshalText() is called on it to get a text.
Channel, complex, and function values cannot be encoded in Hjson, will result in an error.
Hjson cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an error.
func Unmarshal ¶
Unmarshal parses the Hjson-encoded data using default options and stores the result in the value pointed to by v.
See UnmarshalWithOptions.
func UnmarshalWithOptions ¶
func UnmarshalWithOptions(data []byte, v interface{}, options DecoderOptions) error
UnmarshalWithOptions parses the Hjson-encoded data and stores the result in the value pointed to by v.
Internally the Hjson input is converted to JSON, which is then used as input to the function json.Unmarshal().
For more details about the output from this function, see the documentation for json.Unmarshal().
Types ¶
type DecoderOptions ¶
type DecoderOptions struct {
// UseJSONNumber causes the Decoder to unmarshal a number into an interface{} as a
// json.Number instead of as a float64.
UseJSONNumber bool
// DisallowUnknownFields causes an error to be returned when the destination
// is a struct and the input contains object keys which do not match any
// non-ignored, exported fields in the destination.
DisallowUnknownFields bool
}
DecoderOptions defines options for decoding Hjson.
func DefaultDecoderOptions ¶
func DefaultDecoderOptions() DecoderOptions
DefaultDecoderOptions returns the default decoding options.
type EncoderOptions ¶
type EncoderOptions struct {
// End of line, should be either \n or \r\n
Eol string
// Place braces on the same line
BracesSameLine bool
// Emit braces for the root object
EmitRootBraces bool
// Always place string in quotes
QuoteAlways bool
// Place string in quotes if it could otherwise be a number, boolean or null
QuoteAmbiguousStrings bool
// Indent string
IndentBy string
// Base indentation string
BaseIndentation string
}
EncoderOptions defines options for encoding to Hjson.
func DefaultOptions ¶
func DefaultOptions() EncoderOptions
DefaultOptions returns the default encoding options. Eol = "\n" BracesSameLine = true EmitRootBraces = true QuoteAlways = false QuoteAmbiguousStrings = true IndentBy = " " BaseIndentation = ""
