Home > Backend Development > Golang > Golang's json.Unmarshal does not explicitly set a pointer value to nil if the field does not exist in the JSON

Golang's json.Unmarshal does not explicitly set a pointer value to nil if the field does not exist in the JSON

WBOY
Release: 2024-02-09 08:12:08
forward
866 people have browsed it

如果 JSON 中不存在该字段,Golang 的 json.Unmarshal 不会显式将指针值设置为 nil

According to PHP editor Zimo’s introduction, when json.Unmarshal in Golang parses JSON data, if a field does not exist in the JSON, it will not make the pointer value explicit Set to nil. This means that even if a field is missing from the JSON, the pointer will still retain its original value, potentially causing errors or exceptions in your program. Therefore, when using json.Unmarshal, we need to pay special attention to handling missing fields to ensure the stability and correctness of the program.

Question content

Consider this example:

<code>package main

import (
    "encoding/json"
    "fmt"
)

type InternalStructFirst struct {
    Field string `json:"field"`
}
type ExternalStruct struct {
    StructA InternalStructFirst `json:"struct_a"`
    StructB *InternalStructFirst `json:"struct_b"`
}

func main() {
    var structVar ExternalStruct
    string1 := "{\"struct_a\":{\"field\":\"a\"},\"struct_b\":{\"field\":\"b\"}}"

    json.Unmarshal([]byte(string1), &structVar)
    fmt.Printf("first: %+v %+v\n", structVar.StructA, structVar.StructB)

    string2 := "{\"struct_a\":{\"field\":\"c\"}}"
    json.Unmarshal([]byte(string2), &structVar)
    fmt.Printf("second: %+v %+v\n", structVar.StructA, structVar.StructB)

    var structVar2 ExternalStruct
    json.Unmarshal([]byte(string2), &structVar)
    fmt.Printf("third: %+v %+v\n", structVar2.StructA, structVar2
}


</code>
Copy after login

Which outputs:

first: {Field:a} &{Field:b}
second: {Field:c} &{Field:b}
third: {Field:} <nil>
Copy after login

When I execute json.Unmarshal for the first time, the StructB appears in the response and is unmarshaled correctly. But when I do this a second time, it doesn't seem to explicitly set it to nil when the field isn't shown in the response. When doing this on a struct that doesn't have this field set, it does set it to nil (or apparently doesn't set it to something) (as shown in the third example).

If I have set struct_b to non-nil, how can I change my example so that when parsing a JSON string that does not contain a struct_b field, the second json.Unmarshal Will explicitly set StructB to nil?

Solution

You can't. json.Unmarshal Structural fields that are not represented in the input JSON will not be modified. Either start with an empty structure, or set any fields to whatever value you want them to have (if they are not in the JSON input) before calling Unmarshal.

The above is the detailed content of Golang's json.Unmarshal does not explicitly set a pointer value to nil if the field does not exist in the JSON. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template