json5

package
v3.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package json5 use the https://github.com/yosuke-furukawa/json5 for parse json5

Example
package main

import (
	"fmt"

	"github.com/abulo/ratel/v3/config"
	"github.com/abulo/ratel/v3/config/json5"
)

func main() {
	config.WithOptions(config.ParseEnv)

	// add Decoder and Encoder
	config.AddDriver(json5.Driver)

	err := config.LoadFiles("testdata/json_base.json5")
	if err != nil {
		panic(err)
	}

	fmt.Printf("config data: \n %#v\n", config.Data())

	err = config.LoadFiles("testdata/json_other.json")
	// config.LoadFiles("testdata/json_base.json", "testdata/json_other.json")
	if err != nil {
		panic(err)
	}

	fmt.Printf("config data: \n %#v\n", config.Data())
	fmt.Print("get config example:\n")

	name := config.String("name")
	fmt.Printf("get string\n - val: %v\n", name)

	arr1 := config.Strings("arr1")
	fmt.Printf("get array\n - val: %#v\n", arr1)

	val0 := config.String("arr1.0")
	fmt.Printf("get sub-value by path 'arr.index'\n - val: %#v\n", val0)

	map1 := config.StringMap("map1")
	fmt.Printf("get map\n - val: %#v\n", map1)

	val0 = config.String("map1.key")
	fmt.Printf("get sub-value by path 'map.key'\n - val: %#v\n", val0)

	// can parse env name(ParseEnv: true)
	fmt.Printf("get env 'envKey' val: %s\n", config.String("envKey", ""))
	fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))

	// set value
	_ = config.Set("name", "new name")
	name = config.String("name")
	fmt.Printf("set string\n - val: %v\n", name)

	// if you want export config data
	// buf := new(bytes.Buffer)
	// _, err = config.DumpTo(buf, json5.NAME)
	// if err != nil {
	// 	panic(err)
	// }
	// fmt.Printf("export config:\n%s", buf.String())
}
Output:

Index

Examples

Constants

View Source
const NAME = "json5"

NAME for driver

Variables

View Source
var (
	// Decoder for json
	Decoder config.Decoder = json5.Unmarshal

	// Encoder for json5
	Encoder config.Encoder = func(v interface{}) (out []byte, err error) {
		if len(JSONMarshalIndent) == 0 {
			return json5.Marshal(v)
		}

		return json5.MarshalIndent(v, "", JSONMarshalIndent)
	}

	// Driver for json5
	Driver = config.NewDriver(NAME, Decoder, Encoder)
)
View Source
var JSONMarshalIndent string

JSONMarshalIndent if not empty, will use json.MarshalIndent for encode data.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL