Skip to content

go-universal/jsoner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jsoner (Extended GoLang JSON Marshal)

GitHub Tag Go Reference License Go Report Card Contributors Issues

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:

go get github.com/go-universal/jsoner

Usage

Import the package in your Go code:

import "github.com/go-universal/jsoner"

Marshal

Converts the input value (v) into JSON format, excluding specified fields or paths.

func Marshal(v any, exclude ...string) ([]byte, error)
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.

func MarshalIndent(v any, indent string, exclude ...string) ([]byte, error)
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 file for details.