Documentation
¶
Overview ¶
Package jwcc implements a parser for JSON With Commas and Comments (JWCC) as defined by https://nigeltao.github.io/blog/2021/json-with-commas-comments.html
Index ¶
- func CleanComments(coms ...string) []string
- func Format(w io.Writer, v Value) error
- func FormatToString(v Value) string
- func ValueLocation(v Value) jtree.Location
- type Array
- type Comments
- type Datum
- type Document
- type Formatter
- type Member
- type Object
- func (o *Object) Comments() *Comments
- func (o *Object) Find(key string) *Member
- func (o *Object) FindKey(f func(ast.Text) bool) *Member
- func (o *Object) IndexKey(f func(ast.Text) bool) int
- func (o Object) JSON() string
- func (o Object) Len() int
- func (o Object) Sort()
- func (o Object) String() string
- func (o *Object) Undecorate() ast.Value
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanComments ¶
CleanComments combines and removes comment markers from the given comments, returning a slice of plain lines of text. Leading and trailing spaces are removed from the lines.
func FormatToString ¶
FormatToString formats v to a string with default settings. In case of error in formatting, it returns an empty string.
func ValueLocation ¶
ValueLocation reports the location of the specified value.
Types ¶
type Array ¶
type Array struct { Values []Value // contains filtered or unexported fields }
An Array is a commented array of values.
func (*Array) Undecorate ¶
type Comments ¶
type Comments struct { Before []string Line string End []string // contains filtered or unexported fields }
Comments records the comments associated with a value. All values have a comment record; use IsEmpty to test whether the value has any actual comment text.
Comments read during parsing are stored as-seen in the input, including comment markers. When adding or editing a comment programmatically, comment markers are optional; the text will be decorated when formatting the output. To include blank space separating multiple chunks of comment text, include an empty string. To include a blank line in the middle of a chunk, include a comment marker.
For example:
c := jtree.Comments{Before: []string{"a", "", "b"}}
renders as:
// a // b
By contrast:
c := jtree.Comments{Before: []string{"a", "//", "c"}}
renders as:
// a // // b
If a comment begins with "//", it will be processed as line comments. If a comment begins with "/*" it will be processed as a block comment. Multiple lines are OK; they will be reformatted as necessary.
type Datum ¶
A Datum is a commented base value; a string, number, Boolean, or null.
func (*Datum) Undecorate ¶
type Document ¶
type Document struct { Value // contains filtered or unexported fields }
A Document is a single value with optional trailing comments.
func Parse ¶
Parse parses and returns a single JWCC value from r. If r contains data after the first value, apart from comments and whitespace, Parse returns the first value along with an ast.ErrExtraInput error.
func (*Document) Undecorate ¶
type Formatter ¶
type Formatter struct{}
A Formatter carries the settings for pretty-printing JWCC values. A zero value is ready for use with default settings.
type Member ¶
A Member is a key-value pair in an object.
func Field ¶
Field constructs an object member with the given key and value. The value must be a string, int, float, bool, nil, or jwcc.Value.
func (*Member) Undecorate ¶
type Object ¶
type Object struct { Members []*Member // contains filtered or unexported fields }
An Object is a collection of key-value members.
func (*Object) FindKey ¶
FindKey returns the first member of o for whose key f reports true, or nil.
func (*Object) IndexKey ¶
IndexKey returns the index of the first member of o for whose key f reports true, or -1.
func (*Object) Undecorate ¶
type Value ¶
type Value interface { ast.Value // Comments returns the comments annotating this value. Comments() *Comments // Convert this value to an undecorated ast.Value. Undecorate() ast.Value }
A Value is a JSON value optionally decorated with comments. The concrete type is one *Array, *Datum, *Document, or *Object.