serialization

package module
v0.30.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package serialization provides JSON, YAML, TOML, and JSONL output formatters for tabular, tree, and graph data.

Table renderers implement output.TableRenderer via embedding output.TableStore. Tree and graph renderers implement output.TreeRenderer and output.GraphRenderer respectively, marshaling data structures using the appropriate encoding library.

Use MarshalJSON, MarshalYAML, or MarshalTOML for one-shot marshaling of any value, or MarshalTOMLFromTable / MarshalJSONLFromTable for Table. JSON and YAML Table use the renderer-based dispatch (JSONTableRenderer, YAMLTableRenderer) via the output.RenderTable registry.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSON

func MarshalJSON(v any) ([]byte, error)

MarshalJSON encodes v to JSON.

func MarshalJSONLFromTable added in v0.30.0

func MarshalJSONLFromTable(data *output.Table) ([]byte, error)

MarshalJSONLFromTable marshals Table as JSON Lines.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/serialization"
)

func main() {
	data := output.NewTable([]string{"Name", "Age"})
	data.AddRow([]string{"Alice", "30"})
	data.AddRow([]string{"Bob", "25"})

	result, err := serialization.MarshalJSONLFromTable(data)
	if err != nil {
		fmt.Printf("error: %v\n", err)

		return
	}

	fmt.Print(string(result))
}

func MarshalTOML

func MarshalTOML(v any) ([]byte, error)

MarshalTOML encodes v to TOML.

func MarshalTOMLFromTable added in v0.30.0

func MarshalTOMLFromTable(data *output.Table) ([]byte, error)

MarshalTOMLFromTable marshals Table as TOML.

func MarshalYAML

func MarshalYAML(v any) ([]byte, error)

MarshalYAML encodes v to YAML.

func RenderJSON added in v0.30.0

func RenderJSON(data *output.Table) (string, error)

RenderJSON renders a Table as a JSON string.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/serialization"
)

func main() {
	tbl := output.NewTableBuilder().
		SetHeaders("Name", "Status").
		AddRow("Compile", "done").
		AddRow("Test", "done").
		Build()

	json, err := serialization.RenderJSON(tbl)
	if err != nil {
		fmt.Printf("error: %v\n", err)

		return
	}

	fmt.Println(json)
}

func RenderJSONL added in v0.30.0

func RenderJSONL(data *output.Table) (string, error)

RenderJSONL renders a Table as a JSONL string.

func RenderTOML added in v0.30.0

func RenderTOML(data *output.Table) (string, error)

RenderTOML renders a Table as a TOML string.

func RenderYAML added in v0.30.0

func RenderYAML(data *output.Table) (string, error)

RenderYAML renders a Table as a YAML string.

func UnmarshalJSON

func UnmarshalJSON(data []byte, v any) error

UnmarshalJSON decodes JSON data into v.

func UnmarshalTOML

func UnmarshalTOML(data []byte, v any) error

UnmarshalTOML decodes TOML data into v.

func UnmarshalYAML

func UnmarshalYAML(data []byte, v any) error

UnmarshalYAML decodes YAML data into v.

func WriteJSON added in v0.30.0

func WriteJSON(w io.Writer, data *output.Table) error

WriteJSON writes a Table as JSON directly to the provided writer using json.NewEncoder — no intermediate string allocation.

func WriteJSONL added in v0.30.0

func WriteJSONL(w io.Writer, data *output.Table) error

WriteJSONL writes a Table as JSON Lines directly to the provided writer. Each row is encoded as a separate JSON object on its own line via NewJSONLWriter — true row-level streaming.

func WriteTOML added in v0.30.0

func WriteTOML(w io.Writer, data *output.Table) error

WriteTOML writes a Table as TOML directly to the provided writer using toml.NewEncoder — no intermediate string allocation. Rows are nested under the key "row" because TOML cannot encode a bare top-level array.

func WriteYAML added in v0.30.0

func WriteYAML(w io.Writer, data *output.Table) error

WriteYAML writes a Table as YAML directly to the provided writer using yaml.NewEncoder — no intermediate string allocation.

Types

type JSONGraphRenderer

type JSONGraphRenderer struct {
	output.GraphBuilder
}

JSONGraphRenderer renders graph nodes and edges as JSON.

func NewJSONGraphRenderer

func NewJSONGraphRenderer() *JSONGraphRenderer

NewJSONGraphRenderer creates a new JSONGraphRenderer.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/serialization"
	"github.com/larsartmann/go-output/testhelpers"
	"github.com/larsartmann/go-output/testhelpers/graphtest"
)

func main() {
	renderer := serialization.NewJSONGraphRenderer()
	renderer.SetNodes([]output.GraphNode{
		{
			ID:    output.NewBrandedID[output.GraphNodeIDBrand]("a"),
			Label: output.NewBrandedID[output.GraphNodeLabelBrand]("Node A"),
		},
	})
	renderer.SetEdges([]output.GraphEdge{
		graphtest.NewTestEdge("a", "b", ""),
	})

	fmt.Println(testhelpers.MustRender(renderer))
}

func (*JSONGraphRenderer) Render

func (r *JSONGraphRenderer) Render() (string, error)

Render returns the graph as a JSON string.

type JSONLTableRenderer

type JSONLTableRenderer struct {
	output.TableStore
}

JSONLTableRenderer renders Table as JSON Lines.

func NewJSONLTableRenderer

func NewJSONLTableRenderer() *JSONLTableRenderer

NewJSONLTableRenderer creates a new JSONLTableRenderer.

func (*JSONLTableRenderer) Render

func (r *JSONLTableRenderer) Render() (string, error)

Render returns the table data as JSON Lines.

type JSONLWriter

type JSONLWriter struct {
	// contains filtered or unexported fields
}

JSONLWriter writes JSON Lines output — one JSON object per line.

func NewJSONLWriter

func NewJSONLWriter(w io.Writer) *JSONLWriter

NewJSONLWriter creates a new JSONLWriter.

func (*JSONLWriter) Encode

func (j *JSONLWriter) Encode(v any) error

Encode writes v as a single JSON line to the underlying writer.

func (*JSONLWriter) Flush

func (j *JSONLWriter) Flush() error

Flush flushes the underlying buffered writer.

type JSONTableRenderer

type JSONTableRenderer struct {
	output.TableStore
}

JSONTableRenderer renders Table as a JSON array of objects.

func NewJSONTableRenderer

func NewJSONTableRenderer() *JSONTableRenderer

NewJSONTableRenderer creates a new JSONTableRenderer.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/serialization"
)

func main() {
	data := output.NewTable([]string{"Name", "Age"})
	data.AddRow([]string{"Alice", "30"})
	data.AddRow([]string{"Bob", "25"})

	renderer := serialization.NewJSONTableRenderer()
	renderer.SetData(data)

	result, err := renderer.Render()
	if err != nil {
		fmt.Printf("error: %v\n", err)

		return
	}

	fmt.Println(result)
}

func (*JSONTableRenderer) Render

func (r *JSONTableRenderer) Render() (string, error)

Render returns the table data as a JSON string.

type JSONTreeRenderer

type JSONTreeRenderer struct {
	// contains filtered or unexported fields
}

JSONTreeRenderer renders a TreeNode hierarchy as JSON.

func NewJSONTreeRenderer

func NewJSONTreeRenderer() *JSONTreeRenderer

NewJSONTreeRenderer creates a new JSONTreeRenderer.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/serialization"
)

func main() {
	root := output.NewTreeNode("root", "Root")
	root.AddChild(output.NewTreeNode("child1", "Child 1"))
	root.AddChild(output.NewTreeNode("child2", "Child 2"))

	renderer := serialization.NewJSONTreeRenderer()
	renderer.SetRoot(root)

	result, err := renderer.Render()
	if err != nil {
		fmt.Printf("error: %v\n", err)

		return
	}

	fmt.Println(result)
}

func (*JSONTreeRenderer) Render

func (r *JSONTreeRenderer) Render() (string, error)

Render returns the tree as a JSON string.

func (*JSONTreeRenderer) SetRoot

func (r *JSONTreeRenderer) SetRoot(node *output.TreeNode)

SetRoot sets the root node of the tree.

type JSONWriter

type JSONWriter struct {
	Writer io.Writer
}

JSONWriter writes JSON output to an io.Writer.

func NewJSONWriter

func NewJSONWriter(w io.Writer) *JSONWriter

NewJSONWriter creates a new JSONWriter.

func (*JSONWriter) Encode

func (j *JSONWriter) Encode(v any) error

Encode writes v as JSON to the underlying writer.

type TOMLGraphRenderer

type TOMLGraphRenderer struct {
	output.GraphBuilder
}

TOMLGraphRenderer renders graph nodes and edges as TOML.

func NewTOMLGraphRenderer

func NewTOMLGraphRenderer() *TOMLGraphRenderer

NewTOMLGraphRenderer creates a new TOMLGraphRenderer.

func (*TOMLGraphRenderer) Render

func (r *TOMLGraphRenderer) Render() (string, error)

Render returns the graph as a TOML string.

type TOMLTableRenderer

type TOMLTableRenderer struct {
	output.TableStore
}

TOMLTableRenderer renders Table as a TOML array of tables.

func NewTOMLTableRenderer

func NewTOMLTableRenderer() *TOMLTableRenderer

NewTOMLTableRenderer creates a new TOMLTableRenderer.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/serialization"
)

func main() {
	data := output.NewTable([]string{"Name", "Age"})
	data.AddRow([]string{"Alice", "30"})

	renderer := serialization.NewTOMLTableRenderer()
	renderer.SetData(data)

	result, err := renderer.Render()
	if err != nil {
		fmt.Printf("error: %v\n", err)

		return
	}

	fmt.Println(result)
}

func (*TOMLTableRenderer) Render

func (r *TOMLTableRenderer) Render() (string, error)

Render returns the table data as a TOML string using array-of-tables syntax.

type TOMLTreeRenderer

type TOMLTreeRenderer struct {
	// contains filtered or unexported fields
}

TOMLTreeRenderer renders a TreeNode hierarchy as TOML.

func NewTOMLTreeRenderer

func NewTOMLTreeRenderer() *TOMLTreeRenderer

NewTOMLTreeRenderer creates a new TOMLTreeRenderer.

func (*TOMLTreeRenderer) Render

func (r *TOMLTreeRenderer) Render() (string, error)

Render returns the tree as a TOML string.

func (*TOMLTreeRenderer) SetRoot

func (r *TOMLTreeRenderer) SetRoot(node *output.TreeNode)

SetRoot sets the root node of the tree.

type YAMLGraphRenderer

type YAMLGraphRenderer struct {
	output.GraphBuilder
}

YAMLGraphRenderer renders graph nodes and edges as YAML.

func NewYAMLGraphRenderer

func NewYAMLGraphRenderer() *YAMLGraphRenderer

NewYAMLGraphRenderer creates a new YAMLGraphRenderer.

func (*YAMLGraphRenderer) Render

func (r *YAMLGraphRenderer) Render() (string, error)

Render returns the graph as a YAML string.

type YAMLTableRenderer

type YAMLTableRenderer struct {
	output.TableStore
}

YAMLTableRenderer renders Table as a YAML sequence of mappings.

func NewYAMLTableRenderer

func NewYAMLTableRenderer() *YAMLTableRenderer

NewYAMLTableRenderer creates a new YAMLTableRenderer.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/serialization"
)

func main() {
	data := output.NewTable([]string{"Name", "Age"})
	data.AddRow([]string{"Alice", "30"})

	renderer := serialization.NewYAMLTableRenderer()
	renderer.SetData(data)

	result, err := renderer.Render()
	if err != nil {
		fmt.Printf("error: %v\n", err)

		return
	}

	fmt.Println(result)
}

func (*YAMLTableRenderer) Render

func (r *YAMLTableRenderer) Render() (string, error)

Render returns the table data as a YAML string.

type YAMLTreeRenderer

type YAMLTreeRenderer struct {
	// contains filtered or unexported fields
}

YAMLTreeRenderer renders a TreeNode hierarchy as YAML.

func NewYAMLTreeRenderer

func NewYAMLTreeRenderer() *YAMLTreeRenderer

NewYAMLTreeRenderer creates a new YAMLTreeRenderer.

func (*YAMLTreeRenderer) Render

func (r *YAMLTreeRenderer) Render() (string, error)

Render returns the tree as a YAML string.

func (*YAMLTreeRenderer) SetRoot

func (r *YAMLTreeRenderer) SetRoot(node *output.TreeNode)

SetRoot sets the root node of the tree.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL