Enums, Stringer and output to Json

836 views
Skip to first unread message

webus...@gmail.com

unread,
Dec 31, 2014, 10:30:28 AM12/31/14
to golan...@googlegroups.com
If I have some enums as :

type SomeEnum int8

const(
   Value1 SomeEnum  = 0
   Value2 SomeEnum  = 1
...
)

With the go generate stringer I can get the string representation of the enum for debugging which is great. However, how would I marshall SomeEnum to Json so that I have the following map:

{
   "Value1" : 0,
   "Value2": 1
   ...
}



Matt Harden

unread,
Dec 31, 2014, 3:26:09 PM12/31/14
to webus...@gmail.com, golan...@googlegroups.com
It would be easy to implement MarshalText or MarshalJSON in terms of String; there is no need to generate these:

func (x SomeEnum) MarshalText() ([]byte, error) {
        return []byte(x.String()), nil
}

func (x SomeEnum) MarshalJSON() ([]byte, error) {
        return json.Marshal(x.String())
}

Note that json.Marshal will use MarshalText if it exists, even though this fact is not documented in the encoding/json package.

Based on the map you listed though, it sounds like you want to do the reverse, and Unmarshal string values into your enum. The stringer command won't do that directly. If all your values are unique and consecutive, you could do this:

var SomeEnumMap = func() map[string]SomeEnum {
        out := make(map[string]SomeEnum)
        for i := Value1; i <= LastValue, i++ {
                out[i.String()] = i
        }
        return out
}()

Otherwise an "unstringer" tool would be helpful, but as far as I know it doesn't exist.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

webus...@gmail.com

unread,
Jan 1, 2015, 11:37:20 PM1/1/15
to golan...@googlegroups.com, webus...@gmail.com
I think you misunderstood what I was asking for. I want to dump all the possible values for a given enum to a json string/file. So in my example, I want all values defined for SomeEnum marshalled to json. 

Matt Harden

unread,
Jan 2, 2015, 12:08:22 PM1/2/15
to webus...@gmail.com, golan...@googlegroups.com
Just generate the map as I described, except make it a map[string]int, then dump it to json using json.Marshal.

webus...@gmail.com

unread,
Jan 2, 2015, 12:12:09 PM1/2/15
to golan...@googlegroups.com, webus...@gmail.com
Yes but the function to generate the map has to be updated each time the enum changes. In particular the LastValue has to be updated each time something gets added...

Matt Harden

unread,
Jan 2, 2015, 12:28:39 PM1/2/15
to webus...@gmail.com, golan...@googlegroups.com
Yes. Here's one way to do it without much trouble.

type SomeEnum int
const (
    Value1 SomeEnum = iota
    Value2
    ...
    LastValue = iota - 1
)

This only works if you assign all the values using iota.

webus...@gmail.com

unread,
Jan 2, 2015, 12:34:26 PM1/2/15
to golan...@googlegroups.com, webus...@gmail.com
Ok. However, it also has some drawbacks:

- that you have to rename LastValue to something else if you have multiple enum types defined in the same package
- you need a new function for each enum type

Ideally the stringer utility also adds the ability to dump the entire enum ...

Matt Harden

unread,
Jan 2, 2015, 1:26:59 PM1/2/15
to webus...@gmail.com, golan...@googlegroups.com
It's open source; you can modify it to do whatever you want.
Reply all
Reply to author
Forward
0 new messages