Skip to content

math/big: big.Rat has not MarshallJSON/UnmarshallJSON methods #64546

@iuliananz

Description

@iuliananz

Go version

go1.21.4

What operating system and processor architecture are you using (go env)?

big.Rat doesn't have MarshallJSON and UnmarshallJSON methods. Is this intentional?

I noticed it while trying to save an object containing a big.Rat field in mongodb, field comes up empty, due to no marshalling.

What did you do?

package main

import (
	"encoding/json"
	"fmt"
	"math/big"
)

type SomeStructure struct {
	Name     string
	RatField big.Rat
}

func main() {
	rat := big.NewRat(10, 3)
	obj := SomeStructure{
		Name:     "Object name",
		RatField: *rat,
	}
	result, err := json.Marshal(obj)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("Json string: %s", string(result))
}

What did you expect to see?

Json string: {"Name":"Object name","RatField":{a: 10, b: 3}}

What did you see instead?

Json string: {"Name":"Object name","RatField":{}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions