2016-06-19 12:42 GMT+02:00 'Mihai B' via golang-nuts <golang-nuts@googlegroups.com>: > I have a struct that needs to be marshalled/unmarshalled using various > serialization formats(xml, json, name/value). The tags start to represent a > considerable effort[0] so I'm wondering if this is a common use case and if > a change[1] to the encoding packages to specify the tag key/selectors would > be a bad idea. Another option would be to use a standard tag key as default > (e.g. "encoding") but I think it may violate the Go1 backward > compatibility. > > Mihai. > > > [0] > type Payment struct { > ActionType paypal.ActionType `query:"actionType,omitempty" > json:"actionType,omitempty" xml:"actionType,omitempty"` > ReceiverList paypal.ReceiverList `query:"actionType,omitempty" > json:"receiverList,omitempty" xml:"receiverList,omitempty"` > }
For what it's worth, in cases where I've needed to do this the tags have often differed for the various formats. type SomeOptions struct { ... AlwaysLocalNets []string `xml:"alwaysLocalNet" json:"alwaysLocalNets"` DeprecatedUPnPEnabled bool `xml:"upnpEnabled,omitempty" json:"-"` ... } For slices, the plural case differs as you want "items": ["foo", "bar"] in JSON but <item>foo</item><item>bar</item> in XML. The deprecated option should be read from the XML source but omitted in the JSON response. And so on. //jb -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.