diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index feeeb08..0d548d5 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -56,6 +56,7 @@ func (g *Generator) writeStub() error { fmt.Fprintln(f, ")") for _, t := range g.Types { + fmt.Fprintln(f) if !g.NoStdMarshalers { fmt.Fprintln(f, "func (*", t, ") MarshalJSON() ([]byte, error) { return nil, nil }") fmt.Fprintln(f, "func (*", t, ") UnmarshalJSON([]byte) error { return nil }") @@ -63,6 +64,8 @@ func (g *Generator) writeStub() error { fmt.Fprintln(f, "func (*", t, ") MarshalEasyJSON(w *jwriter.Writer) {}") fmt.Fprintln(f, "func (*", t, ") UnmarshalEasyJSON(l *jlexer.Lexer) {}") + fmt.Fprintln(f) + fmt.Fprintln(f, "type EasyJSON_exporter_"+t+" *"+t) } return nil } @@ -104,7 +107,7 @@ func (g *Generator) writeMain() (path string, err error) { fmt.Fprintln(f, " g.NoStdMarshalers()") } for _, v := range g.Types { - fmt.Fprintln(f, " g.Add(pkg."+v+"{})") + fmt.Fprintln(f, " g.Add(pkg.EasyJSON_exporter_"+v+"(nil))") } fmt.Fprintln(f, " if err := g.Run(os.Stdout); err != nil {") diff --git a/gen/generator.go b/gen/generator.go index 6e32cfa..068cf73 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -112,8 +112,12 @@ func (g *Generator) addType(t reflect.Type) { // Add requests to generate (un-)marshallers and en-/decoding functions for the type of given object. func (g *Generator) Add(obj interface{}) { - g.addType(reflect.TypeOf(obj)) - g.marshallers[reflect.TypeOf(obj)] = true + t := reflect.TypeOf(obj) + if t.Kind() == reflect.Ptr { + t = t.Elem() + } + g.addType(t) + g.marshallers[t] = true } // printHeader prints package declaration and imports. diff --git a/tests/basic_test.go b/tests/basic_test.go index 3c1017c..79e52ec 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -26,6 +26,7 @@ var testCases = []struct { {&optsValue, optsString}, {&rawValue, rawString}, {&stdMarshalerValue, stdMarshalerString}, + {&unexportedStructValue, unexportedStructString}, } func TestMarshal(t *testing.T) { diff --git a/tests/data.go b/tests/data.go index 441e403..6bb9da1 100644 --- a/tests/data.go +++ b/tests/data.go @@ -313,3 +313,10 @@ type StdMarshaler struct { var stdMarshalerValue = StdMarshaler{T: time.Date(2016, 01, 02, 14, 15, 10, 0, time.UTC)} var stdMarshalerString = `{"T":"2016-01-02T14:15:10Z"}` + +type unexportedStruct struct { + Value string +} + +var unexportedStructValue = unexportedStruct{"test"} +var unexportedStructString = `{"Value":"test"}`