gsjson

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 16 Imported by: 1

README

gsjson

JSON utility library for Go — thread-safe in-memory JSON store, file-backed JSON with encryption, and encrypted environment variable JSON store.

Installation

go get github.com/sonnt85/gsjson

Features

  • Ejson — thread-safe in-memory JSON buffer with get/set/delete via dotted key paths (backed by gjson/sjson)
  • Fjson — file-backed JSON with file locking, optional AES encryption/decryption, fsnotify watch, and auto-persist
  • EnvJson — store encrypted JSON inside an environment variable; get/set individual keys transparently
  • Change hooks: register a callback triggered on any mutation
  • Load hooks: transform raw bytes on load (e.g., decrypt)
  • Atomic file writes using lockedfile (lock with timeout)
  • Decode helper: read and decrypt a JSON file to string without creating a full Fjson

Usage

import "github.com/sonnt85/gsjson"

// In-memory JSON store
ej, _ := gsjson.NewGsjson([]byte(`{"name":"alice","age":30}`), nil)
result := ej.Get("name")          // gjson.Result
ej.Set("age", 31)
ej.Delete("name")
fmt.Println(ej.String())

// File-backed JSON (plaintext)
fj, _ := gsjson.NewFjson("/var/data/config.json", nil, true, map[string]interface{}{
    "version": "1.0",
})
fj.Set("version", "2.0")
fj.WriteToFile()

// File-backed JSON (AES-encrypted)
fj, _ := gsjson.NewFjson("/var/data/config.enc", []byte("mypassphrase"), true)
fj.Set("token", "secret")
fj.WriteToFile()

// Environment variable JSON store
ev := gsjson.NewEnvJson("APP_CONFIG", "mypassword")
ev.Setenv("db.host", "localhost")
ev.Setenv("db.port", "5432")
host := ev.Getenv("db.host")

// Global singleton env-json
gsjson.Init()
gsjson.Setenv("key", "value")
val := gsjson.Getenv("key")

API

Ejson (in-memory)
  • NewGsjson(data []byte, hookChange func(), forceInit ...bool) (*Ejson, error) — create from bytes
  • (*Ejson).Get(path string) gjson.Result — get value at dotted path
  • (*Ejson).Set(path string, value interface{}) error — set value
  • (*Ejson).SetRaw(path string, value []byte) error — set raw JSON bytes
  • (*Ejson).Delete(path string) error — remove key
  • (*Ejson).LoadAndDelete(path string) gjson.Result — atomic get-and-delete
  • (*Ejson).LoadOrStore(path string, value any) gjson.Result — get existing or store default
  • (*Ejson).Loads() gjson.Result — parse entire buffer
  • (*Ejson).String() string — return raw JSON string
  • (*Ejson).WriteTo(path string, password []byte) error — write to file (optionally encrypted)
  • (*Ejson).LoadNewData(buffer []byte) error — replace buffer with new valid JSON
  • (*Ejson).SetHookLoad(f func(*[]byte)) — register load transform hook
Fjson (file-backed)
  • NewFjson(path string, passphrase []byte, removeIfInvalid bool, datas ...map[string]interface{}) (*Fjson, error) — open or create JSON file
  • (*Fjson).ReadFile(paths ...string) ([]byte, error) — read and (optionally) decrypt file
  • (*Fjson).WriteToFile() — async persist to file
  • (*Fjson).Remove() error — delete the backing file
  • (*Fjson).GetJsonFilePath() string — return file path
  • (*Fjson).GetPassPhrase() []byte — return passphrase
  • Inherits all Ejson methods
EnvJson (environment variable)
  • NewEnvJson(envkey, pwd string) *EnvJson — create an env-JSON handle
  • (*EnvJson).Setenv/Getenv/Unsetenv/Hasenv(key string) — manage individual keys
  • (*EnvJson).GetOrCreateEnv(key, defaultValue string) string — get or initialize key
  • (*EnvJson).GetDecryptedEnvValue() string — return decrypted JSON string
  • (*EnvJson).Decode(encenv string) string — decrypt an arbitrary encrypted string
Package-level helpers
  • DecodeJsonFile(path string, passphrase []byte) (string, error) — read and decrypt JSON file
  • DecodeJsonFileNoErr(path string, passphrase []byte) string — same, ignoring errors
  • Init() — initialize global singleton EnvJson (env key ENVJSON, password ENVJSON)
  • Setenv/Getenv/Unsetenv/Hasenv/GetOrCreateEnv(...) — delegate to global singleton

Author

sonnt85thanhson.rf@gmail.com

License

MIT License - see LICENSE for details.

Documentation

Overview

Package gsjson provides structure and helper functions for JSON handling

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(encenv string) string

func DecodeJsonFile

func DecodeJsonFile(path string, passphrase []byte) (retstr string, err error)

func DecodeJsonFileNoErr

func DecodeJsonFileNoErr(path string, passphrase []byte) (retstr string)

func GetDecryptedEnvValue

func GetDecryptedEnvValue() string

func GetDecryptedEnvValueThenAddEnv

func GetDecryptedEnvValueThenAddEnv(m map[string]string) (ejsondata string)

func GetEncryptedEnvValue

func GetEncryptedEnvValue() string

func GetEncryptedEnvValueThenAddEnv

func GetEncryptedEnvValueThenAddEnv(m map[string]string) (ejsondata string)

func GetEnvName

func GetEnvName() string

func GetOrCreateEnv

func GetOrCreateEnv(key, defaultValue string) (retstr string)

func Getenv

func Getenv(key string) (value string)

func Hasenv

func Hasenv(key string) bool

func Init

func Init()

func SetEnvFromDecryptedValue

func SetEnvFromDecryptedValue(value string)

func Setenv

func Setenv(key, value string) (err error)

func String

func String() string

func Unsetenv

func Unsetenv(key string) (err error)

Types

type Ejson

type Ejson struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

var default_passphrase = []byte("default_passphrase") Ejson is a file used to store the process ID of a running process.

func NewGsjson

func NewGsjson(datas []byte, hookChange func(), forceInit ...bool) (f *Ejson, err error)

NewGsjson creates a PID file using the specified path.

func (*Ejson) Delete

func (gs *Ejson) Delete(path string) (err error)

Delete deletes the value for a key path.

func (*Ejson) Get

func (gs *Ejson) Get(path string) (result gjson.Result)

get the value for a key.

func (*Ejson) LoadAndDelete

func (gs *Ejson) LoadAndDelete(path string) (value gjson.Result)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Ejson) LoadNewData

func (gs *Ejson) LoadNewData(buffer []byte) (err error)

func (*Ejson) LoadOrStore

func (gs *Ejson) LoadOrStore(path string, value any) (actual gjson.Result)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Ejson) Loads

func (gs *Ejson) Loads() (result gjson.Result)

func (*Ejson) Set

func (gs *Ejson) Set(path string, value interface{}) (err error)

set the value for a key.

func (*Ejson) SetHookLoad

func (gs *Ejson) SetHookLoad(f func(buffer *[]byte))

func (*Ejson) SetRaw

func (gs *Ejson) SetRaw(path string, value []byte) (err error)

set the value for a key.

func (*Ejson) String

func (gs *Ejson) String() string

func (*Ejson) WriteTo

func (gs *Ejson) WriteTo(path string, password []byte) (err error)

type EnvJson

type EnvJson struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func GetGenvjson

func GetGenvjson() *EnvJson

func NewEnvJson

func NewEnvJson(envkey string, pwd string) *EnvJson

func (*EnvJson) Decode

func (dd *EnvJson) Decode(encenv string) string

func (*EnvJson) GetDecryptedEnvValue

func (dd *EnvJson) GetDecryptedEnvValue() string

func (*EnvJson) GetDecryptedEnvValueThenAddEnv

func (dd *EnvJson) GetDecryptedEnvValueThenAddEnv(m map[string]string) (ejsondata string)

func (*EnvJson) GetEncryptedEnvValue

func (dd *EnvJson) GetEncryptedEnvValue() string

func (*EnvJson) GetEncryptedEnvValueThenAddEnv

func (dd *EnvJson) GetEncryptedEnvValueThenAddEnv(m map[string]string) (ejsondata string)

func (*EnvJson) GetEnvName

func (dd *EnvJson) GetEnvName() string

func (*EnvJson) GetOrCreateEnv

func (dd *EnvJson) GetOrCreateEnv(key, defaultValue string) (retstr string)

func (*EnvJson) Getenv

func (dd *EnvJson) Getenv(key string) (value string)

func (*EnvJson) Hasenv

func (dd *EnvJson) Hasenv(key string) bool

func (*EnvJson) SetEnvFromDecryptedValue

func (dd *EnvJson) SetEnvFromDecryptedValue(value string)

func (*EnvJson) Setenv

func (dd *EnvJson) Setenv(key, value string) (err error)

func (*EnvJson) String

func (dd *EnvJson) String() string

func (*EnvJson) Unsetenv

func (dd *EnvJson) Unsetenv(key string) (err error)

type Fjson

type Fjson struct {
	RmdirFlag bool `json:"RmdirFlag"`
	ForceInit bool

	*Ejson
	// contains filtered or unexported fields
}

func NewFjson

func NewFjson(path string, passphrase []byte, removeIfFileInvalid bool, datas ...map[string]interface{}) (f *Fjson, err error)

New creates a Fjson file using the specified path.

func (*Fjson) GetJsonFilePath

func (f *Fjson) GetJsonFilePath() string

func (*Fjson) GetPassPhrase

func (f *Fjson) GetPassPhrase() []byte

func (*Fjson) ReadFile

func (f *Fjson) ReadFile(paths ...string) (jsonByte []byte, err error)

func (*Fjson) Remove

func (f *Fjson) Remove() (err error)

Remove pid file

func (*Fjson) SendCmd

func (f *Fjson) SendCmd(cmd int)

func (*Fjson) WriteToFile

func (f *Fjson) WriteToFile()

Jump to

Keyboard shortcuts

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