https://github.com/knadh/jsonconfig
Super tiny JSON configuration file parser with comments support for Go programs
https://github.com/knadh/jsonconfig
Last synced: about 1 year ago
JSON representation
Super tiny JSON configuration file parser with comments support for Go programs
- Host: GitHub
- URL: https://github.com/knadh/jsonconfig
- Owner: knadh
- License: mit
- Created: 2015-01-16T19:20:17.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-06-14T06:38:15.000Z (almost 10 years ago)
- Last Synced: 2025-03-19T00:17:59.381Z (about 1 year ago)
- Language: Go
- Size: 3.91 KB
- Stars: 12
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go jsconfig
Kailash Nadh, January 2015
MIT License
## What?
jsconfig is a tiny JSON configuration file parser for Go with support for comments. Really, JSON spec doesn't include comments, but a configuration file without helpful comments is a pain to deal with.
Moreover, JSON for configuration files is powerful when combined with structs, enabling effortless loading of complex, nested data structures with Go's native JSON Unmarshaling.
## Installation (go 1.1+)
`go get github.com/knadh/jsonconfig`
## Usage
### Sample file: config.json
Notice the comments
{
// url to the site
"url": "http://google.com",
"methods": ["GET", "POST"], // supported methods
"always_load": true,
// nested structure with different types
"module": {
"name": "Welcome",
"route": "/",
"port": 8080
}
}
### Loading the configuration
```go
package main
import (
"github.com/knadh/jsonconfig"
)
func main() {
// setup the structure
config := struct {
Url string `json:"url"`
Methods []string `json:"methods"`
AlwaysLoad bool `json:"always_load"`
Module struct{
Name string `json:"name"`
Route string `json:"route"`
Port int `json:"port"`
} `json:"module"`
}{}
// parse and load json config
err := jsonconfig.Load("config.json", &config)
if err == nil {
println("The url is", config.Url)
println("Supported methods are", config.Methods[0], config.Methods[1])
println("The module is", config.Module.Name, "on route", config.Module.Route)
}
}
```