Skip to content

Conversation

@gngrchrl
Copy link

@gngrchrl gngrchrl commented Dec 4, 2022

We have a bug in the current enum rendering system where the MarshalJSON method is never called on pointer structs. This fixes this behavior (quiet easily).

import (
	"encoding/json"
	"fmt"
)

type Enum struct {
	value string
}

type Enum2 struct {
	valie string
}

func (e Enum) MarshalJSON() ([]byte, error) {
	return []byte("\"another_value\""), nil
}

func (e *Enum2) MarshalJSON() ([]byte, error) {
	return []byte("\"another_value\""), nil
}

type Test struct {
	A Enum
	C Enum2
	B int
}

func main() {
	x := Test{
		A: Enum{},
		C: Enum2{},
		B: 1,
	}
	j, err := json.Marshal(x)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(j))
}

Prints:

{"A":"another_value","C":{},"B":1}

Go playground example: https://go.dev/play/p/8QQzFwO9a-K

@karitham karitham merged commit 8ca3a5b into discord-gophers:main Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants