JSON

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.
- 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).
- 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):

Standard Library JSON (119 KB):

License
See LICENSE for details.