• Stars
    star
    490
  • Rank 89,811 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This tool is similar to golang.org/x/tools/cmd/stringer but generates MarshalJSON and UnmarshalJSON methods.

jsonenums

jsonenums is a tool to automate the creation of methods that satisfy the json.Marshaler and json.Unmarshaler interfaces. Given the name of a (signed or unsigned) integer type T that has constants defined, jsonenums will create a new self-contained Go source file implementing

func (t T) MarshalJSON() ([]byte, error)
func (t *T) UnmarshalJSON([]byte) error

The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.

jsonenums is a simple implementation of a concept and the code might not be the most performant or beautiful to read.

For example, given this snippet,

package painkiller

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

running this command

jsonenums -type=Pill

in the same directory will create the file pill_jsonenums.go, in package painkiller, containing a definition of

func (r Pill) MarshalJSON() ([]byte, error)
func (r *Pill) UnmarshalJSON([]byte) error

MarshalJSON will translate the value of a Pill constant to the []byte representation of the respective constant name, so that the call json.Marshal(painkiller.Aspirin) will return the bytes []byte("\"Aspirin\"").

UnmarshalJSON performs the opposite operation; given the []byte representation of a Pill constant it will change the receiver to equal the corresponding constant. So given []byte("\"Aspirin\"") the receiver will change to Aspirin and the returned error will be nil.

Typically this process would be run using go generate, like this:

//go:generate jsonenums -type=Pill

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_jsonenums.go, where t is the lower-cased name of the first type listed. The suffix can be overridden with the -suffix flag and a prefix may be added with the -prefix flag.

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

More Repositories

1

go-tooling-workshop

A workshop covering all the tools gophers use in their day to day life
Go
2,792
star
2

justforfunc

The repository for the YouTube series JustForFunc
Go
1,474
star
3

embedmd

embedmd: embed code into markdown and keep everything in sync
Go
767
star
4

todo

A task manager written in Go using TDD techniques, each commit should represent a step in the application development.
Go
569
star
5

go-web-workshop

Build Web Applications with Go on App Engine
Go
534
star
6

golang-plugins

A simple demo of hot code swapping with Go 1.8 and its plugins feature.
Go
249
star
7

tools

List of tools
Go
109
star
8

flappy-gopher

The code for the Flappy Gopher episodes of #justforfunc
Go
105
star
9

golang-groups

List of Go user groups, meetups, and similar.
JavaScript
102
star
10

unique

Package unique provides primitives for sorting slices removing repeated elements.
Go
94
star
11

chat

A good demonstration of Go composition of types and processes.
Go
85
star
12

apiai

A simple framework for api.ai fulfillment backends
Go
72
star
13

whispering-gophers

Whispering Gophers is a Go workshop
Go
71
star
14

podcast-to-youtube

Post any podcast episode from a RSS feed to YouTube
Go
55
star
15

mandelbrot

Go
42
star
16

apicheck

A tool to compare different versions of an API and find incompatibilities.
Go
40
star
17

links

Working on stuff
Go
38
star
18

csvql

cvsql: a SQL server for CSV files
Go
35
star
19

goml

Machine Learning Adventures in Go
Go
34
star
20

mat

A simple to use, not ready for production, matrix package
Go
30
star
21

podcasts

A list of podcasts that I listen to or I've been told I should listen to
30
star
22

svg-badge

Code for https://svg-badge.appspot.com
Go
30
star
23

advent-of-code-2018

My solutions for advent of code 2018
Go
27
star
24

audio

Audio experiments in Go
Go
26
star
25

groto

A protocol buffer file definition parser written in Go
Go
20
star
26

httplog

Easy and flexible logging of HTTP requests
Go
18
star
27

pokegraph

Serving poke-api on Dgraph
Go
17
star
28

gaesave

Helper tool to save objects to the datastore offering pre and post save hooks. Inspired by http://www.mrvdot.com/all/creating-a-universal-save-method-in-go-on-gae.
Go
14
star
29

leap

Go API for Leap Motion devices
Go
13
star
30

code-as-graphs

Source code is a graph, so why wouldn't we store it in Dgraph?
Go
10
star
31

sortslice

Sorting slices with less lines
Go
10
star
32

chromegodoc

A Chrome extension opening the current tab in godoc.
JavaScript
9
star
33

goodgopher

A GitHub bot for Good Gophers
9
star
34

blog

A blog system written in Go and running on App Engine
Go
7
star
35

abc

ABC music notation parser
Go
7
star
36

badger-migrations

A repository to experiment with badger and all its versions
Go
6
star
37

mandarin

my notes while learning Mandarin
6
star
38

learning-from-context

Implementing the context package step by step for a talk
Go
6
star
39

campoy.cat

Generic personal webpage with a list of links to other resources.
CSS
6
star
40

golang-driver

a bblfsh go driver
Go
5
star
41

singing-gophers

Singing gophers
Go
5
star
42

docker-go1.3

Docker images providing different versions of Go
Shell
5
star
43

github-reminder

Source for GitHub reminder app
4
star
44

goconf

Conference manager on the cloud using Go and Google Cloud
Go
4
star
45

advent-of-code-2019

Advent of Code 2019
Go
4
star
46

ObjectOrientedAndConcurrent

Source for the talk "Go: object oriented and concurrent"
Go
4
star
47

riff

Encoder decoder for RIFF (Resource Interchange File Format) files based on the wikipedia format explanation (http://en.wikipedia.org/wiki/Resource_Interchange_File_Format)
Go
4
star
48

wav

Support for WAV format audio files on Go
Go
3
star
49

ci-cd-workshop

CSS
2
star
50

jsparser

A javascript parser written in Go
2
star
51

wordiction

A simple example showing how to write a word prediction algorithm, similar to t9, in Go.
2
star
52

cooltool

Dummy repository to demonstrate pull requests with Go
Go
2
star
53

go4java

Go for Javaneros, talk and code
Go
1
star
54

gameoflife

Conway's Game of Life
1
star
55

lookout-test

Testing stuff
JavaScript
1
star
56

gio

Interpreter of io language in Go
1
star
57

gosamples

Simple samples of Go code
Go
1
star