79319115

Date: 2024-12-31 03:38:53
Score: 1
Natty:
Report link

Field.MarshalJSON calls Encoder.Encode to marshal the Field value. Encoder.Encode calls Field.MarshalJSON to marshal the Field. This repeats until the stack space is exceeded.

Break the recursion by declaring a new type without the MarshalJSON method. Convert the Field value to that new type and encode that value:

type x Field
if err := encoder.Encode(x(f)); err != nil {
    return nil, err
}

https://go.dev/play/p/hA8dkwgDc-S

Reasons:
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Frank Martingale