Skip to content

Latest commit

 

History

History

json

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

JSON Parser

Go Version Example Documentation Workflow Status

This parser loads json formatted configuration from an io.ReadCloser.

Example

Click the Playground badge above to see the example running in the Go Playground.

package main

import (
	"fmt"

	"go.krak3n.codes/gofig"
	"go.krak3n.codes/gofig/parsers/json"
)

type Config struct {
	Foo  string `gofig:"foo"`
	Bar  int    `gofig:"bar"`
	Fizz struct {
		Buzz string `gofig:"buzz"`
	} `gofig:"fizz"`
}

const blob = `{
	"foo": "bar",
	"bar": 12,
	"fizz": {
		"buzz": "fizz"
	}
}`

func main() {
	var cfg Config

	// Initialise gofig with the struct config values will be placed into
	gfg, err := gofig.New(&cfg)
	gofig.Must(err)

	// Parse
	gofig.Must(gfg.Parse(gofig.FromString(json.New(), blob)))

	fmt.Println(fmt.Sprintf("%+v", cfg))
}