j2s

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(s string) (string, error)

Convert uses the default options and returns a string of JSON strings converted to Go structures.

The default values are as follows.

Option {
    UseTag:    true,
    TagName:   "json",
    Omitempty: OmitemptyNone,
}

return an error if the string is invalid as JSON.

Example
package main

import (
	"fmt"

	"github.com/masakurapa/go-json2struct/pkg/j2s"
)

func main() {
	input := `{
		"title": "j2s",
		"snake_case": 99,
		"CamelCase": true,
		"kebab-case": null,
		"map": {"child1": "apple", "child2": 12345},
		"array": ["1", "2", "3", "4", "5"]
	}`

	output, err := j2s.Convert(input)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(output)

}
Output:

type J2S1 struct {
	CamelCase bool        `json:"CamelCase"`
	Array     []string    `json:"array"`
	KebabCase interface{} `json:"kebab-case"`
	Map       J2S2        `json:"map"`
	SnakeCase int         `json:"snake_case"`
	Title     string      `json:"title"`
}

type J2S2 struct {
	Child1 string `json:"child1"`
	Child2 int    `json:"child2"`
}

func ConvertWithOption added in v0.3.0

func ConvertWithOption(s string, opt Option) (string, error)

ConvertWithOption uses the specified options and returns a string that is a JSON string converted to a Go structure.

return an error if the string is invalid as JSON.

Types

type Omitempty added in v0.3.0

type Omitempty int

Omitempty is an optional type that outputs "omitempty"

const (
	// OmitemptyNone does not always output "omitempty"
	OmitemptyNone Omitempty = iota
	// OmitemptyAlways always output "omitempty"
	OmitemptyAlways
	// OmitemptyPtrOnly outputs "omitempty" only if it is a pointer type
	OmitemptyPtrOnly
)

type Option added in v0.3.0

type Option struct {
	// UseTag outputs tag if true
	UseTag bool

	// TagName is a tag name. (default is "json")
	//
	// if empty, the default value is used.
	//
	// if "UseTag" is false, the value is not used.
	TagName string

	// Omitempty is an optional type that outputs "omitempty". (default is non output)
	Omitempty Omitempty
}

Option is an option to customize output results

Jump to

Keyboard shortcuts

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