Coder Social home page Coder Social logo

gojson's Introduction

Build Status gojson

gojson generates go struct definitions from json or yaml documents.

Example

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

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"`
}

CLI Installation

$ go get github.com/ChimeraCoder/gojson/gojson

Assuming $GOPATH/bin is in your PATH, you can now invoke gojson directly.

API Installation

$ go get github.com/ChimeraCoder/gojson/gojson

Development

$ git clone https://github.com/ChimeraCoder/gojson.git
$ cd gojson
$ go test

Building CLI

$ go build -o _build/gojson ./gojson

Installing CLI

$ go install ./gojson

Formatting

$ gofmt -w -e -s -l .

Related Work

github.com/str1ngs/jflect

License

gojson is free software distributed under Version 3 of the GNU Public License.

As of the time of writing, this is the same license used for gcc (and therefore gccgo), so it is unlikely to restrict use in any way. Note that the GPL does not extend to any output generated by gojson; the GPL only applies to software which includes copies of gojson itself.

gojson's People

Contributors

chimeracoder avatar darrennoble avatar e-dard avatar fhs avatar jmervine avatar jmhodges avatar joshu-stripe avatar kyokomi avatar nyedidikeke avatar rstrong-pica9 avatar sethwklein avatar tmc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gojson's Issues

Add flag for struct name

Currently all generated structs have the name TestStruct. A flag (optional?) should be added to specify the struct name. I propose -struct, though perhaps -name might work.

Removing the vendor directory

I personally think that the vendor directory should be removed, because it'll be either duplicated with the current YAML package that we currently have, or out of sync with it.

Either case, it make little sense to keep it there, IMO.

Please consider. Thx.

Allow dashes in JSON keys

Via cgtdk on #go-nuts, this error currently occurs:

### JSON DATA (THIS LINE NOT INCLUDED)
{
  "query-continue": {
    "recentchanges": {
      "rcstart": "2013-03-18T20:20:30Z"
    }
  },
  "query": {
    "recentchanges": [
      {
        "type": "edit",
        "ns": 4,
        "title": "Wikipedia:Deletion review\/Log\/2013 March 18",
        "rcid": 563307455,
        "pageid": 38844023,
        "revid": 545266112,
        "old_revid": 545265931,
        "timestamp": "2013-03-18T20:20:30Z"
      },
      {
        "type": "edit",
        "ns": 0,
        "title": "Pope John IV",
        "rcid": 563307454,
        "pageid": 243546,
        "revid": 545266111,
        "old_revid": 545261452,
        "timestamp": "2013-03-18T20:20:30Z"
      }
    ]
  }
}
### END JSON DATA

### RESULT OF USING GOJSON:
chris@DPC3:~$ gojson -file="mwdata.json"
panic: 3:7: expected ';', found '-'

goroutine 1 [running]:
main.main()
    /home/chris/code/go/src/github.com/ChimeraCoder/gojson/gojson.go:97 +0x126

goroutine 2 [syscall]:
created by runtime.main
    /build/buildd/golang-1.0.2/src/pkg/runtime/proc.c:221
chris@DPC3:~$

The easiest solution is probably to replace - with _.

Define types for array elements

This came up in ChimeraCoder/anaconda#187.

[
  {
    "id": "1",
    "url": "https://united.fr/webhook",
    "valid": true,
    "created_timestamp": "134325325301"
  }
]

will create

type Foo []struct {
	CreatedTimestamp string `json:"created_timestamp"`
	ID               string `json:"id"`
	URL              string `json:"url"`
	Valid            bool   `json:"valid"`
}

but in reality, most of the time, this is more convenient:

type Foo struct {
	CreatedTimestamp string `json:"created_timestamp"`
	ID               string `json:"id"`
	URL              string `json:"url"`
	Valid            bool   `json:"valid"`
}

State gopkg.in/yaml.v2 as a prequisite

After installation on a fresh system I got:

json-to-struct.go:112:2: cannot find package "gopkg.in/yaml.v2" in any of:

So we have to first install the yaml package. Please add this to the docs or install it iimplicit if needed.

Design a logo for gojson

Summary

In keeping with the Gopher spirit, it would be great to have a gopher mascot for this project!

The JSON logo is available under a Creative Commons license, so we should combine that with the gopher in some way. One option is to put the logo on the gopher's chest (similar to what vim-go does - see below), but we could also do other things as well - perhaps interpreting the JSON logo like a mobius strip and having the gopher sit on/inside it. Or we could get even more creative!

Outline

  • The logo should be in some vector format, such as SVG, for flexibility in reuse.

  • Like the original Go gopher, the artwork will need to be licensed CC-BY-licensed, under version 3.0 of the Creative Commons license, to keep the licensing situation simple.

Background

The Go project itself has the original Go gopher, but many Go projects have variants customized to their own project theme. In addition, regional Go conferences often use versions that are on-theme for the region.

Examples include Digby for dep, [vim-go](https://github.com/fatih/vim-go and the many personal profile icons available at Gopherize.me.

Gallery (Example gophers)

vim-go

Wearing the Vim logo!

image

GothamGo

One of my favorites for how subtle it is - it uses a silhouette of the Gopher as a bat, similar to the Bat Signal. (Gotham is one of the nicknames of New York City).

image

GopherCon Singapore

The mer-lion (half-fish, half-lion) is the mascot for Singapore, so the mer-Gopher (half-fish, half-gopher) is the mascot for GopherCon Singapore.

image

Capital Go

Wearing a hat and beard like President Abraham Lincoln, for a conference in the capital city of the US.

image

Digby (for dep)

Digby is hard at work managing packages!

image

GopherCon India

image

GopherCon Dubai

image

-forcefloats flag does not work

It does not work.
Input:

echo '{"value": 9.00}' | gojson -forcefloats

and the output is:

type Foo struct {
	Value int64 `json:"value"`
}

The -forcefloats flag does not work.

Re-write the CLI interface

OK to re-write the gojsonย using the CLI handling ofย jsonfiddle j2sย instead? (of course no sub-commands for gojson like jsonfiddle j2s)

Currently the advantages ofย jsonfiddle j2sย overย gojsonย are:

  • No longer uses the dummyย Fooย as the default for the name of the struct now, but use the input file name to automatic set the struct name instead, yet it can be overridden from command line (byย --name).ย Check the details here.
  • Internally, much more code consolidation and simplification (e.g., input/output file handling) have been done under the hood, so the hand-edited code has been reduced from over a hundred (132) toย just several lines.

Much more enhancements will be taken place,ย check out details here, but all depends on the new CLI infrastructure, as writing CLI handling by hand should give way to automatic code generation.

Improve handling of fields which begin with a numeral

Just discovered this quirk:

If a JSON object contains the field "3ED", it will be translated to the unexported field _ED. If the object contains both "3ED" and "4ED", both will be translated to unexported fields named _ED (which will fail to compile with the error message duplicate field _ED).

example for use API

Hi
How can I use gojson in the code without use cli and command line? please add a example to readme

tnx

Expand type inference on slices for homogeneous JSON arrays

For technical reasons, JSON arrays are currently translated as slices of the empty interface (ie, []interface{}).

Since JSON arrays can be heterogeneous, this should always be the default case, but in instances where all values in a given JSON array correspond to a single Go type, it may be helpful to use that type instead of the catch-all interface{}.

gojson always interprets natural JSON numbers as ints.

At the moment if gojson encounters a natural JSON number it converts it into an int. This isn't always desirable, because sometimes gojson is going to encounter a resource like:

{ "foo": 2.0 }

or

[{ "foo": 2 }, {"foo": 2.3 }]

In both cases gojson will define the JSON numbers in the results type as int.

Parse floats as float64

{
    "duration": 123.0,
    "timestamp" 1234
}

should provide

type Trace struct {
   Duration float64
   Timestamp int64
}

instead of int, int

Support flag to auto-export struct fields

By default, the struct fields may not be exported, depending on whether or not the JSON key is capitalized (most JSON keys in the wild are not capitalized).

Ideally, we should have a command-line flag which automatically capitalizes the struct fields so that they are exported. Speaking from my own experience, this is usually the very first thing that I do after generating the struct definition - while Vim fortunately makes this easy to do en-masse, it's one more step that we could easily automate.

OpenAPI/Swagger struct generation

Nice to see that I'm not the only one who wants to generate code from JSON.
However, plain JSON/YAML is the only notation that can be used as a source for code generation.

I'd like to propose OpenAPI V3 generation, assume you have the following JSON file:

{
  "type": "object",
  "properties": {
    "error": {
      "type": "object",
      "properties": {
        "message": {
          "type": "string"
        },
        "fields": {
          "type": "string"
        }
      }
    }
  }
}

this JSON file may end up in a go struct like:

type Error struct {
    Message string `json:"message"`
    Fields string `json:"fields"`
}

type SomeStruct struct {
    Error Error `json:"error"`
}

thoughts?

Should we add a new release?

The newest released was released in last year, but the master code update in few months.

If we use go1.11, we can only use v1.0.0 version of code.

For example:

version: v1.0.1

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

version: master

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

Master version add parameter convertFloats.

Possible bug in isInteractive

The following line of code bitwise ORs os.ModeCharDevice with itself. This is either redundant, or meant to use another variable on the RHS:

return fileInfo.Mode()&(os.ModeCharDevice|os.ModeCharDevice) != 0
/src/github.com/ChimeraCoder/gojson/gojson/gojson.go:105:26: identical expressions on the left and right side of the '|' operator

error parsing unexpected type: []interface {}

Attempting to parse the JSON generated by the Reddit.com API, and ran into the following error when trying to process the JSON from:

https://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go.json

jbuberel@golang-metrics:~/workspace $ curl https://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go.json | ~/bin/gojson --name RedditIndex --pkg reddit 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 20696    0 20696    0     0   115k      0 --:--:-- --:--:-- --:--:--  116k
error parsing unexpected type: []interface {}

However, the JSON appears formatted correctly (it validates using http://jsonformatter.curiousconcept.com/ and http://jsonformat.com/).

The formatted JSON is:

[ { "data" : { "after" : null,
        "before" : null,
        "children" : [ { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "HarveyKandola",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "clicked" : false,
                  "created" : 1426385069.0,
                  "created_utc" : 1426356269.0,
                  "distinguished" : null,
                  "domain" : "github.com",
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "hidden" : false,
                  "id" : "2z1frh",
                  "is_self" : false,
                  "likes" : null,
                  "link_flair_css_class" : null,
                  "link_flair_text" : null,
                  "media" : null,
                  "media_embed" : {  },
                  "mod_reports" : [  ],
                  "name" : "t3_2z1frh",
                  "num_comments" : 22,
                  "num_reports" : null,
                  "over_18" : false,
                  "permalink" : "/r/golang/comments/2z1frh/a_jvm_writing_in_go/",
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 46,
                  "secure_media" : null,
                  "secure_media_embed" : {  },
                  "selftext" : "",
                  "selftext_html" : null,
                  "stickied" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "thumbnail" : "",
                  "title" : "A JVM writing in GO",
                  "ups" : 46,
                  "upvote_ratio" : 0.88,
                  "url" : "https://github.com/zxh0/jvm.go",
                  "user_reports" : [  ],
                  "visited" : false
                },
              "kind" : "t3"
            } ],
        "modhash" : ""
      },
    "kind" : "Listing"
  },
  { "data" : { "after" : null,
        "before" : null,
        "children" : [ { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "rrubmo",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "Can someone explain me why it's so shocking a JVM writing in Go?",
                  "body_html" : "<div class=\"md\"><p>Can someone explain me why it&#39;s so shocking a JVM writing in Go?</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426405175.0,
                  "created_utc" : 1426376375.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpf3eo2",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpf3eo2",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "RIC_FLAIR-WOOO",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "It's not. These comments are just really silly.\n\nMuch better discussion [here](https://news.ycombinator.com/item?id=9203045)",
                                    "body_html" : "<div class=\"md\"><p>It&#39;s not. These comments are just really silly.</p>\n\n<p>Much better discussion <a href=\"https://news.ycombinator.com/item?id=9203045\">here</a></p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426409612.0,
                                    "created_utc" : 1426380812.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf5hgv",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf5hgv",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf3eo2",
                                    "replies" : { "data" : { "after" : null,
                                            "before" : null,
                                            "children" : [ { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "rrubmo",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "Thanks a lot, it was quite confusing.",
                                                      "body_html" : "<div class=\"md\"><p>Thanks a lot, it was quite confusing.</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426409937.0,
                                                      "created_utc" : 1426381137.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpf5mwx",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpf5mwx",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpf5hgv",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 4,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 4,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                },
                                                { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "maester_chief",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "In general the discussion is much better at HN. Especially on Go related posts, you're likely to see comments from Russ Cox(rsc) or Andrew Gerrand(enneff) while on reddit you will see /u/dvirsky and similar users contribute quality comments like [this](http://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go/cpf2rdc)",
                                                      "body_html" : "<div class=\"md\"><p>In general the discussion is much better at HN. Especially on Go related posts, you&#39;re likely to see comments from Russ Cox(rsc) or Andrew Gerrand(enneff) while on reddit you will see <a href=\"/u/dvirsky\">/u/dvirsky</a> and similar users contribute quality comments like <a href=\"http://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go/cpf2rdc\">this</a></p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426445730.0,
                                                      "created_utc" : 1426416930.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpfhejy",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpfhejy",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpf5hgv",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 1,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 1,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                }
                                              ],
                                            "modhash" : ""
                                          },
                                        "kind" : "Listing"
                                      },
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 10,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 10,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "UnreachablePaul",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "Because you can't use generics",
                                    "body_html" : "<div class=\"md\"><p>Because you can&#39;t use generics</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426458672.0,
                                    "created_utc" : 1426429872.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpfkrg5",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpfkrg5",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf3eo2",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "jeandem",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "Because writing a virtual machine in a GCd language - impossibru. /s",
                                    "body_html" : "<div class=\"md\"><p>Because writing a virtual machine in a GCd language - impossibru. /s</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426412615.0,
                                    "created_utc" : 1426383815.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf6vh6",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf6vh6",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf3eo2",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : -1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : -1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "children" : [ "cpfd5n5" ],
                                    "count" : 1,
                                    "id" : "cpfd5n5",
                                    "name" : "t1_cpfd5n5",
                                    "parent_id" : "t1_cpf3eo2"
                                  },
                                "kind" : "more"
                              }
                            ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 7,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 7,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "coder543",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "doesn't it bother anyone else that both here in the title and at the top of the readme it says \"writing\" instead of \"written\"?\n\nlooks like an interesting project though.",
                  "body_html" : "<div class=\"md\"><p>doesn&#39;t it bother anyone else that both here in the title and at the top of the readme it says &quot;writing&quot; instead of &quot;written&quot;?</p>\n\n<p>looks like an interesting project though.</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426427224.0,
                  "created_utc" : 1426398424.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpfd3zq",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpfd3zq",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "UnreachablePaul",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "Because author likes Java and that probably amounts to being mentally challenged in a funny way",
                                    "body_html" : "<div class=\"md\"><p>Because author likes Java and that probably amounts to being mentally challenged in a funny way</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426441149.0,
                                    "created_utc" : 1426412349.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpfgkdw",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpfgkdw",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpfd3zq",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : -9,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : -9,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              } ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 4,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 4,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "meowtasticly",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "Can someone explain what this is? Are they making a JVM port in Go?",
                  "body_html" : "<div class=\"md\"><p>Can someone explain what this is? Are they making a JVM port in Go?</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426390353.0,
                  "created_utc" : 1426361553.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpew9xo",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpew9xo",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "LimEJET",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "I guess, but it's really badly documented and there's basically no comments at all so I'm just guessing. It's probably really early in development.",
                                    "body_html" : "<div class=\"md\"><p>I guess, but it&#39;s really badly documented and there&#39;s basically no comments at all so I&#39;m just guessing. It&#39;s probably really early in development.</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426393270.0,
                                    "created_utc" : 1426364470.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpexp4u",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpexp4u",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpew9xo",
                                    "replies" : { "data" : { "after" : null,
                                            "before" : null,
                                            "children" : [ { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "uncle_bad_touches",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "Spent way more time on the graphic than the docs",
                                                      "body_html" : "<div class=\"md\"><p>Spent way more time on the graphic than the docs</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426406565.0,
                                                      "created_utc" : 1426377765.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpf42mr",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpf42mr",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpexp4u",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 4,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 4,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                },
                                                { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "devsquid",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "Or someone is just doing it for class or something and wants to share",
                                                      "body_html" : "<div class=\"md\"><p>Or someone is just doing it for class or something and wants to share</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426408818.0,
                                                      "created_utc" : 1426380018.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpf54jd",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpf54jd",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpexp4u",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 2,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 2,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                }
                                              ],
                                            "modhash" : ""
                                          },
                                        "kind" : "Listing"
                                      },
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 3,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 3,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              } ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 1,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 1,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "dvirsky",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "YO DAWG, I HERD YOU LIKE GC PAUSES, SO I BUILT A JVM WITH A RUNTIME THAT HAS GC, SO YOUR JVM CAN HAVE GC PAUSES WHILE IT HAS GC PAUSES.",
                  "body_html" : "<div class=\"md\"><p>YO DAWG, I HERD YOU LIKE GC PAUSES, SO I BUILT A JVM WITH A RUNTIME THAT HAS GC, SO YOUR JVM CAN HAVE GC PAUSES WHILE IT HAS GC PAUSES.</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426403794.0,
                  "created_utc" : 1426374994.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpf2rdc",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpf2rdc",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "RIC_FLAIR-WOOO",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "While that is a pretty dank meme you've posted, this implementation uses Go's native GC only.",
                                    "body_html" : "<div class=\"md\"><p>While that is a pretty dank meme you&#39;ve posted, this implementation uses Go&#39;s native GC only.</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426410575.0,
                                    "created_utc" : 1426381775.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf5xih",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf5xih",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf2rdc",
                                    "replies" : { "data" : { "after" : null,
                                            "before" : null,
                                            "children" : [ { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "dvirsky",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "that's interesting. had there been any documentation I would have known that :/ \n(but I wouldn't have missed the joke opportunity regardless)",
                                                      "body_html" : "<div class=\"md\"><p>that&#39;s interesting. had there been any documentation I would have known that :/ \n(but I wouldn&#39;t have missed the joke opportunity regardless)</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426436084.0,
                                                      "created_utc" : 1426407284.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpffkil",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpffkil",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpf5xih",
                                                      "replies" : { "data" : { "after" : null,
                                                              "before" : null,
                                                              "children" : [ { "data" : { "approved_by" : null,
                                                                        "archived" : false,
                                                                        "author" : "jeandem",
                                                                        "author_flair_css_class" : null,
                                                                        "author_flair_text" : null,
                                                                        "banned_by" : null,
                                                                        "body" : "> had there been any documentation I would have known that :/\n\nLack of documentation is not an excuse for blindly guessing. ;)",
                                                                        "body_html" : "<div class=\"md\"><blockquote>\n<p>had there been any documentation I would have known that :/</p>\n</blockquote>\n\n<p>Lack of documentation is not an excuse for blindly guessing. ;)</p>\n</div>",
                                                                        "controversiality" : 0,
                                                                        "created" : 1426485428.0,
                                                                        "created_utc" : 1426456628.0,
                                                                        "distinguished" : null,
                                                                        "downs" : 0,
                                                                        "edited" : false,
                                                                        "gilded" : 0,
                                                                        "id" : "cpfyg8y",
                                                                        "likes" : null,
                                                                        "link_id" : "t3_2z1frh",
                                                                        "mod_reports" : [  ],
                                                                        "name" : "t1_cpfyg8y",
                                                                        "num_reports" : null,
                                                                        "parent_id" : "t1_cpffkil",
                                                                        "replies" : "",
                                                                        "report_reasons" : null,
                                                                        "saved" : false,
                                                                        "score" : 1,
                                                                        "score_hidden" : false,
                                                                        "subreddit" : "golang",
                                                                        "subreddit_id" : "t5_2rc7j",
                                                                        "ups" : 1,
                                                                        "user_reports" : [  ]
                                                                      },
                                                                    "kind" : "t1"
                                                                  } ],
                                                              "modhash" : ""
                                                            },
                                                          "kind" : "Listing"
                                                        },
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 0,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 0,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                } ],
                                            "modhash" : ""
                                          },
                                        "kind" : "Listing"
                                      },
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 15,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 15,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "ChasingLogic",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "This made me laugh so hard my wife was like what and then just gave me a weird look. (She's not a programmer.)",
                                    "body_html" : "<div class=\"md\"><p>This made me laugh so hard my wife was like what and then just gave me a weird look. (She&#39;s not a programmer.)</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426409685.0,
                                    "created_utc" : 1426380885.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf5in0",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf5in0",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf2rdc",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              }
                            ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 10,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 10,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "dominosci",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "WAT?!",
                  "body_html" : "<div class=\"md\"><p>WAT?!</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426388510.0,
                  "created_utc" : 1426359710.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpevdan",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpevdan",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "koffiezet",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "I came here to say this...\n\nBut - if it really works, it'd be really cool though :P Not gonna try it out though...\n",
                                    "body_html" : "<div class=\"md\"><p>I came here to say this...</p>\n\n<p>But - if it really works, it&#39;d be really cool though :P Not gonna try it out though...</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426395497.0,
                                    "created_utc" : 1426366697.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpeysw8",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpeysw8",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpevdan",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              } ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : -5,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : -5,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "ksawicki",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "lol",
                  "body_html" : "<div class=\"md\"><p>lol</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426393573.0,
                  "created_utc" : 1426364773.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpexufw",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpexufw",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : "",
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : -6,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : -6,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            }
          ],
        "modhash" : ""
      },
    "kind" : "Listing"
  }
]

Add a flag to prevent from fields reorder

Current the fields of output struct is sorted, it is not good for me to advanced process.
If the fields order is same with original json format, it is more easy to use for me.

Parse decimals ending in .0 as floats instead of ints

{
    "value": 10.0,
    "timestamp": 147847894,
}

value should be parsed as a float64, but timestamp should be an int64.

This comes from @nstogner: (#22 (comment))

To elaborate on my personal use-case: I would like to be able to define a struct's default values via a JSON object... To do this I would like to use gojson to first create the struct and then use another process to read in the object again to determine the default values, ie:

{ "name": "", "health": "100", "score": 0.0 }
-- piped thru gojson -->

type Foo struct {
Name string `json:"name"`
Health int `json:"health"` 
Score float64 `json:"score"` 
}

-- JSON object piped thru extra process -->

f := Foo{
Health: 100,
}

In other words, I would need to be able to support 0.0 values which get translated into floats.

Generate struct with pointers

I'm using gojson here and I need to check for some obligatory fields on my JSON. AFAIK the only way to do that using only Go (no other third party tool that validates the JSON based on a schema) is to use pointers on the struct, so I can check it for nil after the parsing.

Is there a way to generate the struct with pointers right now ? If it does not have support to this, does it even make sense ? :-)

Automatically deserialize JSON 'null' values as pointers

The following excerpt from the Twitter API will cause a runtime panic:

{
    "expanded_url": null,
    "url": "http://about.me/michaelangelo",
    "indices": [
        0,  
    29  
        ]   
}

The offending line is probably

structure += "\n" + indentation + key + " " + curType.Name()

due to a dereferencing of a nil value. We should fix this to deserialize the JSON values correctly as a nullable value (ie, a pointer).

Can we export output as struct variable

I want to use this structure variable when the program is running, but the result is stored in the file. I can't predict it in advance, which will lead to compilation failure. Is there any way

Generate better substruct names

I just started using the -subStruct flag because I need to be able to append sub-structs to arrays. It works great, thanks!

The name it gives the sub-structs could be better. I understand you'd want to avoid struct name collisions but even something like OuterStruct_InnerStruct would be better and more useful than the current _sub# method. Especially with IDE autocomplete. I can choose from 4 sub-structs but without going and seeing which is which I don't actually know. Also I'm assuming that if the order in the JSON file changed the sub indexing would get messed up and everything would break.

I'll take a look at submitting a PR soon-ish of my idea.

Unmarshall heterogeneous arrays as tagged unions

This is inspired by #17.

A number of APIs, such as Reddit and Hacker News, return heterogenous results, with a special field (kind or type) that indicates which type it actually is.

The easiest way to handle this is to have gojson provide a super-struct which is the union of all the fields it finds, and then the developer can use []SuperStruct as a target to unmarshal this endpoint. The developer will then likely want to create separate struct definitions for each typekind, which is done by simply copying the definition and removing the fields which aren't relevant to that type. Unfortunately that step can't be done automatically, but it shouldn't be too bad.

As a bonus, we could potentially add stub toFoo(), toBar() methods for each typekind found. The developer still has to create the type Foo struct definition, but this is one extra step we can eliminate.

New tool to generate proto(protobuf) file from json

Protobuf is a general tool for generating and management cross-language structures.
A tool to generate proto from json is useful.

Here is such a tool written by me.

Maybe you can reimplement such a tool with golang or mention it in the readme.

Allow named struct types for nested objects?

This is particularly relevant now that Go 1.4 provides go generate.

Currently, gojson handles nested objects by creating an anonymous struct. For example:

type Repository struct {
    ArchiveURL string `json:"archive_url"`
    Owner      struct {
        AvatarURL string `json:"avatar_url"`
    } `json:"owner"`
}

This is useful, but sometimes we might want to specify an Owner struct type, and instead have the following:

type Repository struct {
    ArchiveURL string `json:"archive_url"`
    Owner      Owner  `json:"owner"`
}


// owner.go

type Owner struct {
    AvatarURL string `json:"avatar_url"`
}

And sometimes we might already have the Owner struct defined, but gojson doesn't know that, and will redefine it (anonymously) every time it is used.

Solving the general case may be difficult, but one easy approach for the latter case (the type has already been defined elsewhere) would be to scan the current directory for struct definitions that match the desired structure, and then use those. So in this example, gojson would output the first definition (with the embedded struct) by default, but would use the Owner type if it finds it in the current directory.

An example of the use case is in this Twitter client library: https://github.com/ChimeraCoder/anaconda/blob/master/tweet.go#L30. The User type is quite large, and it would not only be wasteful but inconvenient to have it defined anonymously within the Tweet struct.

Prefer interface{} over struct{} for empty object: {}

I think it would be more desirable to translate {"abc": {}} as

...
Abc interface{} `json:"abc"`

rather than

...
Abc struct{} `json:"abc"`

The empty interface would be more useful than a struct with no fields. Does this sound reasonable?

Field named "id" is translated as "ID", not "Id"

Most of the other fields are converted in CamelCase with the first character capitalized (so it is exported).

I can't think offhand of why this is being translated like this, or decide now whether it makes sense to special-case two letter ids like this, so I'm filing this issue as a reminder for this discussion point.

-subStruct does not print nested fields in array items

I'm trying to use gojson to generate structs for an array of items, which may or may not contain (disjunct) keys with different sub-objects. Example (let's call this test.json):

[{"foo": {"bar": 2, "baz": 4, "xx": {"foo": "bar"}}}, 
 {"foo": {"bar": 2, "yy": {"bar": "baz"}}}]

In my original input these are one object per line, but as gojson can not handle this I have converted the input to an array instead. While this format can be well processed using the default settings:

$ cat test.json | gojson
package main

type Foo []struct {
	Foo struct {
		Bar int64 `json:"bar"`
		Baz int64 `json:"baz"`
		Xx  struct {
			Foo string `json:"foo"`
		} `json:"xx"`
		Yy struct {
			Bar string `json:"bar"`
		} `json:"yy"`
	} `json:"foo"`
}

there's a problem when trying to use -subStruct:

$ cat test.json | gojson -subStruct
package main

type Foo []struct {
	Foo Foo_sub3 `json:"foo"`
}

The substructs just are not printed. I would have expected an output similar to what I would get from a non-array input:

$ cat test2.json
{"foo": {"bar": 2, "baz": 4, "xx": {"foo": "bar"}}}
$ cat test2.json | gojson -subStruct
package main

type Foo struct {
	Foo Foo_sub2 `json:"foo"`
}

type Foo_sub2 struct {
	Bar int64    `json:"bar"`
	Baz int64    `json:"baz"`
	Xx  Foo_sub1 `json:"xx"`
}

type Foo_sub1 struct {
	Foo string `json:"foo"`
}

where the referenced substructs are also printed in the actual output.

Add flag to prepend comment to file

I'd like to add a comment like // Code generated by gojson. DO NOT EDIT. to my generated file. I don't want to do this manually.
So I propose adding -comment flag which would just prepend that line to the file.

Add a command-line flag for separating out embedded structs

@AeroNotix and @skelterjohn point out that in some cases it may be preferable to have distinct named types for struct fields instead of using embedded structs. (See the #go-nuts conversation starting at https://botbot.me/freenode/go-nuts/msg/3067419/)

This wouldn't be too hard to do; the only problem is naming the structs (and avoiding naming collisions). @skelterjohn outlines one potential method for addressing that here: https://botbot.me/freenode/go-nuts/msg/3067637/ .

Add support to time.Time

Hi, this is a request to extend the struct generation to support time fields.
If the string matches the ISO 8601 / RFC 3339 patterns, like:

  • 2021-07-28T12:34:56Z
  • 2021-07-28T16:34:56-04:00

Then the type should time.Time instead of string.
Thanks for the consideration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.