Home > Backend Development > Golang > How Can I Add Arbitrary Fields to JSON Output for Unknown Go Structs?

How Can I Add Arbitrary Fields to JSON Output for Unknown Go Structs?

Patricia Arquette
Release: 2024-12-16 06:10:10
Original
702 people have browsed it

How Can I Add Arbitrary Fields to JSON Output for Unknown Go Structs?

Extending JSON Output for Unknown Structs

Problem:

When dealing with unknown structs or interfaces, adding arbitrary fields to JSON output can be challenging. Demonstrated code demonstrates this issue. How can we replicate the desired JSON output for unknown structs?

Solution 1: Dynamic Type Generation using Reflection

One approach involves using reflection to generate a dynamic type at runtime. This type is a struct with an anonymous field of the wrapped interface type and an extra field. By manipulating this dynamic type, we can achieve the desired JSON output.

func printInterface(val interface{}) {
    t2 := reflect.StructOf([]reflect.StructField{
        {Name: "X", Anonymous: true, Type: reflect.TypeOf(val)},
        {Name: "Extra", Type: reflect.TypeOf("")},
    })

    v2 := reflect.New(t2).Elem()
    v2.Field(0).Set(reflect.ValueOf(val))
    v2.FieldByName("Extra").SetString("text")

    json.NewEncoder(os.Stdout).Encode(v2.Interface())
}
Copy after login

Solution 2: Marshal-Unmarshal-Marshal

An alternative solution involves marshalling the input value, unmarshalling it into a map, adding the extra field, and marshalling it again.

func printInterface(val interface{}) error {
    data, err := json.Marshal(val)
    if err != nil { return err }

    v2 := map[string]interface{}{}
    if err := json.Unmarshal(data, &v2); err != nil { return err }

    v2["Extra"] = "text"
    return json.NewEncoder(os.Stdout).Encode(v2)
}
Copy after login

Both solutions allow us to add arbitrary fields to JSON output for unknown structs. The first solution is more sophisticated and offers better control, while the second solution is simpler but less performant due to dual marshalling. Ultimately, the choice of solution depends on the specific requirements and priorities of the use case.

The above is the detailed content of How Can I Add Arbitrary Fields to JSON Output for Unknown Go Structs?. 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