# json: invalid recursive type: type MyStruct has recursive JSON method

- **ID:** `go/json-marshal-infinite-recursion`
- **Domain:** go
- **Category:** encoding_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

A custom MarshalJSON or UnmarshalJSON method on a type calls json.Marshal or json.Unmarshal on itself, causing infinite recursion.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Go 1.15 | active | — | — |
| Go 1.16 | active | — | — |
| Go 1.17 | active | — | — |
| Go 1.18 | active | — | — |
| Go 1.19 | active | — | — |
| Go 1.20 | active | — | — |
| Go 1.21 | active | — | — |
| Go 1.22 | active | — | — |
| Go 1.23 | active | — | — |

## Workarounds

1. **Define an alias type (type MyStructAlias MyStruct) and marshal the alias instead of the original type** (95% success)
   ```
   Define an alias type (type MyStructAlias MyStruct) and marshal the alias instead of the original type
   ```
2. **Use a raw byte slice or map[string]interface{} for manual marshaling without calling json.Marshal on self** (90% success)
   ```
   Use a raw byte slice or map[string]interface{} for manual marshaling without calling json.Marshal on self
   ```
3. **Implement MarshalJSON by calling json.Marshal on a temporary struct that mirrors the fields without the recursive method** (85% success)
   ```
   Implement MarshalJSON by calling json.Marshal on a temporary struct that mirrors the fields without the recursive method
   ```

## Dead Ends

- **** — Both functions call the same MarshalJSON method; recursion persists. (95% fail)
- **** — Loses custom serialization behavior; data may be serialized incorrectly. (70% fail)
- **** — json package detects recursion before runtime depth limit; counter doesn't prevent detection. (80% fail)
