mgo icon indicating copy to clipboard operation
mgo copied to clipboard

JSON Marshalling for Decimal128 type

Open Dragomir-Ivanov opened this issue 5 years ago • 7 comments

Hi there, How open are maintainers of this project on adding JSON Marshaling for Decimal type. It is pretty simple change:

func (d *Decimal128) UnmarshalJSON(b []byte) error {
	str, err := strconv.Unquote(string(b))
	if err != nil {
		return err
	}
	*d, err = ParseDecimal128(str)
	return err
}

func (d Decimal128) MarshalJSON() ([]byte, error) {
	return []byte(strconv.Quote(d.String())), nil
}

Dragomir-Ivanov avatar Apr 21 '19 09:04 Dragomir-Ivanov

Probably it would be a bit more elegant to add encoding.TextMarshaler which is respected by encoding/json.

It's a common way to provide a general marshalling logic which is repected by many other marshallers, e.g. https://github.com/go-yaml/yaml (https://github.com/go-yaml/yaml/blob/v2/encode.go#L135)

yalegko avatar Apr 24 '19 11:04 yalegko

Thanks for the reply @yalegko , you may be right. I will make a PR for implementing encoding.TextMarshaler. I see there are lot of PRs in the queue, but no commits to master last 6 months. Is this project alive?

Dragomir-Ivanov avatar Apr 25 '19 13:04 Dragomir-Ivanov

I will make a PR for implementing

Woops, looks like I was too impatient, sorry :)

Is this project alive?

No idea, but suppose it uses development -> release model and dev branch seems fresh enough

yalegko avatar Apr 25 '19 13:04 yalegko

Just made the same PR, but you put an effort for the tests :laughing: We are cool. I will close my PR :rofl:

Dragomir-Ivanov avatar Apr 25 '19 13:04 Dragomir-Ivanov

I really like how Go made our 2 implementations to look exactly the same.

Dragomir-Ivanov avatar Apr 25 '19 13:04 Dragomir-Ivanov

Hi @Dragomir-Ivanov and @yalegko,

Thanks a lot for taking the time to implement this, and sorry for the delay!

We are planning on cutting a last release shortly after which we'll stop supporting this repository, since the official driver is now out and available (https://github.com/mongodb/mongo-go-driver/).

We are sorting out all the existing PRs, and we'll run a battery of performance tests to make sure we don't degrade the driver. Until then, please feel free to use development or the new mgo official driver.

Thanks again! Esther

eminano avatar May 16 '19 13:05 eminano

Hi @eminano, no problem for the delay, and thanks for merging into development.

Dragomir-Ivanov avatar May 16 '19 19:05 Dragomir-Ivanov