Skip to content

yyle88/neatjson

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

neatjson

neatjson makes JSON encoding in Golang clean and simple.


CHINESE README

δΈ­ζ–‡θ―΄ζ˜Ž

Main Features

🎨 Flexible Indentation Options: Choose from TAB, spaces (0-4), or custom indentation styles ⚑ Error Handling Modes: Must (panic), Soft (log), or Omit (silent) - pick the mode that fits context πŸ”„ Bidirectional Formatting: Format both Go structures and raw JSON data (strings/bytes) πŸ“¦ Convenience Wrappers: Auto-generated packages with sensible defaults for rapid development πŸ› οΈ Type-Safe API: Clean, chainable interface with compile-time safety

Installation

go get github.com/yyle88/neatjson

Usage

Convert Struct to Formatted JSON

package main

import (
	"fmt"

	"github.com/yyle88/neatjson/neatjsons"
)

func main() {
	// Convert struct to formatted JSON string
	data := map[string]any{
		"name":  "example",
		"count": 42,
	}

	result := neatjsons.S(data)
	fmt.Println(result)
}

⬆️ Source: Source

Format Compact JSON Bytes

package main

import (
	"fmt"

	"github.com/yyle88/neatjson/neatjsons"
)

func main() {
	// Format compact JSON bytes to readable string
	jsonBytes := []byte(`{"name":"test","age":30,"active":true}`)

	result := neatjsons.SxB(jsonBytes)
	fmt.Println(result)
}

⬆️ Source: Source

Examples

Convenience Packages

neatjsons - Must mode:

import "github.com/yyle88/neatjson/neatjsons"
json := neatjsons.S(data)  // panic on error

neatjsono - Omit mode:

import "github.com/yyle88/neatjson/neatjsono"
json := neatjsono.S(data)  // silent on error

neatjsonz - Soft mode:

import "github.com/yyle88/neatjson/neatjsonz"
json := neatjsonz.S(data)  // log on error

Indentation Options

TAB indentation (default):

result := neatjson.TAB.Must().S(data)

No indentation (compact):

result := neatjson.NOI.Must().S(data)

2-space indentation:

result := neatjson.SP2.Must().S(data)

4-space indentation:

result := neatjson.SP4.Must().S(data)

Error Handling Modes

Must mode - panic on error:

result := neatjson.TAB.Must().S(data)

Soft mode - log error and return empty:

result := neatjson.TAB.Soft().S(data)

Omit mode - silently return empty:

result := neatjson.TAB.Omit().S(data)

Format Raw JSON Data

String to String:

formatted := neatjson.TAB.Must().SxS(`{"compact":"json"}`)

Bytes to Bytes:

formatted := neatjson.SP2.Must().BxB([]byte(`{"raw":"data"}`))

Bytes to String:

formatted := neatjson.TAB.Must().SxB(jsonBytes)

String to Bytes:

formatted := neatjson.SP4.Must().BxS(jsonString)

Convert to JSON Bytes

Go struct to JSON bytes:

type User struct {
	Name    string `json:"name"`
	Mailbox string `json:"mailbox"`
}
user := User{Name: "Alice", Mailbox: "alice@example.com"}
jsonBytes := neatjson.SP4.Must().B(user)

Error Handling with Return Values

Chain with error handling:

result, err := neatjson.TAB.S(complexData)
if err != nil {
	// Handle error
}

πŸ“„ License

MIT License. See LICENSE.


🀝 Contributing

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • πŸ› Found a mistake? Open an issue on GitHub with reproduction steps
  • πŸ’‘ Have a feature idea? Create an issue to discuss the suggestion
  • πŸ“– Documentation confusing? Report it so we can improve
  • πŸš€ Need new features? Share the use cases to help us understand requirements
  • ⚑ Performance issue? Help us optimize through reporting slow operations
  • πŸ”§ Configuration problem? Ask questions about complex setups
  • πŸ“’ Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • πŸ’¬ Feedback? We welcome suggestions and comments

πŸ”§ Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • ⭐ Give GitHub stars if this project helps you
  • 🀝 Share with teammates and (golang) programming friends
  • πŸ“ Write tech blogs about development tools and workflows - we provide content writing support
  • 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! πŸŽ‰πŸŽ‰πŸŽ‰


GitHub Stars

Stargazers

About

neat json make it neat to use "encoding/json" in golang.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published