Home > Backend Development > Golang > How to Avoid Stack Overflow When Using json.Unmarshal Inside UnmarshalJSON?

How to Avoid Stack Overflow When Using json.Unmarshal Inside UnmarshalJSON?

Barbara Streisand
Release: 2024-12-19 22:50:15
Original
115 people have browsed it

How to Avoid Stack Overflow When Using json.Unmarshal Inside UnmarshalJSON?

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
}
Copy after login

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)
}
Copy after login

Output:

<nil>
&{Bob 10}
<nil>
&{Bob 0}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template