Documentation
¶
Overview ¶
Package json implements the stable encoding/json API with support for the FloraSync JSONC Profile v1.
The profile adds JavaScript-style line and block comments and permits one trailing comma after the final member of a non-empty object or element of a non-empty array. All encoding operations emit ordinary JSON.
Index ¶
- Variables
- func Compact(dst *bytes.Buffer, src []byte) error
- func HTMLEscape(dst *bytes.Buffer, src []byte)
- func HasCommentRunes(data []byte) bool
- func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
- func Marshal(v any) ([]byte, error)
- func MarshalIndent(v any, prefix, indent string) ([]byte, error)
- func Sanitize(data []byte) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- func Valid(data []byte) bool
- type Decoder
- type Delim
- type Encoder
- type InvalidUTF8Error
- type InvalidUnmarshalError
- type JSONCSyntaxError
- type Marshaler
- type MarshalerError
- type Number
- type RawMessage
- type SyntaxError
- type Token
- type UnmarshalFieldError
- type UnmarshalTypeError
- type Unmarshaler
- type UnsupportedTypeError
- type UnsupportedValueError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidUTF8 reports malformed UTF-8 in a JSONC comment body. // Malformed UTF-8 outside comments retains encoding/json behavior. ErrInvalidUTF8 = errors.New("jsonc: invalid UTF-8 in comment") // ErrUnterminatedBlockComment reports a block comment without a closing */. ErrUnterminatedBlockComment = errors.New("jsonc: unterminated block comment") )
Functions ¶
func HTMLEscape ¶
HTMLEscape preserves the behavior of encoding/json.HTMLEscape. Its input contract remains ordinary JSON because the standard signature cannot report malformed JSONC extensions.
func HasCommentRunes ¶
HasCommentRunes reports whether data contains a // or /* opener outside a JSON string. It does not validate the surrounding document or detect trailing commas.
func MarshalIndent ¶
MarshalIndent returns the indented JSON encoding of v.
func Sanitize ¶
Sanitize replaces JSONC comments and accepted trailing commas with byte-length-preserving JSON whitespace. It never mutates data.
Sanitize validates JSONC lexical extensions but does not otherwise validate JSON. Call Valid or Unmarshal when full validation is required.
func Unmarshal ¶
Unmarshal parses JSON or JSONC data and stores the result in v.
Example ¶
var v interface{}
data := []byte(`{/* comment */"foo": "bar"}`)
err := json.Unmarshal(data, &v)
if err != nil {
panic(err)
}
fmt.Println(v)
Output: map[foo:bar]
Example (SanitizeError) ¶
var v interface{}
invalid := append([]byte("/*"), byte(0xa5))
invalid = append(invalid, []byte("*/{}")...)
err := json.Unmarshal(invalid, &v)
fmt.Println(err)
Output: jsonc: invalid UTF-8 in comment at byte 3
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads JSON or JSONC values from an input stream.
Decoder preserves encoding/json's streaming behavior while presenting a byte-length-preserving normalized view to the standard decoder. As with encoding/json.Decoder, a Decoder is not safe for concurrent use.
func NewDecoder ¶
NewDecoder returns a decoder that reads JSON or JSONC values from r.
The decoder introduces its own buffering and may read data from r beyond the values requested.
func (*Decoder) Buffered ¶
Buffered returns a reader of committed normalized data remaining in the Decoder's buffers. The reader is valid until the next call to Decode.
A trailing-comma candidate whose disposition depends on unread input is not committed until the normalizer encounters the following significant byte.
func (*Decoder) Decode ¶
Decode reads the next JSON-encoded or JSONC-encoded value from its input and stores it in the value pointed to by v.
func (*Decoder) DisallowUnknownFields ¶
func (d *Decoder) DisallowUnknownFields()
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and an object contains an unknown key.
func (*Decoder) InputOffset ¶
InputOffset returns the original input stream byte offset of the current decoder position.
func (*Decoder) More ¶
More reports whether there is another element in the current array or object being parsed.
type Encoder ¶
func NewEncoder ¶
NewEncoder returns a standard JSON encoder that writes to w.
type InvalidUTF8Error ¶
type InvalidUTF8Error = stdjson.InvalidUTF8Error
type InvalidUnmarshalError ¶
type InvalidUnmarshalError = stdjson.InvalidUnmarshalError
type JSONCSyntaxError ¶
JSONCSyntaxError describes syntax that belongs to the JSONC extension rather than ordinary JSON. Offset is a one-based byte offset into the original input. Err can be inspected with errors.Is.
func (*JSONCSyntaxError) Error ¶
func (e *JSONCSyntaxError) Error() string
func (*JSONCSyntaxError) Unwrap ¶
func (e *JSONCSyntaxError) Unwrap() error
Unwrap returns the JSONC syntax category.
type MarshalerError ¶
type MarshalerError = stdjson.MarshalerError
type RawMessage ¶
type RawMessage = stdjson.RawMessage
type SyntaxError ¶
type SyntaxError = stdjson.SyntaxError
type UnmarshalFieldError ¶
type UnmarshalFieldError = stdjson.UnmarshalFieldError
type UnmarshalTypeError ¶
type UnmarshalTypeError = stdjson.UnmarshalTypeError
type Unmarshaler ¶
type Unmarshaler = stdjson.Unmarshaler
type UnsupportedTypeError ¶
type UnsupportedTypeError = stdjson.UnsupportedTypeError
type UnsupportedValueError ¶
type UnsupportedValueError = stdjson.UnsupportedValueError