Documentation
¶
Overview ¶
json package is a goschtalt decoder package.
The json package automatically registers the decoder as a default decoder with the goschtalt package so the usage is as simple as possible ... simply import the package and it should just work.
Import the package like you do for pprof - like this:
import ( "fmt" "os" ... "github.com/goschtalt/goschtalt" _ "github.com/goschtalt/goschtalt/extensions/decoders/json" )
See the example for how to use this extension package.
Example ¶
// SPDX-FileCopyrightText: 2022 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"io/fs"
"strings"
"github.com/goschtalt/goschtalt"
_ "github.com/goschtalt/json-decoder"
"github.com/psanford/memfs"
)
const filename = `example.json`
const text = `{
"example": {
"version": 1,
"colors": ["red", "green", "blue"]
}
}`
func getFS() fs.FS {
mfs := memfs.New()
if err := mfs.WriteFile(filename, []byte(text), 0755); err != nil {
panic(err)
}
return mfs
}
func main() {
// Normally, you use something like os.DirFS("/etc/program")
g, err := goschtalt.New(goschtalt.AddDir(getFS(), "."))
if err != nil {
panic(err)
}
err = g.Compile()
if err != nil {
panic(err)
}
var cfg struct {
Example struct {
Version int
Colors []string
}
}
err = g.Unmarshal("", &cfg)
if err != nil {
panic(err)
}
fmt.Println("example")
fmt.Printf(" version = %d\n", cfg.Example.Version)
fmt.Printf(" colors = [ %s ]\n", strings.Join(cfg.Example.Colors, ", "))
}
Output: example version = 1 colors = [ red, green, blue ]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
Click to show internal directories.
Click to hide internal directories.