https://github.com/go-universal/jsoner
Flexible JSON encoder for Go.
https://github.com/go-universal/jsoner
golang json json-marshalling
Last synced: 2 months ago
JSON representation
Flexible JSON encoder for Go.
- Host: GitHub
- URL: https://github.com/go-universal/jsoner
- Owner: go-universal
- License: isc
- Created: 2025-04-07T11:41:37.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-07T11:55:54.000Z (about 1 year ago)
- Last Synced: 2025-04-10T00:08:31.985Z (about 1 year ago)
- Topics: golang, json, json-marshalling
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jsoner (Extended GoLang JSON Marshal)

[](https://pkg.go.dev/github.com/go-universal/jsoner)
[](https://github.com/go-universal/jsoner/blob/main/LICENSE)
[](https://goreportcard.com/report/github.com/go-universal/jsoner)


`jsoner` is a Go package that provides utilities for marshaling Go structs into JSON with advanced filtering capabilities. It allows you to exclude specific fields or nested paths from the JSON output.
## Installation
To install the package, run:
```bash
go get github.com/go-universal/jsoner
```
## Usage
Import the package in your Go code:
```go
import "github.com/go-universal/jsoner"
```
### Marshal
Converts the input value (`v`) into JSON format, excluding specified fields or paths.
```go
func Marshal(v any, exclude ...string) ([]byte, error)
```
```go
type Book struct {
Title string
ISBN string `json:"isbn"`
}
type Author struct {
Name string `json:"name"`
Family string `json:"family"`
Books []Book `json:"author_books"`
}
author := Author{
Name: "John",
Family: "Doe",
Books: []Book{
{Title: "Book 1", ISBN: "12345"},
{Title: "Book 2", ISBN: "67890"},
},
}
jsonData, err := jsoner.Marshal(author, "family", "author_books.isbn")
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
// Output: {"name":"John","author_books":[{"Title":"Book 1"},{"Title":"Book 2"}]}
```
### MarshalIndent
Converts the input value (`v`) into indented JSON format, excluding specified fields or paths.
```go
func MarshalIndent(v any, indent string, exclude ...string) ([]byte, error)
```
```go
type Book struct {
Title string
ISBN string `json:"isbn"`
}
type Author struct {
Name string `json:"name"`
Family string `json:"family"`
Books []Book `json:"author_books"`
}
author := Author{
Name: "John",
Family: "Doe",
Books: []Book{
{Title: "Book 1", ISBN: "12345"},
{Title: "Book 2", ISBN: "67890"},
},
}
jsonData, err := jsoner.MarshalIndent(author, " ", "family", "author_books.isbn")
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
// Output:
// {
// "name": "John",
// "author_books": [
// {
// "Title": "Book 1"
// },
// {
// "Title": "Book 2"
// }
// ]
// }
```
## License
This project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.