ron

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 24 Imported by: 0

README

ron-go

Go reference implementation for RON, Readable Object Notation.

RON is documented in the language-neutral reference repo:

Install

go get github.com/starfederation/ron-go

API

package main

import ron "github.com/starfederation/ron-go"

func main() {
    ronBody := []byte("find [?id ?name]")

    compactJSON, err := ron.ToJSON(ronBody)
    if err != nil {
        panic(err)
    }

    prettyJSON, err := ron.ToJSON(ronBody, ron.PrettyJSON("", "  "))
    if err != nil {
        panic(err)
    }

    prettyRON, err := ron.FromJSON([]byte(`{"find":["?id","?name"]}`), ron.Indent("  "))
    if err != nil {
        panic(err)
    }

    compactRON, err := ron.FromJSONCompact([]byte(`{"find":["?id","?name"]}`))
    if err != nil {
        panic(err)
    }

    fmt.Print(string(compactJSON))
    fmt.Print(string(prettyJSON))
    fmt.Print(string(prettyRON))
    fmt.Print(string(compactRON))
}

For repeated conversions, reuse a bytes.Buffer:

var buf bytes.Buffer
jsonBody, err := ron.ToJSONInto(&buf, ronBody)
if err != nil {
    panic(err)
}
println(string(jsonBody))
buf.Reset()

ronBody, err = ron.FromJSONCompactInto(&buf, jsonBody)
if err != nil {
    panic(err)
}
println(string(ronBody))
buf.Reset()

Pretty JSON-to-RON renders root object members directly and can map JSON values to tagged RON values:

ronBody, err := ron.FromJSON(
    []byte(`{"tx":"tx-48830","committed":"2026-06-13T00:00:00Z"}`),
    ron.IsCanonical(false),
    ron.MapJSONValues(func(path []ron.JSONPathSegment, value any) (any, bool) {
        if len(path) != 1 || path[0].IsIndex {
            return nil, false
        }
        switch path[0].Key {
        case "tx":
            return ron.Tagged("", value), true
        case "committed":
            return ron.Tagged("time", value), true
        default:
            return nil, false
        }
    }),
)
if err != nil {
    panic(err)
}
fmt.Print(string(ronBody))

Output:

tx {# tx-48830}
committed {#time 2026-06-13T00:00:00Z}

Supported typed vocabularies are enabled by default. Matching tagged values validate and map to native Go values, while unsupported tags remain ordinary RON objects:

ronBody, err := ron.FromJSON(
    []byte(`{"id":{"#uid":"00112233-4455-6677-8899-aabbccddeeff"}}`),
)

See typed vocabularies for supported vocabulary pages, Go type mappings, and external libraries. VocabularyCoreV1 supports #uid, #url, #dec, #b64, #sha256, #, and #tag.

Conformance

Conformance tests use the reference corpus from github.com/starfederation/ron through flake.nix.

Run with Nix:

nix flake check

nix develop creates a local testdata symlink to the pinned flake input, so plain Go tests work inside the shell:

nix develop
go test ./...

Without Nix, set RON_TESTDATA_DIR=/path/to/ron/testdata or provide local testdata/conformance, testdata/rfc8785, and testdata/vocabularies directories. Otherwise testdata-backed tests are skipped.

To update to the latest reference corpus:

nix flake update ron
nix flake check

Commit flake.lock after the check passes.

Why no git submodule?

Submodules make every clone responsible for git submodule update --init. The flake input pins the reference corpus in flake.lock and gives reproducible Nix checks without vendoring fixture files into this repo.

Documentation

Overview

Package ron converts between RON, Readable Object Notation, and JSON.

Index

Constants

View Source
const (
	EulerOrderXYZ = ronmath.EULER_ORDER_XYZ
	EulerOrderYXZ = ronmath.EULER_ORDER_YXZ
	EulerOrderZXY = ronmath.EULER_ORDER_ZXY
	EulerOrderZYX = ronmath.EULER_ORDER_ZYX
	EulerOrderYZX = ronmath.EULER_ORDER_YZX
	EulerOrderXZY = ronmath.EULER_ORDER_XZY
)
View Source
const (
	// VocabularyColorV1 is the RON color typed vocabulary URI.
	VocabularyColorV1 = "https://ron.dev/vocab/color/v1"
)
View Source
const (
	// VocabularyCoreV1 is the RON core typed vocabulary URI.
	VocabularyCoreV1 = "https://ron.dev/vocab/core/v1"
)
View Source
const (
	// VocabularyGeoV1 is the RON GeoJSON typed vocabulary URI.
	VocabularyGeoV1 = "https://ron.dev/vocab/geo/v1"
)
View Source
const (
	// VocabularyMathV1 is the RON math typed vocabulary URI.
	VocabularyMathV1 = "https://ron.dev/vocab/math/v1"
)
View Source
const (
	// VocabularyNetworkV1 is the RON network typed vocabulary URI.
	VocabularyNetworkV1 = "https://ron.dev/vocab/network/v1"
)
View Source
const (
	// VocabularySpatialV1 is the RON spatial typed vocabulary URI.
	VocabularySpatialV1 = "https://ron.dev/vocab/spatial/v1"
)
View Source
const (
	// VocabularyTimeV1 is the RON time typed vocabulary URI.
	VocabularyTimeV1 = "https://ron.dev/vocab/time/v1"
)

Variables

This section is empty.

Functions

func FromJSON

func FromJSON(src []byte, options ...Option) ([]byte, error)

FromJSON converts JSON to pretty RON unless compact output is requested.

func FromJSONCompact

func FromJSONCompact(src []byte) ([]byte, error)

FromJSONCompact converts JSON to compact RON.

func FromJSONCompactInto

func FromJSONCompactInto(dst *bytes.Buffer, src []byte) ([]byte, error)

FromJSONCompactInto appends JSON converted to compact RON to dst.

func FromJSONInto

func FromJSONInto(dst *bytes.Buffer, src []byte, options ...Option) ([]byte, error)

FromJSONInto appends JSON converted to RON to dst.

func MarshalCompact

func MarshalCompact(value any) ([]byte, error)

MarshalCompact returns value as compact RON.

func Tagged added in v0.0.2

func Tagged(tag string, value any) map[string]any

Tagged returns a single-member object that renders as a tagged RON form. An empty tag renders as #. Payload validation is left to application-specific typed hooks.

func ToJSON

func ToJSON(src []byte, options ...Option) ([]byte, error)

ToJSON converts RON to compact JSON unless pretty output is requested.

func ToJSONInto

func ToJSONInto(dst *bytes.Buffer, src []byte, options ...Option) ([]byte, error)

ToJSONInto appends RON converted to JSON to dst.

func ValidateVocabularyProfile added in v0.0.3

func ValidateVocabularyProfile(src []byte, options ...Option) error

ValidateVocabularyProfile rejects required vocabularies that ron-go does not support.

Types

type Box2 added in v0.0.3

type Box2 = orb.Bound

Box2 is a spatial vocabulary #bx2 value.

type Box3 added in v0.0.3

type Box3 = ronmath.Box3[float64]

Box3 is a spatial vocabulary #bx3 value.

type Bytes added in v0.0.3

type Bytes []byte

Bytes is a core vocabulary #b64 value decoded from base64url without padding.

type CIDR added in v0.0.3

type CIDR struct {
	Prefix netip.Prefix
}

CIDR is a network vocabulary #cdr value.

type Color added in v0.0.3

type Color struct {
	Space    ColorSpace
	Channels []float64
	Value    colorlib.Color
}

Color is a color vocabulary #clr value.

func NewOKLCHColor added in v0.0.3

func NewOKLCHColor(lightness, chroma, hue float64) Color

func (Color) OKLCH added in v0.0.3

func (c Color) OKLCH() *colorlib.OKLCH

type ColorSpace added in v0.0.3

type ColorSpace string

ColorSpace names a color vocabulary color space.

const (
	ColorSpaceRGB    ColorSpace = "rgb"
	ColorSpaceRGBA   ColorSpace = "rgba"
	ColorSpaceHSL    ColorSpace = "hsl"
	ColorSpaceHSLA   ColorSpace = "hsla"
	ColorSpaceHSV    ColorSpace = "hsv"
	ColorSpaceHSVA   ColorSpace = "hsva"
	ColorSpaceHWB    ColorSpace = "hwb"
	ColorSpaceHWBA   ColorSpace = "hwba"
	ColorSpaceLAB    ColorSpace = "lab"
	ColorSpaceLABA   ColorSpace = "laba"
	ColorSpaceLCH    ColorSpace = "lch"
	ColorSpaceLCHA   ColorSpace = "lcha"
	ColorSpaceOKLAB  ColorSpace = "oklab"
	ColorSpaceOKLABA ColorSpace = "oklaba"
	// ColorSpaceOKLCH is the canonical color vocabulary color space.
	ColorSpaceOKLCH  ColorSpace = "oklch"
	ColorSpaceOKLCHA ColorSpace = "oklcha"
	ColorSpaceXYZ    ColorSpace = "xyz"
	ColorSpaceXYZA   ColorSpace = "xyza"
)

type CustomParseFunc added in v0.0.3

type CustomParseFunc func(tag string, payload any) (any, error)

CustomParseFunc validates and maps a custom typed payload.

type CustomRenderFunc added in v0.0.3

type CustomRenderFunc func(value any) (tag string, payload any, ok bool)

CustomRenderFunc maps a native value to a custom typed tag and payload.

type CustomValue added in v0.0.3

type CustomValue struct {
	Tag     string
	Payload any
}

CustomValue is a generic custom typed value.

func Custom added in v0.0.3

func Custom(tag string, payload any) CustomValue

Custom returns a generic custom typed value.

type CustomVocabulary added in v0.0.3

type CustomVocabulary struct {
	URI    string
	Tags   []string
	Parse  CustomParseFunc
	Render CustomRenderFunc
}

CustomVocabulary defines an option-scoped typed vocabulary.

type Cylindrical added in v0.0.3

type Cylindrical = ronmath.Cylindrical[float64]

Cylindrical is a spatial vocabulary #cyl value.

type Decimal added in v0.0.3

type Decimal = apd.Decimal

Decimal is a core vocabulary #dec arbitrary-precision decimal.

type Duration added in v0.0.3

type Duration = time.Duration

Duration is a time vocabulary #dur value.

type EntityRef added in v0.0.3

type EntityRef struct {
	Value any
}

EntityRef is a core vocabulary # value for integer or string entity references.

type Euler added in v0.0.3

type Euler = ronmath.Euler[float64]

Euler is a math vocabulary #eul value.

type EulerOrder added in v0.0.3

type EulerOrder = ronmath.EulerOrder

EulerOrder is the rotation order for a math vocabulary #eul value.

type Float64 added in v0.0.3

type Float64 float64

Float64 is a math vocabulary #f64 value.

type Frustum added in v0.0.3

type Frustum [6]Plane

Frustum is a spatial vocabulary #fru value.

type GeoJSON added in v0.0.3

type GeoJSON = rongeo.Value

GeoJSON is a geo vocabulary #geo value.

type IPv4 added in v0.0.3

type IPv4 struct {
	Addr netip.Addr
}

IPv4 is a network vocabulary #ip4 value.

type IPv6 added in v0.0.3

type IPv6 struct {
	Addr netip.Addr
}

IPv6 is a network vocabulary #ip6 value.

type Instant added in v0.0.3

type Instant = time.Time

Instant is a time vocabulary #utc value.

type Int64 added in v0.0.3

type Int64 int64

Int64 is a math vocabulary #i64 value.

type IntVector2 added in v0.0.3

type IntVector2 [2]int64

IntVector2 is a math vocabulary #iv2 value.

type IntVector3 added in v0.0.3

type IntVector3 [3]int64

IntVector3 is a math vocabulary #iv3 value.

type IntVector4 added in v0.0.3

type IntVector4 [4]int64

IntVector4 is a math vocabulary #iv4 value.

type IntVectorN added in v0.0.3

type IntVectorN []int64

IntVectorN is a math vocabulary #ivN value.

type JSONPathSegment added in v0.0.2

type JSONPathSegment struct {
	Key     string
	Index   int
	IsIndex bool
}

JSONPathSegment identifies one object key or array index in a decoded JSON value.

type Line2 added in v0.0.3

type Line2 struct {
	Line orb.LineString
}

Line2 is a spatial vocabulary #ln2 value.

type Line3 added in v0.0.3

type Line3 = ronmath.Line3[float64]

Line3 is a spatial vocabulary #ln3 value.

type LngLatAlt added in v0.0.3

type LngLatAlt struct {
	Point    orb.Point
	Altitude float64
}

LngLatAlt is a spatial vocabulary #lla value.

type Matrix2 added in v0.0.3

type Matrix2 [4]float64

Matrix2 is a math vocabulary #m2x value.

type Matrix3 added in v0.0.3

type Matrix3 = ronmath.Matrix3[float64]

Matrix3 is a math vocabulary #m3x value.

type Matrix4 added in v0.0.3

type Matrix4 = ronmath.Matrix4[float64]

Matrix4 is a math vocabulary #m4x value.

type OpaqueTag added in v0.0.3

type OpaqueTag struct {
	Tag     any
	Payload any
}

OpaqueTag is a core vocabulary #tag value with implementation-defined payload.

type Option

type Option func(*optionState)

Option configures RON and JSON conversion.

func EnableVocabularies added in v0.0.3

func EnableVocabularies(uris ...string) Option

EnableVocabularies enables validation for supported typed vocabulary URIs. Supported vocabularies are enabled by default; use this for explicit profiles. Unsupported typed values remain ordinary JSON/RON objects unless their vocabulary is enabled.

func Indent

func Indent(indent string) Option

Indent sets the pretty RON indentation string.

func IsCanonical

func IsCanonical(canonical bool) Option

IsCanonical selects RFC 8785 UTF-16 object key ordering when true. When false, source object order is preserved when available; unordered Go maps use canonical order.

func IsPretty

func IsPretty(pretty bool) Option

IsPretty selects multiline pretty output when true or compact output when false.

func MapJSONValues added in v0.0.2

func MapJSONValues(mapper func(path []JSONPathSegment, value any) (any, bool)) Option

MapJSONValues transforms decoded JSON values before JSON-to-RON rendering.

func PrettyJSON

func PrettyJSON(prefix, indent string) Option

PrettyJSON enables indented JSON output.

func UseCustomVocabulary added in v0.0.3

func UseCustomVocabulary(vocabulary CustomVocabulary) Option

UseCustomVocabulary enables an option-scoped custom vocabulary.

type Plane added in v0.0.3

type Plane = ronmath.Plane[float64]

Plane is a spatial vocabulary #pln value.

type Quaternion added in v0.0.3

type Quaternion = ronmath.Quaternion[float64]

Quaternion is a math vocabulary #qat value.

type Ray added in v0.0.3

type Ray = ronmath.Ray[float64]

Ray is a spatial vocabulary #ray value.

type SHA256 added in v0.0.3

type SHA256 [32]byte

SHA256 is a core vocabulary #sha256 value.

type Sphere added in v0.0.3

type Sphere = ronmath.Sphere[float64]

Sphere is a spatial vocabulary #spr value.

type Spherical added in v0.0.3

type Spherical = ronmath.Spherical[float64]

Spherical is a spatial vocabulary #sph value.

type SphericalHarmonics3 added in v0.0.3

type SphericalHarmonics3 = ronmath.SphericalHarmonics3[float64]

SphericalHarmonics3 is a spatial vocabulary #sh3 value.

type Triangle added in v0.0.3

type Triangle = ronmath.Triangle[float64]

Triangle is a spatial vocabulary #tri value.

type UUID added in v0.0.3

type UUID = uuid.UUID

UUID is a core vocabulary #uid value.

type Uint64 added in v0.0.3

type Uint64 uint64

Uint64 is a math vocabulary #u64 value.

type Vector2 added in v0.0.3

type Vector2 = ronmath.Vector2[float64]

Vector2 is a math vocabulary #f2v value.

type Vector3 added in v0.0.3

type Vector3 = ronmath.Vector3[float64]

Vector3 is a math vocabulary #f3v value.

type Vector4 added in v0.0.3

type Vector4 [4]float64

Vector4 is a math vocabulary #f4v value.

type VectorN added in v0.0.3

type VectorN []float64

VectorN is a math vocabulary #vN value.

type VocabularyProfile added in v0.0.3

type VocabularyProfile struct {
	Vocabularies map[string]bool `json:"vocabularies"`
}

VocabularyProfile declares required and optional typed vocabularies.

type VoxelCell added in v0.0.3

type VoxelCell struct {
	Coordinate []int64
	Value      any
}

VoxelCell is one sparse #vox coordinate/value pair.

type VoxelSet added in v0.0.3

type VoxelSet struct {
	Dimensions int64
	Origin     VectorN
	CellSize   VectorN
	Cells      []VoxelCell
}

VoxelSet is a spatial vocabulary #vox value.

Directories

Path Synopsis
components
geo

Jump to

Keyboard shortcuts

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