superjson

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2023 License: Apache-2.0 Imports: 4 Imported by: 1

README

super json

Build Status

JSON picker and converter.

API

Pick

buf := []byte(`{
  "name": "tree.xie",
  "address": "GZ",
  "no": 123
}`)
data := superjson.Pick(buf, []string{
  "name",
  "no",
})
// {"name":"tree.xie","no":123}
fmt.Println(string(data))

Omit

buf := []byte(`{
  "name": "tree.xie",
  "address": "GZ",
  "no": 123
}`)
data := superjson.Omit(buf, []string{
  "address",
})
// {"name":"tree.xie","no":123}
fmt.Println(string(data))

Filter

buf := []byte(`{
  "name": "tree.xie",
  "address": "GZ",
  "no": 123
}`)
data := superjson.Filter(buf, func(key, _ string) (omit bool, newKey string) {
  // omit the no
  if key == "no" {
    return true, ""
  }
  // convert the address to addr
  if key == "address" {
    return false, "addr"
  }
  // key original
  return false, key
})
// {"name":"tree.xie","addr":"GZ"}
fmt.Println(string(data))

Mask

buf := []byte(`{
  "name": "tree.xie",
  "address": "GZ",
  "no": 123
}`)
data := superjson.Mask(buf, func(key, _ string) (newValue string) {
  // mask the no
  if key == "no" {
    return `"***"`
  }
  return ""
})
// {"name":"tree.xie","address":"GZ","no":"***"}
fmt.Println(string(data))

CamelCase

buf := []byte(`{
	"book_author_name": "tree.xie",
	"book_no": 123
}`)
data := superjson.CamelCase(buf)
// {"bookAuthorName":"tree.xie","bookNo":123}
fmt.Println(string(data)

SnakeCase

buf := []byte(`{
  "bookAuthorName": "tree.xie",
  "bookNo": 123
}`)
data := superjson.SnakeCase(buf)
// {"book_author_name":"tree.xie","book_no":123}
fmt.Println(string(data))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(buf []byte) []byte

CamelCase convert json to camel case

func Filter added in v0.0.3

func Filter(buf []byte, filter KeyFilter) []byte

Filter json filter

func Mask added in v0.0.4

func Mask(buf []byte, mask ValueMask) []byte

Mask json mask

func Omit

func Omit(buf []byte, fields []string) []byte

Omit omit fields from json

func Pick

func Pick(buf []byte, fields []string) []byte

Pick pick fields from json

func SnakeCase

func SnakeCase(buf []byte) []byte

SnakeCase convert json to snake case

Types

type KeyConvert added in v0.0.3

type KeyConvert func(string) string

KeyConvert key convert function

type KeyFilter added in v0.0.3

type KeyFilter func(key, value string) (omit bool, newKey string)

KeyFilter key filter function

type ValueMask added in v0.0.4

type ValueMask func(key, value string) (newValue string)

ValueMask value mask function

Jump to

Keyboard shortcuts

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