Skip to content

Commit

Permalink
Merge pull request #33 from jimhickinbottom/pointerfix
Browse files Browse the repository at this point in the history
fixed encoder tag option JSONMarshaler opt in for pointer fields
  • Loading branch information
kungfusheep authored Dec 6, 2023
2 parents ba5ad09 + 01ee016 commit 64766c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
22 changes: 12 additions & 10 deletions jingo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion structencoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 64766c6

Please sign in to comment.