diff --git a/jingo_test.go b/jingo_test.go index 368061a..17dc1a8 100644 --- a/jingo_test.go +++ b/jingo_test.go @@ -31,11 +31,12 @@ type all struct { PropPs []*string `json:"ps"` PropNamesEscaped []string `json:"propNameEscaped,escape"` } `json:"propStruct"` - PropEncode encode0 `json:"propEncode,encoder"` - PropEncodeP *encode0 `json:"propEncodeP,encoder"` - PropEncodenilP *encode0 `json:"propEncodenilP,encoder"` - PropEncodeS encode1 `json:"propEncodeS,encoder"` - PropJSONMarshaler jsonMarshaler `json:"propJSONMarshaler,encoder"` + PropEncode encode0 `json:"propEncode,encoder"` + PropEncodeP *encode0 `json:"propEncodeP,encoder"` + PropEncodenilP *encode0 `json:"propEncodenilP,encoder"` + PropEncodeS encode1 `json:"propEncodeS,encoder"` + PropJSONMarshaler jsonMarshaler `json:"propJSONMarshaler,encoder"` + PropJSONMarshalerP *jsonMarshaler `json:"propJSONMarshalerP,encoder"` } type encode0 struct { @@ -95,16 +96,17 @@ func Example() { PropPs: []*string{&s, nil, &s}, PropNamesEscaped: []string{"one\\two\\,three\"", "\"four\\five\\,six\""}, }, - PropEncode: encode0{'1'}, - PropEncodeP: &encode0{'2'}, - PropEncodeS: encode1{encode0{'3'}, encode0{'4'}}, - PropJSONMarshaler: jsonMarshaler{[]byte("1")}, + PropEncode: encode0{'1'}, + PropEncodeP: &encode0{'2'}, + PropEncodeS: encode1{encode0{'3'}, encode0{'4'}}, + PropJSONMarshaler: jsonMarshaler{[]byte("1")}, + PropJSONMarshalerP: &jsonMarshaler{[]byte("2")}, }, b) fmt.Println(b.String()) // Output: - // {"propBool":false,"propInt":1234567878910111212,"propInt8":123,"propInt16":12349,"propInt32":1234567891,"propInt64":1234567878910111213,"propUint":12345678789101112138,"propUint8":255,"propUint16":12345,"propUint32":1234567891,"propUint64":12345678789101112139,"propFloat32":21.232426,"propFloat64":2799999999888.2827,"propString":"thirty two thirty four","propStruct":{"propName":["a name","another name","another"],"ps":["test pointer string",null,"test pointer string"],"propNameEscaped":["one\\two\\,three\"","\"four\\five\\,six\""]},"propEncode":1,"propEncodeP":2,"propEncodenilP":null,"propEncodeS":134,"propJSONMarshaler":1} + // {"propBool":false,"propInt":1234567878910111212,"propInt8":123,"propInt16":12349,"propInt32":1234567891,"propInt64":1234567878910111213,"propUint":12345678789101112138,"propUint8":255,"propUint16":12345,"propUint32":1234567891,"propUint64":12345678789101112139,"propFloat32":21.232426,"propFloat64":2799999999888.2827,"propString":"thirty two thirty four","propStruct":{"propName":["a name","another name","another"],"ps":["test pointer string",null,"test pointer string"],"propNameEscaped":["one\\two\\,three\"","\"four\\five\\,six\""]},"propEncode":1,"propEncodeP":2,"propEncodenilP":null,"propEncodeS":134,"propJSONMarshaler":1,"propJSONMarshalerP":2} } func Example_testStruct2() { diff --git a/structencoder.go b/structencoder.go index 9cebd92..df18d77 100755 --- a/structencoder.go +++ b/structencoder.go @@ -107,8 +107,14 @@ func NewStructEncoder(t interface{}) *StructEncoder { /// support calling .JSONEncode(*Buffer) when the 'encoder' option is passed case opts.Contains("encoder"): + // requrie explicit opt-in for JSONMarshaler implementation - if _, ok := reflect.PtrTo(reflect.ValueOf(e.t).Field(e.i).Type()).MethodByName("EncodeJSON"); ok { + t := reflect.ValueOf(e.t).Field(e.i).Type() + if t.Kind() != reflect.Ptr { + t = reflect.PtrTo(t) + } + + if _, ok := t.MethodByName("EncodeJSON"); ok { e.optInstrEncoderWriter() break }