Documentation
¶
Overview ¶
Package json provides JSON marshal/unmarshal functions with custom naming conventions.
Package json provides JSON marshal/unmarshal functions with custom naming conventions.
Package json provides JSON marshal/unmarshal functions with custom naming conventions.
Example ¶
Example demonstrates basic usage of MarshalKebab and UnmarshalKebab.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
EnableTLS bool
}
cfg := Config{
ServerName: "my-server",
ListenPort: 8080,
EnableTLS: true,
}
// Marshal to JSON with kebab-case
data, err := json.MarshalKebab(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
// Unmarshal from JSON with kebab-case
var loaded Config
if err := json.UnmarshalKebab(data, &loaded); err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded: %+v\n", loaded)
}
Index ¶
- func MarshalKebab(v interface{}) ([]byte, error)
- func MarshalKebabIndent(v interface{}, prefix, indent string) ([]byte, error)
- func MarshalKebabToFile(filename string, v interface{}) error
- func MarshalSnake(v interface{}) ([]byte, error)
- func MarshalSnakeIndent(v interface{}, prefix, indent string) ([]byte, error)
- func MarshalSnakeToFile(filename string, v interface{}) error
- func UnmarshalKebab(data []byte, v interface{}) error
- func UnmarshalKebabFromFile(filename string, v interface{}) error
- func UnmarshalSnake(data []byte, v interface{}) error
- func UnmarshalSnakeFromFile(filename string, v interface{}) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalKebab ¶
MarshalKebab marshals v to JSON bytes using kebab-case for field names that don't have explicit json tags. Fields with explicit json:"name" tags will use the tag value. This produces compact JSON output.
This function uses reflection to transform struct field names to kebab-case before marshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleMarshalKebab demonstrates marshaling with explicit tags.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string `json:"custom_server_name"`
ListenPort int // Will use kebab-case: listen-port
EnableTLS bool `json:"tls_enabled"`
}
cfg := Config{
ServerName: "test-server",
ListenPort: 8080,
EnableTLS: true,
}
data, err := json.MarshalKebab(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
func MarshalKebabIndent ¶
MarshalKebabIndent marshals v to indented JSON bytes using kebab-case for field names that don't have explicit json tags. Each JSON element begins on a new line indented by prefix followed by one or more copies of indent according to the nesting depth.
Example ¶
ExampleMarshalKebabIndent demonstrates marshaling with indentation.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
cfg := Config{
ServerName: "test-server",
ListenPort: 8080,
}
data, err := json.MarshalKebabIndent(cfg, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
func MarshalKebabToFile ¶
MarshalKebabToFile marshals v to a JSON file using kebab-case naming convention. The output is indented with 2 spaces for readability.
Example ¶
ExampleMarshalKebabToFile demonstrates writing JSON to a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
cfg := Config{
ServerName: "file-server",
ListenPort: 9090,
}
// Write to file (indented automatically)
if err := json.MarshalKebabToFile("/tmp/config.json", cfg); err != nil {
log.Fatal(err)
}
fmt.Println("Config written to /tmp/config.json")
}
func MarshalSnake ¶
MarshalSnake marshals v to JSON bytes using snake_case for field names that don't have explicit json tags. Fields with explicit json:"name" tags will use the tag value. This produces compact JSON output.
This function uses reflection to transform struct field names to snake_case before marshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleMarshalSnake demonstrates basic usage of MarshalSnake.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
EnableTLS bool
}
cfg := Config{
ServerName: "my-server",
ListenPort: 8080,
EnableTLS: true,
}
data, err := json.MarshalSnake(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
func MarshalSnakeIndent ¶
MarshalSnakeIndent marshals v to indented JSON bytes using snake_case for field names that don't have explicit json tags. Each JSON element begins on a new line indented by prefix followed by one or more copies of indent according to the nesting depth.
Example ¶
ExampleMarshalSnakeIndent demonstrates marshaling snake_case with indentation.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
cfg := Config{
ServerName: "test-server",
ListenPort: 8080,
}
data, err := json.MarshalSnakeIndent(cfg, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
func MarshalSnakeToFile ¶
MarshalSnakeToFile marshals v to a JSON file using snake_case naming convention. The output is indented with 2 spaces for readability.
Example ¶
ExampleMarshalSnakeToFile demonstrates writing JSON with snake_case to a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
cfg := Config{
ServerName: "file-server",
ListenPort: 9090,
}
// Write to file (indented automatically)
if err := json.MarshalSnakeToFile("/tmp/config-snake.json", cfg); err != nil {
log.Fatal(err)
}
fmt.Println("Config written to /tmp/config-snake.json")
}
func UnmarshalKebab ¶
UnmarshalKebab unmarshals JSON bytes into v, expecting kebab-case field names for fields without explicit json tags. Fields with explicit json:"name" tags will use the tag value.
This function uses reflection to transform struct field names to kebab-case before unmarshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleUnmarshalKebab demonstrates unmarshaling with nested structs.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type ServerSettings struct {
HostName string
ListenPort int
}
type DatabaseConfig struct {
ConnectionString string
MaxConnections int `json:"max_conn"`
}
type AppConfig struct {
ServerSettings ServerSettings
DatabaseConfig DatabaseConfig
}
jsonData := []byte(`{
"server-settings": {
"host-name": "localhost",
"listen-port": 8080
},
"database-config": {
"connection-string": "postgres://localhost",
"max_conn": 10
}
}`)
var cfg AppConfig
if err := json.UnmarshalKebab(jsonData, &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Server: %s:%d\n", cfg.ServerSettings.HostName, cfg.ServerSettings.ListenPort)
fmt.Printf("Database: %s (max %d connections)\n",
cfg.DatabaseConfig.ConnectionString,
cfg.DatabaseConfig.MaxConnections)
}
func UnmarshalKebabFromFile ¶
UnmarshalKebabFromFile unmarshals JSON from a file into v using kebab-case naming convention.
Example ¶
ExampleUnmarshalKebabFromFile demonstrates reading JSON from a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
var cfg Config
if err := json.UnmarshalKebabFromFile("/tmp/config.json", &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded config: %+v\n", cfg)
}
func UnmarshalSnake ¶
UnmarshalSnake unmarshals JSON bytes into v, expecting snake_case field names for fields without explicit json tags. Fields with explicit json:"name" tags will use the tag value.
This function uses reflection to transform struct field names to snake_case before unmarshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleUnmarshalSnake demonstrates unmarshaling with snake_case.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
EnableTLS bool
}
jsonData := []byte(`{
"server_name": "test-server",
"listen_port": 8080,
"enable_tls": true
}`)
var cfg Config
if err := json.UnmarshalSnake(jsonData, &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Server: %s:%d (TLS: %v)\n", cfg.ServerName, cfg.ListenPort, cfg.EnableTLS)
}
func UnmarshalSnakeFromFile ¶
UnmarshalSnakeFromFile unmarshals JSON from a file into v using snake_case naming convention.
Example ¶
ExampleUnmarshalSnakeFromFile demonstrates reading JSON with snake_case from a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/json"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
var cfg Config
if err := json.UnmarshalSnakeFromFile("/tmp/config-snake.json", &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded config: %+v\n", cfg)
}
Types ¶
This section is empty.