json2struct

package
v0.0.0-...-a528bbb Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 10 Imported by: 0

README

json2struct

Translate to Golang struct from json

Playground

Try playground https://yudppp.github.io/json2struct

CLI

Install
$ go get github.com/yudppp/json2struct/cmd/json2struct
How to use
$ echo '{"url": "http://blog.yudppp.com", "text": "Hello:)", "status": 1, "categories": [{"name": "k8s"}]}' |  json2struct -name=blog
type Blog struct {
	Categories []BlogCategory `json:"categories"`
	Status     int            `json:"status"`
	Text       string         `json:"text"`
	URL        string         `json:"url"`
}

type BlogCategory struct {
	Name string `json:"name"`
}
options
option description
name Set struct name (default "data")
prefix Set struct name prefix
suffix Set struct name suffix
short Set short struct name mode
local Use local struct mode
omitempty Set omitempty mode
example Use example tag (https://github.com/yudppp/structs)

Documentation

Index

Constants

View Source
const (
	DefaultStructName = "data"
)

Variables

This section is empty.

Functions

func Parse

func Parse(reader io.Reader, opt Options) (string, error)

func SetDebug

func SetDebug(v bool)

func SpathToName

func SpathToName(spath, name string) string

Types

type ObjectType

type ObjectType uint
const (
	Invalid ObjectType = iota
	Value
	Hash
	Array
)

type Options

type Options struct {
	UseOmitempty   bool
	UseShortStruct bool
	UseLocal       bool
	UseExample     bool
	Name           string
	Prefix         string
	Suffix         string
}

type Propety

type Propety struct {
	Name  string
	Kind  reflect.Kind
	Value interface{}
	Refs  *Structure `json:",omitempty"`
}

func (*Propety) String

func (p *Propety) String() string

type Props

type Props []Propety

func (Props) Len

func (p Props) Len() int

func (Props) Less

func (p Props) Less(i, j int) bool

func (Props) Swap

func (p Props) Swap(i, j int)

type Structure

type Structure struct {
	ID    string
	Name  string
	Props Props
}

func NewStructure

func NewStructure(spath, name string) *Structure

func (*Structure) AddPropety

func (s *Structure) AddPropety(name string, kind reflect.Kind, val interface{}, refs *Structure)

func (*Structure) Output

func (s *Structure) Output() []string

func (*Structure) Refs

func (s *Structure) Refs() []*Structure

func (*Structure) String

func (s *Structure) String() string

type Walker

type Walker struct {
	// contains filtered or unexported fields
}

func NewWalker

func NewWalker(root interface{}) *Walker

Jump to

Keyboard shortcuts

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