gojson

package
v0.0.0-...-391253a Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: GPL-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package gojson - generates go struct defintions from JSON documents

Reads from stdin and prints to stdout

Output:

package main

type User struct {
	AvatarURL         string      `json:"avatar_url"`
	Bio               interface{} `json:"bio"`
	Blog              string      `json:"blog"`
	Company           string      `json:"company"`
	CreatedAt         string      `json:"created_at"`
	Email             string      `json:"email"`
	EventsURL         string      `json:"events_url"`
	Followers         float64     `json:"followers"`
	FollowersURL      string      `json:"followers_url"`
	Following         float64     `json:"following"`
	FollowingURL      string      `json:"following_url"`
	GistsURL          string      `json:"gists_url"`
	GravatarID        string      `json:"gravatar_id"`
	Hireable          bool        `json:"hireable"`
	HtmlURL           string      `json:"html_url"`
	ID                float64     `json:"id"`
	Location          string      `json:"location"`
	Login             string      `json:"login"`
	Name              string      `json:"name"`
	OrganizationsURL  string      `json:"organizations_url"`
	PublicGists       float64     `json:"public_gists"`
	PublicRepos       float64     `json:"public_repos"`
	ReceivedEventsURL string      `json:"received_events_url"`
	ReposURL          string      `json:"repos_url"`
	StarredURL        string      `json:"starred_url"`
	SubscriptionsURL  string      `json:"subscriptions_url"`
	Type              string      `json:"type"`
	UpdatedAt         string      `json:"updated_at"`
	URL               string      `json:"url"`
}

Package gojson - generates go struct definitions from JSON documents

Reads from stdin and prints to stdout

Example:

curl -s https://api.github.com/repos/chimeracoder/gojson | gojson -name=Repository

Output:

	package main

	type Repository struct {
    	ArchiveURL       string      `json:"archive_url"`
    	AssigneesURL     string      `json:"assignees_url"`
    	BlobsURL         string      `json:"blobs_url"`
    	BranchesURL      string      `json:"branches_url"`
    	CloneURL         string      `json:"clone_url"`
    	CollaboratorsURL string      `json:"collaborators_url"`
    	CommentsURL      string      `json:"comments_url"`
    	CommitsURL       string      `json:"commits_url"`
    	CompareURL       string      `json:"compare_url"`
    	ContentsURL      string      `json:"contents_url"`
    	ContributorsURL  string      `json:"contributors_url"`
    	CreatedAt        string      `json:"created_at"`
    	DefaultBranch    string      `json:"default_branch"`
    	Description      string      `json:"description"`
    	DownloadsURL     string      `json:"downloads_url"`
    	EventsURL        string      `json:"events_url"`
    	Fork             bool        `json:"fork"`
    	Forks            float64     `json:"forks"`
    	ForksCount       float64     `json:"forks_count"`
    	ForksURL         string      `json:"forks_url"`
    	FullName         string      `json:"full_name"`
    	GitCommitsURL    string      `json:"git_commits_url"`
    	GitRefsURL       string      `json:"git_refs_url"`
    	GitTagsURL       string      `json:"git_tags_url"`
    	GitURL           string      `json:"git_url"`
    	HasDownloads     bool        `json:"has_downloads"`
    	HasIssues        bool        `json:"has_issues"`
    	HasWiki          bool        `json:"has_wiki"`
    	Homepage         interface{} `json:"homepage"`
    	HooksURL         string      `json:"hooks_url"`
    	HtmlURL          string      `json:"html_url"`
    	ID               float64     `json:"id"`
    	IssueCommentURL  string      `json:"issue_comment_url"`
    	IssueEventsURL   string      `json:"issue_events_url"`
    	IssuesURL        string      `json:"issues_url"`
    	KeysURL          string      `json:"keys_url"`
    	LabelsURL        string      `json:"labels_url"`
    	Language         string      `json:"language"`
    	LanguagesURL     string      `json:"languages_url"`
    	MasterBranch     string      `json:"master_branch"`
    	MergesURL        string      `json:"merges_url"`
    	MilestonesURL    string      `json:"milestones_url"`
    	MirrorURL        interface{} `json:"mirror_url"`
    	Name             string      `json:"name"`
    	NetworkCount     float64     `json:"network_count"`
    	NotificationsURL string      `json:"notifications_url"`
    	OpenIssues       float64     `json:"open_issues"`
    	OpenIssuesCount  float64     `json:"open_issues_count"`
    	Owner            struct {
        	AvatarURL         string  `json:"avatar_url"`
        	EventsURL         string  `json:"events_url"`
        	FollowersURL      string  `json:"followers_url"`
        	FollowingURL      string  `json:"following_url"`
        	GistsURL          string  `json:"gists_url"`
        	GravatarID        string  `json:"gravatar_id"`
        	HtmlURL           string  `json:"html_url"`
        	ID                float64 `json:"id"`
        	Login             string  `json:"login"`
        	OrganizationsURL  string  `json:"organizations_url"`
        	ReceivedEventsURL string  `json:"received_events_url"`
        	ReposURL          string  `json:"repos_url"`
        	SiteAdmin         bool    `json:"site_admin"`
        	StarredURL        string  `json:"starred_url"`
        	SubscriptionsURL  string  `json:"subscriptions_url"`
        	Type              string  `json:"type"`
        	URL               string  `json:"url"`
    } `	json:"owner"`
    	Private         bool    `json:"private"`
    	PullsURL        string  `json:"pulls_url"`
    	PushedAt        string  `json:"pushed_at"`
    	Size            float64 `json:"size"`
    	SshURL          string  `json:"ssh_url"`
    	StargazersURL   string  `json:"stargazers_url"`
    	StatusesURL     string  `json:"statuses_url"`
    	SubscribersURL  string  `json:"subscribers_url"`
    	SubscriptionURL string  `json:"subscription_url"`
    	SvnURL          string  `json:"svn_url"`
    	TagsURL         string  `json:"tags_url"`
    	TeamsURL        string  `json:"teams_url"`
    	TreesURL        string  `json:"trees_url"`
    	UpdatedAt       string  `json:"updated_at"`
    	URL             string  `json:"url"`
    	Watchers        float64 `json:"watchers"`
    	WatchersCount   float64 `json:"watchers_count"`
	}

Index

Constants

View Source
const (
	NameFloat64        = "float64"
	NameInterface      = "interface{}"
	NameArrayInterface = "[]interface{}"
)

Variables

View Source
var ForceFloats bool

Functions

func FmtFieldName

func FmtFieldName(s string) string

FmtFieldName formats a string as a struct key

Example:

FmtFieldName("foo_id")

Output: FooID

func Generate

func Generate(input io.Reader, parser Parser, structName, pkgName string, tags []string, subStruct bool, convertFloats bool) ([]byte, error)

Generate a struct definition given a JSON string representation of an object and a name structName.

func Parse

func Parse(options Options, data []byte) (string, error)

func ParseJson

func ParseJson(input io.Reader) (interface{}, error)

func ParseYaml

func ParseYaml(input io.Reader) (interface{}, error)

func TypeLookup

func TypeLookup(Type string) string

Types

type Options

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

func (*Options) ForceFloats

func (o *Options) ForceFloats()

func (*Options) InputIsJson

func (o *Options) InputIsJson()

func (*Options) InputIsYaml

func (o *Options) InputIsYaml()

func (*Options) PackageName

func (o *Options) PackageName(name string)

func (*Options) SaveOutput

func (o *Options) SaveOutput()

func (*Options) SetOutput

func (o *Options) SetOutput(name string)

func (*Options) StructureName

func (o *Options) StructureName(name string)

func (*Options) SubStructs

func (o *Options) SubStructs()

func (*Options) Tags

func (o *Options) Tags(names []string)

type Parser

type Parser func(io.Reader) (interface{}, error)

Jump to

Keyboard shortcuts

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