globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 231 forks source link

JSON Marshalling for Decimal128 type #350

Open Dragomir-Ivanov opened 5 years ago

Dragomir-Ivanov commented 5 years ago

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
}
yalegko commented 5 years ago

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)

Dragomir-Ivanov commented 5 years ago

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?

yalegko commented 5 years ago

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

Dragomir-Ivanov commented 5 years ago

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 commented 5 years ago

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

eminano commented 5 years ago

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

Dragomir-Ivanov commented 5 years ago

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