neatjson makes JSON encoding in Golang clean and simple.
π¨ 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
go get github.com/yylego/neatjsonpackage 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
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
neatjsons - Must mode:
import "github.com/yylego/neatjson/neatjsons"
json := neatjsons.S(data) // panic on failureneatjsono - Omit mode:
import "github.com/yylego/neatjson/neatjsono"
json := neatjsono.S(data) // silent on failureneatjsonz - Soft mode:
import "github.com/yylego/neatjson/neatjsonz"
json := neatjsonz.S(data) // log on failureTAB 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)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)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)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)Chain with exception handling:
result, err := neatjson.TAB.S(complexData)
if err != nil {
// Handle exception
}| 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) |
| 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 |
| Method | Description |
|---|---|
Must() |
Panic on exception |
Soft() |
Log and give blank |
Omit() |
Silent, give blank |
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)MIT License - see LICENSE.
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
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). - Navigate: Navigate to the cloned project (
cd repo-name) - Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes
- Stage: Stage changes (
git add .) - Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
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! πππ