Tiny JSON Stringify library for Go. Built to have a way to stringify interfaces in WebAssembly applications (it's .wasm
file is 7.5kb
).
When building applications that use Go and WebAssembly I often want to be able to send complicated results back to the client.
There's two problems that occur when trying to do this:
- WebAssembly only supports numbers (int32, int64, float32, and float64). This can be solved by sending back the pointer + length of the string and retreiving it from memory.
tinygo
which is my complier of choice for small WASM binaries doesn't support theencoding/json
package. Even if it did it would add a fair bit of weight to the binary.
This package aims to solve problem number 2 in the most lightweight way possible (no imported libraries).
go get github.com/kvendrik/stringify
package main
import (
"fmt"
"github.com/kvendrik/stringify"
)
func main() {
fmt.Println(stringify.Interface(map[string]interface{}{
"name": "John Doe",
"age": 26,
"data": map[string]interface{}{
"city": "New York",
"state": "NY",
},
"list": []interface{}{23, "NY", 25},
}))
fmt.Println(stringify.Array([]interface{}{23, "NY", 25}))
}
Yields
{
"name": "John Doe",
"age": 26,
"data": { "city": "New York", "state": "NY" },
"list": [23, "NY", 25]
}
[23, "NY", 25]
make run_demo
- Support strings
- Support interfaces
- Support nested interfaces
- Support numbers
- Support arrays
- Support floats