46 lines
879 B
Go
46 lines
879 B
Go
//go:build js && wasm
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"syscall/js"
|
|
)
|
|
|
|
func main() {
|
|
js.Global().Set("formatJSON", jsonWrapper())
|
|
fmt.Println("loaded golang wasm")
|
|
<-make(chan struct{})
|
|
}
|
|
|
|
func prettyJson(input string) (string, error) {
|
|
var raw any
|
|
if err := json.Unmarshal([]byte(input), &raw); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
pretty, err := json.MarshalIndent(raw, "", " ")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(pretty), nil
|
|
}
|
|
|
|
func jsonWrapper() js.Func {
|
|
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
|
|
if len(args) != 1 {
|
|
return "Invalid no of arguments passed"
|
|
}
|
|
inputJSON := args[0].String()
|
|
fmt.Printf("input %s\n", inputJSON)
|
|
pretty, err := prettyJson(inputJSON)
|
|
if err != nil {
|
|
fmt.Printf("unable to convert to json %s\n", err)
|
|
return err.Error()
|
|
}
|
|
return pretty
|
|
})
|
|
return jsonFunc
|
|
}
|