Skip to content

yylego/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), and custom indentation styles ⚑ Exception Handling Modes: Must (panic), Soft (log), and Omit (silent) - pick the mode that fits context πŸ”„ Struct & JSON Formatting: Format both Go structures and raw JSON data (strings/bytes) πŸ“¦ Convenience Packages: Auto-generated packages with sensible defaults πŸ› οΈ Type-Safe API: Clean, chainable interface with compile-time checks

Installation

go get github.com/yylego/neatjson

Usage

Convert Struct to Formatted JSON

package main

import (
	"fmt"

	"github.com/yylego/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/yylego/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/yylego/neatjson/neatjsons"
json := neatjsons.S(data)  // panic on failure

neatjsono - Omit mode:

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

neatjsonz - Soft mode:

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

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)

Exception Handling Modes

Must mode - panic on exception:

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

Soft mode - log and give blank:

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

Omit mode - silent, give blank:

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)

Exception Handling with Return Values

Chain with exception handling:

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

API Reference

Indentation Constants

Constant Description
TAB Tab indentation
SP0 No indentation
SP1 1-space indentation
SP2 2-space indentation
SP3 3-space indentation
SP4 4-space indentation
NOI Compact JSON (no indent)
NON Compact JSON (no newline)

Neatjson Methods

Method Description
S(v) / Sjson(v) Struct to JSON string
B(v) / Bytes(v) Struct to JSON bytes
SxS(s) Reformat JSON string
BxB(data) Reformat JSON bytes
SxB(data) / B2S(data) JSON bytes to string
BxS(data) / S2B(data) JSON string to bytes

Handling Modes

Method Description
Must() Panic on exception
Soft() Log and give blank
Omit() Silent, give blank

Design Notes

Reformat methods keep source input on exception:

SxS/BxB/SxB/BxS pass back source input plus exception when receiving invalid JSON. No data loss.

invalidJSON := `{"name": "test",}`  // trailing comma - invalid
result, e := neatjson.TAB.SxS(invalidJSON)
// e != nil, result == invalidJSON (source input kept safe)

πŸ“„ License

MIT License - see LICENSE.


πŸ’¬ Contact & Feedback

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

  • πŸ› Mistake reports? Open an issue on GitHub with reproduction steps
  • πŸ’‘ Fresh ideas? Create an issue to discuss
  • πŸ“– 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
  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

Go JSON encoding made clean and compact

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors