Documentation
¶
Overview ¶
Package jsonl provides a standalone JSONL API layered on JSONKit primitives.
It is intentionally separate from the root jsonkit facade surface: JSONL has different access/navigation patterns (record indexing + lazy record parsing) than single-document JSON APIs.
Index ¶
- Variables
- type Document
- func (d *Document) Count() int
- func (d *Document) Next(current int) (Record, error)
- func (d *Document) Prev(current int) (Record, error)
- func (d *Document) Raw(index int) ([]byte, error)
- func (d *Document) Record(index int) (Record, error)
- func (d *Document) RecordSpan(index int) (span.Span, bool)
- func (d *Document) Records() []RecordRef
- type Options
- type Record
- type RecordRef
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRecordOutOfRange = errors.New("jsonl: record index out of range")
ErrRecordOutOfRange reports an invalid record index.
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document indexes a JSONL source and lazily parses individual records.
Example ¶
package main
import (
"fmt"
"github.com/forgemechanic/jsonkit/exp/source"
"github.com/forgemechanic/jsonkit/jsonl"
)
func main() {
src := source.NewBytes("records.jsonl", []byte("{\"id\":1}\n{\"id\":2}\n"))
doc, _ := jsonl.Open(src, jsonl.DefaultOptions())
fmt.Println(doc.Count())
fmt.Println(doc.Records()[0].Line)
}
Output: 2 1
func Open ¶
Open indexes a JSONL source and returns a document handle.
Example ¶
package main
import (
"fmt"
"github.com/forgemechanic/jsonkit/exp/profile"
"github.com/forgemechanic/jsonkit/exp/source"
"github.com/forgemechanic/jsonkit/jsonl"
)
func main() {
src := source.NewBytes("records.jsonl", []byte("{\"id\":1}\n{\"id\":2}\n"))
p, _ := profile.Lookup(profile.ProfileStrict)
doc, err := jsonl.Open(src, jsonl.Options{Profile: p})
fmt.Println(err == nil)
fmt.Println(doc.Count())
rec, _ := doc.Record(1)
raw, _ := doc.Raw(1)
fmt.Println(rec.Ref.Index, rec.Ref.Line)
fmt.Println(string(raw))
}
Output: true 2 1 2 {"id":2}
func (*Document) Next ¶
Next returns the record after current.
Example ¶
package main
import (
"fmt"
"github.com/forgemechanic/jsonkit/exp/source"
"github.com/forgemechanic/jsonkit/jsonl"
)
func main() {
src := source.NewBytes("records.jsonl", []byte("{\"id\":1}\n{\"id\":2}\n{\"id\":3}\n"))
doc, _ := jsonl.Open(src, jsonl.DefaultOptions())
next, _ := doc.Next(0)
prev, _ := doc.Prev(2)
fmt.Println(next.Ref.Index)
fmt.Println(prev.Ref.Index)
}
Output: 1 1
func (*Document) RecordSpan ¶
RecordSpan returns the host span of a record by index.
type Options ¶
type Options struct {
// Profile selects the syntax/features used to parse each record.
Profile profile.Profile
// SkipBlankLines controls whether whitespace-only lines are omitted from records.
// Default true.
SkipBlankLines bool
}
Options configures JSONL document opening and record parsing.
Example ¶
package main
import (
"fmt"
"github.com/forgemechanic/jsonkit/exp/profile"
"github.com/forgemechanic/jsonkit/jsonl"
)
func main() {
p, _ := profile.Lookup(profile.ProfileJSON5)
opts := jsonl.Options{
Profile: p,
SkipBlankLines: true,
}
fmt.Println(opts.Profile.ID)
fmt.Println(opts.SkipBlankLines)
}
Output: json5 true
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns strict-profile JSONL options.
Example ¶
package main
import (
"fmt"
"github.com/forgemechanic/jsonkit/exp/source"
"github.com/forgemechanic/jsonkit/jsonl"
)
func main() {
src := source.NewBytes("records.jsonl", []byte("\n{\"id\":1}\n\n{\"id\":2}\n"))
doc, err := jsonl.Open(src, jsonl.DefaultOptions())
fmt.Println(err == nil)
fmt.Println(doc.Count())
}
Output: true 2
type Record ¶
type Record struct {
// Ref identifies record position and span in the host source.
Ref RecordRef
// Raw is the raw record bytes for this line.
Raw []byte
// Tree is the parsed CST for the record.
Tree cst.Tree
// Value is the projected semantic value for the record.
Value sem.Value
// Diagnostics contains per-record parse diagnostics.
Diagnostics []diag.Diagnostic
}
Record is a parsed JSONL record with raw bytes and analysis artifacts.
Example ¶
package main
import (
"fmt"
"github.com/forgemechanic/jsonkit/exp/sem"
"github.com/forgemechanic/jsonkit/exp/source"
"github.com/forgemechanic/jsonkit/jsonl"
)
func main() {
src := source.NewBytes("records.jsonl", []byte("{\"id\":1}\n"))
doc, _ := jsonl.Open(src, jsonl.DefaultOptions())
rec, _ := doc.Record(0)
fmt.Println(rec.Ref.Index)
fmt.Println(len(rec.Raw) > 0)
fmt.Println(rec.Value.Kind == sem.KindObject)
}
Output: 0 true true
type RecordRef ¶
type RecordRef struct {
// Index is the 0-based record index in the JSONL document.
Index int
// Line is the 1-based source line number where the record begins.
Line int
// Span is the host byte span of the raw record line.
Span span.Span
}
RecordRef points to a JSONL record location in the source.
Example ¶
package main
import (
"fmt"
"github.com/forgemechanic/jsonkit/exp/span"
"github.com/forgemechanic/jsonkit/jsonl"
)
func main() {
ref := jsonl.RecordRef{
Index: 3,
Line: 10,
Span: span.New(100, 120),
}
fmt.Println(ref.Index, ref.Line, ref.Span.Start, ref.Span.End)
}
Output: 3 10 100 120