json

package module
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 2 Imported by: 0

README

JSON

Project Badges

A single, platform-agnostic JSON codec for Go that optimizes WebAssembly binary size by using zero reflection. It relies on fmt.Fielder for struct encoding/decoding, which is typically generated by ormc.

Architecture

  • Zero Reflection: Uses type switches and fmt.Fielder instead of the reflect package.
  • Platform-Agnostic: Identical behavior on all platforms (WASM, Linux, macOS, etc.).
  • TinyGo Compatible: Optimized for minimal binary size and memory usage.

Usage

Structs

Structs MUST implement fmt.Fielder to be supported. This is normally handled by generating code with ormc.

package main

import (
    "github.com/tinywasm/fmt"
    "github.com/tinywasm/json"
)

// User implements fmt.Fielder (typically via ormc)
type User struct {
    Name string
}

func (u *User) Schema() []fmt.Field {
    return []fmt.Field{{Name: "Name", Type: fmt.FieldText, JSON: "name"}}
}
func (u *User) Values() []any   { return []any{u.Name} }
func (u *User) Pointers() []any { return []any{&u.Name} }

func main() {
    u := User{Name: "Alice"}

    var out string
    if err := json.Encode(&u, &out); err != nil {
        panic(err)
    }
    // out: {"name":"Alice"}

    var result User
    if err := json.Decode(out, &result); err != nil {
        panic(err)
    }
}
Primitives and Collections

Supports string, int, float64, bool, []byte, []string, []int, []any, and map[string]any.

API

Encode(input any, output any) error
  • input: fmt.Fielder, primitives, or known collections.
  • output: *[]byte, *string, or io.Writer.
Decode(input any, output any) error
  • input: []byte, string, or io.Reader.
  • output: fmt.Fielder, *string, *int64, *float64, *bool, *map[string]any, or *[]any.

Contributing

License

See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode added in v0.1.0

func Decode(input any, output any) error

Decode parses JSON into a Go value. input: []byte | string | io.Reader. output: fmt.Fielder | *string | *int64 | *float64 | *bool | *map[string]any | *[]any.

func Encode added in v0.1.0

func Encode(input any, output any) error

Encode converts a Go value to JSON. input: any supported type (fmt.Fielder, primitives, known collections). output: *[]byte | *string | io.Writer.

Types

This section is empty.

Directories

Path Synopsis
benchmarks
clients/stdlib command
web command

Jump to

Keyboard shortcuts

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