By user1338952


2012-06-20 19:29:27 8 Comments

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

2 comments

@jorelli 2012-06-20 23:31:05

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)

@Tyler Eaves 2013-05-14 14:21:28

Technically, ALL numbers in Javascript are floats.

@jorelli 2013-05-14 19:19:05

JSON is not JavaScript. The JSON spec merely states which characters are acceptable, not which numeric ranges are valid. A number like 876234958273645982736459827346598237465923847561203947812435‌​968234659827346 is still valid in JSON, even if it can't be understood by JavaScript. For a real world example, the Twitter API represents tweet IDs as 64bit unsigned integers, which is not valid in JavaScript, but is valid JSON.

@quetzalcoatl 2014-07-13 20:28:02

Currently, all true, but still the name is JSON, which historically stands for "JavaScript Object Notation". One of those tiny suprising bits that make you want to rename it to NJSON (NewJSON or rather NotJavaScript) :)

@Stephen Weinberg 2012-06-20 23:10:57

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
}

@Derek 2013-03-28 20:05:47

For the record, I had to Unmarshall using "json.Unmarshal(b, &d.jsonData)". Did I do something wrong, or is that expected?

@Stephen Weinberg 2013-03-29 08:00:40

@Derek, thanks, I updated my answer. As I said the code was untested. I also apparently also forgot a return statement in my UnmarshalJSON() method. I fixed this as well.

@davidjosepha 2017-02-28 03:17:15

I'm a bit late to the show, but... while the above works, Field1 and Field2 are exported. You can read and write Field1 and Field2 of JsonData (with a capital J) outside of that package. So, while this is cool in theory, it doesn't actually do anything different than exporting both the type and the fields.

@darkwing 2018-05-15 18:42:11

@davidjosepha you're correct as the answer was originally shown although someone would have to be looking into your source code to know that Field1 and Field2 are present since godoc wouldn't list the unexported struct jsonData. However, this issue can be fixed by not using an Anonymous field in the JsonData struct, and having a named field which is unexported. This then requires that Field1 and Field2 are accessed through the named, unexported field. I edited the answer to use this methodology.

Related Questions

Sponsored Content

33 Answered Questions

[SOLVED] What is the correct JSON content type?

46 Answered Questions

[SOLVED] Can comments be used in JSON?

  • 2008-10-28 20:39:03
  • Michael Gundlach
  • 1647708 View
  • 6263 Score
  • 46 Answer
  • Tags:   json comments

53 Answered Questions

[SOLVED] How can I pretty-print JSON in a shell script?

17 Answered Questions

23 Answered Questions

[SOLVED] How can I pretty-print JSON using JavaScript?

9 Answered Questions

[SOLVED] Parsing values from a JSON file?

  • 2010-05-14 15:54:20
  • michele
  • 2403510 View
  • 1327 Score
  • 9 Answer
  • Tags:   python json parsing

6 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

39 Answered Questions

[SOLVED] How do I format a Microsoft JSON date?

24 Answered Questions

[SOLVED] Safely turning a JSON string into an object

  • 2008-09-05 00:12:01
  • Matt Sheppard
  • 1162446 View
  • 1213 Score
  • 24 Answer
  • Tags:   javascript json

16 Answered Questions

[SOLVED] Parse JSON in JavaScript?

Sponsored Content