json_to_go

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 4 Imported by: 0

README

JSON-to-GO:将JSON转换成Go结构体定义 【GOLANG实现】

灵感和输出结果来自[json-to-go](https://github.com/mholt/json-to-go)
用法:
// 解析json元信息
meta := Parse(json)
// 根据元信息渲染输出结构体定义
// inline:true/false;结构体内联/分离输出
structDef := GenerateStruct(meta, inline)

安装:

go get github.com/AuroraV/json-to-go

使用样例

在线编辑

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"strings"

	j2g "github.com/AuroraV/json-to-go"
)

func main() {
	meta := j2g.Parse(`[{
	"input_index": 100000001,
	"components": [{
		"primary_number": "1",
		"a": 0.83
	}, {
		"a": 1.01,
		"b": 1.28
	}]
}]`)
	structDef := j2g.GenerateStruct(meta, true)
	structDef = j2g.GenerateStruct(meta, false)
	fmt.Println(strings.Join(structDef, "\n----------------------------------------\n"))
	// output:
	/*
		1. output [inline = true] :
		type AutoGenerated []struct {
			InputIndex int64 `json:"input_index"`
			Components []struct {
				PrimaryNumber string `json:"primary_number,omitempty"`
				A float64 `json:"a"`
				B float64 `json:"b,omitempty"`
			} `json:"components"`
		}

		2. output [inline = false] :
		type AutoGenerated []struct {
				InputIndex int64 `json:"input_index"`
				Components []Components `json:"components"`
		}
		----------------------------------------
		type Components struct {
				PrimaryNumber string `json:"primary_number,omitempty"`
				A float64 `json:"a"`
				B float64 `json:"b,omitempty"`
		}
	*/
}

License

This software is released under the MIT License, see LICENSE.

Documentation

Index

Constants

View Source
const (
	Root = "AutoGenerated"
)

Variables

This section is empty.

Functions

func GenerateStruct

func GenerateStruct(meta meta, inline bool) []string

func Intersect

func Intersect(a []string, b []string) (res []string)

func Parse

func Parse(json string) meta

func ToUpperCamelCase

func ToUpperCamelCase(s string) string

Types

type Kind

type Kind uint
const (
	Invalid Kind = iota
	Bool
	Int64
	Float64
	String
	Struct
	Interface
)

func (Kind) String

func (t Kind) String() string

String returns a string representation of the type.

Jump to

Keyboard shortcuts

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