json

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 3 Imported by: 0

README

JSON

Project Badges

A lightweight JSON wrapper for Go that optimizes WebAssembly binary size. It automatically switches between the standard encoding/json for backends and the browser's native JSON API (via syscall/js) for WASM builds.

Usage

package main

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

type User struct {
    Name    string `json:"name"`   // Included
    secret  string                  // Skipped (private)
    Ignored string `json:"-"`       // Skipped (tagged)
}

func main() {
    data := User{Name: "Alice", secret: "hidden", Ignored: "no"}

    // 1. Encode to *[]byte
    var jsonBytes []byte
    if err := json.Encode(data, &jsonBytes); err != nil {
        panic(err)
    }
    // Output: {"name":"Alice"}

    // 2. Decode from []byte
    var result User
    if err := json.Decode(jsonBytes, &result); err != nil {
        panic(err)
    }
}

API

The API is polymorphic and avoids unnecessary allocations by accepting various input/output types.

Encode(input any, output any) error
  • input: The Go value to encode.
  • output: The destination for the JSON output. Supported types:
    • *[]byte: Writes the JSON bytes to the slice.
    • *string: Writes the JSON string to the pointer.
    • io.Writer: Writes the JSON data to the writer (streaming).
Decode(input any, output any) error
  • input: The source of the JSON data. Supported types:
    • []byte: Reads JSON from the byte slice.
    • string: Reads JSON from the string.
    • io.Reader: Reads JSON from the reader.
  • output: A pointer to the Go value where the decoded data will be stored.

Benchmarks

Binary size comparison using TinyGo and Gzip compression:

Implementation Binary Size (WASM + Gzip)
JSON 27.2 KB
encoding/json (stdlib) 119 KB

For build instructions and detailed benchmarking information, see benchmarks/README.md.

Screenshots

JSON (27.2 KB): JSON Benchmark

Standard Library JSON (119 KB): Stdlib JSON Benchmark


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

func Encode added in v0.1.0

func Encode(input any, output any) error

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