jsoner

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Jsoner

Utility function for marshal json with pattern.

You can filter nested field with path using . separator (e.g. user.address will apply pattern to {"user" : { "address" : {...} } }.

NOTE: use "." as path pattern for root object.

NOTE: you can use "JsonerIndent" method to generate json with indent.

import "github.com/gomig/utils"
type Book struct {
    Title string
    ISBN  string `json:"isbn"`
}
type Author struct {
    Name      string `json:"name"`
    Family    string `json:"family"`
    Age       int    `json:"age,string,omitempty"`
    IsMariage bool   `json:",string"`
    Books     []Book `json:"author_books"`
    Skills    []string
    // ignored fields
    PrivateField string `json:"-"`
    ignored      string
}

john := Author{
    Name: "John",
    Family: "Doe",
    Age: 0,
    IsMariage: false,
    Books: []Book{
        {Title: "Basics Of C", ISBN: "12345"},
        {Title: "Golang", ISBN: "88888"},
    },
    Skills: []string{"Web dev", "System programming", "IOT"},
    PrivateField: "Some private information",
    ignored: "i'm ignored"
}
options := map[string]utils.JsonerOption{
    ".":            {Ignore: []string{"family"}}, // ignore family field from root struct
    "author_books": {Only: []string{"title"}},    // only marshal title field of author books
}
bytes, _ := utils.Jsoner(john, options)
fmt.Println(string(bytes))

/*
{
   "author_books": [
      {
         "Title": "Basics Of C"
      },
      {
         "Title": "Golang"
      }
   ],
   "IsMariage": "false",
   "name": "John",
   "Skills": [
      "Web dev",
      "System programming",
      "IOT"
   ]
}
*/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Jsoner

func Jsoner(v any, opt map[string]JsonerOption) ([]byte, error)

Jsoner marshal json with dynamic ignores

you must pass path of json data as key and options as value. e.g. option to marshal isbn and title of author book, map[string]JsonerOption{"author.book": JsonerOption{Only: []string{"isbn", "title"}}} "." path used for root

func JsonerIndent

func JsonerIndent(v any, indent string, opt map[string]JsonerOption) ([]byte, error)

Types

type JsonerOption

type JsonerOption struct {
	Only   []string
	Ignore []string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址