Calling json.Unmarshal Inside UnmarshalJSON Function without Stack Overflow
In custom UnmarshalJSON implementations, invoking json.Unmarshal(b, type) can lead to stack overflows. This occurs because the JSON decoder repeatedly checks for custom UnmarshalJSON implementations, resulting in endless recursion.
To avoid this issue, create a new type using the type keyword and assign your original value to it using a type conversion. This is possible because the new type has the original type as its underlying type.
Example:
Suppose we have a Person type with an Age field. To ensure that the Age cannot be negative, we can implement UnmarshalJSON as follows:
type Person struct { Name string `json:"name"` Age int `json:"age"` } func (p *Person) UnmarshalJSON(data []byte) error { type person2 Person if err := json.Unmarshal(data, (*person2)(p)); err != nil { return err } // Post-processing after unmarshaling: if p.Age < 0 { p.Age = 0 } return nil }
In this approach, type person2 creates a new type with no methods, preventing recursion. When data is unmarshaled, it is assigned to the person2 type and then to the original Person type, allowing for post-processing.
Test:
import ( "encoding/json" "fmt" ) func main() { var p *Person fmt.Println(json.Unmarshal([]byte(`{"name":"Bob","age":10}`), &p)) fmt.Println(p) fmt.Println(json.Unmarshal([]byte(`{"name":"Bob","age":-1}`), &p)) fmt.Println(p) }
Output:
<nil> &{Bob 10} <nil> &{Bob 0}
This demonstrates how to customize UnmarshalJSON without causing stack overflows.
The above is the detailed content of How to Avoid Stack Overflow When Using json.Unmarshal Inside UnmarshalJSON?. For more information, please follow other related articles on the PHP Chinese website!