JSON and dealing with unexported fields

JsonGo

Json Problem Overview


Is there a technical reason why unexported fields are not included by encoding/json? If not and it is an arbitrary decision could there be an additional back door option (say '+') to include even though unexported?

Requiring client code to export to get this functionality feels unfortunate, especially if lower case is providing encapsulation or the decision to marshal structures comes much later than design of them.

How are people dealing with this? Just export everything?

Also, doesn't exporting field names make it difficult to follow suggested idioms. I think if a struct X has field Y, you can not have an accessor method Y(). If you want to provide interface access to Y you have to come up with a new name for the getter and no matter what you'll get something un-idiomatic according to http://golang.org/doc/effective_go.html#Getters

Json Solutions


Solution 1 - Json

There is a technical reason. The json library does not have the power to view fields using reflect unless they are exported. A package can only view the unexported fields of types within its own package

In order to deal with your problem, what you can do is make an unexported type with exported fields. Json will unmarshal into an unexported type if passed to it without a problem but it would not show up in the API docs. You can then make an exported type that embeds the unexported type. This exported type would then need methods to implement the json.Marshaler and json.Unmarshaler interfaces.

Note: all code is untested and may not even compile.

type jsonData struct {
    Field1 string
    Field2 string
}

type JsonData struct {
    jsonData
}

// Implement json.Unmarshaller
func (d *JsonData) UnmarshalJSON(b []byte) error {
    return json.Unmarshal(b, &d.jsonData)
}

// Getter
func (d *JsonData) Field1() string {
    return d.jsonData.Field1
}

Solution 2 - Json

Stephen's answer is complete. As an aside, if all you really want is lowercase keys in your json, you can manually specify the key name as follows:

type Whatever struct {
    SomeField int `json:"some_field"`
}

In that way, marshaling a Whatever produces the key "some_field" for the field SomeField (instead of having "SomeField" in your json).

If you're dead-set on keeping unexported fields, you can also implement the json.Marshaler interface by defining a method with the signature MarshalJSON() ([]byte, error). One way to do this is to use a struct literal that simply has exported versions of the unexported fields, like this:

type Whatever struct {
    someField int
}

func (w Whatever) MarshalJSON() ([]byte, error) {
    return json.Marshal(struct{
        SomeField int `json:"some_field"`
    }{
        SomeField: w.someField,
    })
}

That can be a bit cumbersome, so you can also use a map[string]interface{} if you prefer:

func (w Whatever) MarshalJSON() ([]byte, error) {
    return json.Marshal(map[string]interface{}{
        "some_field": w.SomeField,
    })
}

However it should be noted that marshaling interface{} has some caveats and can do things like marshal uint64 to a float, causing a loss of precision. (all code untested)

Solution 3 - Json

Using an interface would be another option.

type Person interface {
	Name() string
	SetName(name string) Person
	Age() int
	SetAge(age int) Person
}

type person struct {
	Name_ string
	Age_  int
}

func (p *person) Name() string {
	return p.Name_
}

func (p *person) SetName(name string) Person {
	p.Name_ = name

	return p
}

func (p *person) Age() int {
	return p.Age_
}

func (p *person) SetAge(age int) Person {
	p.Age_ = age

	return p
}

func NewPerson() Person {
	return &person{}
}

Since the person struct is lowercase you won't be able to access its public fields outside of its package. To instantiate a person value you provide a constructor that returns it wrapped in uppercase Person interface.

Playground

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1338952View Question on Stackoverflow
Solution 1 - JsonStephen WeinbergView Answer on Stackoverflow
Solution 2 - JsonjorelliView Answer on Stackoverflow
Solution 3 - JsonSteven EckhoffView Answer on Stackoverflow