Documentation
¶
Overview ¶
Package json deals with JSON documents.
This library provides tools to work with raw JSON data, with no attempt to convert it to native go types until the last moment.
This allows to abide strictly to the JSON standards. In particular, it supports JSON-specifics which are not well handled by the standard library, such as:
- null
- values with a zero value in go (e.g. 0, "", false)
- very large or very high-precision numbers that overflow go native types (e.g. int64, float64)
Main memory usage ¶
The library design is primarily focused on keeping a low memory profile, in terms of space as well as in terms of allocations:
- few things are pointers or require allocations
- most data structures that need a dynamic allocation may be recycled using pools, thus amortizing the cost of allocations
- JSON values are isolated in a specific in-memory store, which takes care of optimizing their size
- object keys are interned
- lazy JSON value resolution
Immutability ¶
Another design goal of this package is immutability: all provided objects Document, light.Node, [token.Token], stores.Value etc are all immutable, and designed to be cheap to clone instead.
Mutating or constructing a JSON Document programmatically requires a Builder to carry out a series of fluent building methods and produce a modified clone of the original Document.
Extensibility ¶
There are tons of use-cases out there to play with.
Specific modules may be added to extend or improve the default implementations of lexers.Lexer, writers.Writer and stores.Store. As independent modules, they may bring with them their own set of dependencies without altering the dependencies of the parent module.
Index ¶
- Variables
- func RedeemBuilder(b *Builder)
- func RedeemDocument(d *Document)
- type Builder
- func (b *Builder) AppendElem(value Document) *Builder
- func (b *Builder) AppendKey(key string, value Document) *Builder
- func (b *Builder) Array() *Builder
- func (b *Builder) BoolValue(value bool) *Builder
- func (b Builder) Document() Document
- func (b Builder) Err() error
- func (b *Builder) FloatValue(value types.Number) *Builder
- func (b *Builder) From(d Document) *Builder
- func (b *Builder) Null() *Builder
- func (b *Builder) NumberValue(value types.Number) *Builder
- func (b *Builder) Object() *Builder
- func (b Builder) Ok() bool
- func (b *Builder) Reset()
- func (b *Builder) SetErr(err error)
- func (b *Builder) StringValue(value string) *Builder
- func (b *Builder) WithStore(s stores.Store) *Builder
- type Context
- type Document
- func (d Document) AtKey(k string) (Document, bool)
- func (d Document) Context() Context
- func (d *Document) Decode(r io.Reader) error
- func (d Document) Elem(i int) (Document, bool)
- func (d Document) Elems() iter.Seq[Document]
- func (d Document) Encode(w io.Writer) error
- func (d Document) GetPointer(Pointer) Document
- func (d Document) KeyIndex(k string) (int, bool)
- func (d Document) Kind() nodes.Kind
- func (d Document) Len() int
- func (o Document) LexerFactory() func([]byte) (lexers.Lexer, func())
- func (o Document) LexerFromReaderFactory() func(io.Reader) (lexers.Lexer, func())
- func (d Document) MarshalJSON() ([]byte, error)
- func (d Document) Node() light.Node
- func (d Document) Pairs() iter.Seq2[string, Document]
- func (d Document) Store() stores.Store
- func (d Document) String() string
- func (d *Document) UnmarshalJSON(data []byte) error
- func (d Document) Value() (stores.Value, bool)
- func (o Document) WriterFactory() func() (writers.Writer, func())
- func (o Document) WriterToWriterFactory() func(io.Writer) (writers.Writer, func())
- type DocumentCollection
- func (c *DocumentCollection) Append(d Document)
- func (c *DocumentCollection) Concat(d DocumentCollection)
- func (o DocumentCollection) LexerFactory() func([]byte) (lexers.Lexer, func())
- func (o DocumentCollection) LexerFromReaderFactory() func(io.Reader) (lexers.Lexer, func())
- func (o DocumentCollection) WriterFactory() func() (writers.Writer, func())
- func (o DocumentCollection) WriterToWriterFactory() func(io.Writer) (writers.Writer, func())
- type DocumentFactory
- func (f DocumentFactory) Clone(d Document) Document
- func (f DocumentFactory) Empty() Document
- func (o DocumentFactory) LexerFactory() func([]byte) (lexers.Lexer, func())
- func (o DocumentFactory) LexerFromReaderFactory() func(io.Reader) (lexers.Lexer, func())
- func (o DocumentFactory) WriterFactory() func() (writers.Writer, func())
- func (o DocumentFactory) WriterToWriterFactory() func(io.Writer) (writers.Writer, func())
- type Option
- type Pointer
- type VerbatimDocument
Constants ¶
This section is empty.
Variables ¶
var EmptyDocument = Make()
EmptyDocument is the JSON document of the null type.
It has no stores.Store attached to it.
Functions ¶
func RedeemBuilder ¶
func RedeemBuilder(b *Builder)
func RedeemDocument ¶
func RedeemDocument(d *Document)
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder builds or transforms JSON Document s programmatically.
You may either use it directly, starting from the EmptyDocument or clone from an existing Document using Builder.From.
Since a Document is immutable, the Builder always produces a shallow clone of the original Document.
The Builder exposes fluent building methods which may be chained to construct a JSON document. You should always check the final error state, since the building cease to be effective as soon as an error is encountered.
func BorrowBuilder ¶
func BorrowBuilder() *Builder
func MakeBuilder ¶
func NewBuilder ¶
func (*Builder) AppendElem ¶
func (Builder) Document ¶
Document returns the Document produced by the Builder.
If a build error has occured, it returns the EmptyDocument.
func (*Builder) StringValue ¶
StringValue builds a JSON string
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents a JSON document as a hierarchy of JSON data nodes.
A Document knows how to marshal or unmarshal bytes or decode/encode with streams.
A Document is immutable: it may be unmarshaled from JSON, from [Dynamic JSON] or built programmatically using the Builder.
Accessing nodes ¶
Document.AtKey retrieves an individual keys in an object. Document.Elem does the same for an element in an array.
Iterators ¶
The hierarchy of nodes defined by a Document may be explored using iterators like Document.Pairs and Document.Elems.
Iterators maintain the original order in which object keys and array elements have been provided.
Exploring a document ¶
JSON pointers are supported within a Document using Document.GetPointer.
An implementation of JSONPath is provided in github.com/fredbi/core/json/documents.jsonpath to resolve JSONPath expressions as a Document iterator.
Working with values ¶
TODO(fred)
Dynamic JSON ¶
We call "dynamic JSON" (sometimes referred to as "untyped") refers to the go structures made up of "map[string]any" and "[]any" that you typically get when the go standard library unmarshals JSON into an "any" type (aka "interface{}").
A Document may unmarshal such a data structure or may be converted into one.
In that case, due to the implementation of go maps, the order of keys in objects cannot be maintained.
Related projects ¶
This package only deals with pure JSON, not schemas.
Package github.com/fredbi/core/jsonschma brings the additional logic required to deal with JSON schemas.
Package github.com/fredbi/core/spec brings the additional logic required to deal with OpenAPI documents.
func BorrowDocument ¶
func BorrowDocument() *Document
func (Document) AtKey ¶
AtKey returns the value held under a key in an object, or false if not found.
Key lookup is constant-time (map index).
func (Document) Elems ¶
Elems returns all elements in an array.
Iteration order is stable and honors the original ordering in which elements were declared.
func (Document) GetPointer ¶
func (Document) LexerFactory ¶
func (Document) LexerFromReaderFactory ¶
func (Document) MarshalJSON ¶
MarshalJSON writes the Document as JSON bytes.
func (Document) Pairs ¶
Pairs return all (key,Node) pairs inside an object.
Iteration order is stable and honors the original ordering in which keys were declared.
func (*Document) UnmarshalJSON ¶
UnmarshalJSON builds a Document from JSON bytes.
func (Document) WriterFactory ¶
type DocumentCollection ¶
type DocumentCollection struct {
// contains filtered or unexported fields
}
DocumentCollection is a collection of Document s, that share the same options.
It marshals as an array of [Document]s (TODO).
func NewDocumentCollection ¶
func NewDocumentCollection(opts ...Option) *DocumentCollection
func (*DocumentCollection) Append ¶
func (c *DocumentCollection) Append(d Document)
func (*DocumentCollection) Concat ¶
func (c *DocumentCollection) Concat(d DocumentCollection)
func (DocumentCollection) LexerFactory ¶
func (DocumentCollection) LexerFromReaderFactory ¶
func (DocumentCollection) WriterFactory ¶
type DocumentFactory ¶
type DocumentFactory struct {
// contains filtered or unexported fields
}
DocumentFactory is a factory that produces Document s with the same settings.
func NewDocumentFactory ¶
func NewDocumentFactory(opts ...Option) *DocumentFactory
NewDocumentFactory builds a factory for Document s
func (DocumentFactory) Clone ¶
func (f DocumentFactory) Clone(d Document) Document
func (DocumentFactory) Empty ¶
func (f DocumentFactory) Empty() Document
func (DocumentFactory) LexerFactory ¶
func (DocumentFactory) LexerFromReaderFactory ¶
func (DocumentFactory) WriterFactory ¶
type VerbatimDocument ¶
type VerbatimDocument struct { }
VerbatimDocument holds a JSON document verbatim.
All original marks kepts, including non-significant white space and escaped unicode points.
TODO(fred)
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package constrained expose constrained json.Document types.
|
Package constrained expose constrained json.Document types. |
Package dynamic converts a json.Document to dynamic JSON and vice versa.
|
Package dynamic converts a json.Document to dynamic JSON and vice versa. |
Package lexers exposes interfaces for lexing JSON.
|
Package lexers exposes interfaces for lexing JSON. |
default-lexer
Package lexer provides a JSON lexer.
|
Package lexer provides a JSON lexer. |
error-codes
Package codes exposes common lexer errors.
|
Package codes exposes common lexer errors. |
ld-lexer
Package lexer exposes lexer for ld-json (line-delimited JSON).
|
Package lexer exposes lexer for ld-json (line-delimited JSON). |
token
Package token defines common JSON token types with their kind.
|
Package token defines common JSON token types with their kind. |
Package nodes declares node types for JSON documents.
|
Package nodes declares node types for JSON documents. |
light
Package light exposes an implementation for nodes.
|
Package light exposes an implementation for nodes. |
Package stores exposes the interface to work with a JSON Store.
|
Package stores exposes the interface to work with a JSON Store. |
default-store
Package store provides default implementations for stores.Store.
|
Package store provides default implementations for stores.Store. |
Package types defines common types to work with json.
|
Package types defines common types to work with json. |
default-writer
Package writer exposes a JSON writer.
|
Package writer exposes a JSON writer. |