nu7hatch / gouuid

Go binding for libuuid
MIT License
417 stars 160 forks source link

Adds JSON marshalling support #7

Closed pjvds closed 11 years ago

pjvds commented 11 years ago

I am using the gouuid library for the identifiers of my objects. These objects get stored in a document store and are exposed in a REST api. Both API and database marshal the object to the JSON format. Currently the uuid.UUID does not implement the json.Marshaller and json.Unmarshaller interface. This pull request changes that.

I have added the MarshalJSON and UnmarshalJSON to the uuid.UUID type, which are covered by tests. The JSON value is a hex string as returned by the uuid.UUID.String method.

kisielk commented 11 years ago

Might be worth holding off on this till Go 1.2, which will implement a generic Marshalling API that will work for any encoder / decoder.

What I've done in my apps is make my own type:

type UUID uuid.UUID

on which I then put the MarshalJSON and UnmarshalJSON methods.

pjvds commented 11 years ago

Version 1.2 is not released until december and there are still quite some people writing Go 1.0 and 1.1 code. Regarding aliasing the type, I value a properly tested implementation above writing my own.

Do you think it adds to much overhead to the package?

kisielk commented 11 years ago

I don't really have anything against the change, just putting that out there for consideration.

pjvds commented 11 years ago

Good suggestion, although the json.Marshal and json.Unmarshal take some steps before they actually start working on the marshalling. Also this gives the ability to return better error messages.

nu7hatch commented 11 years ago

I would prefer to hold this until unified marshaling API is released. I'd like to keep this lib as simple as possible. Also, i really don't like the implementation pushed along with this pull request, @kisielk comments explain it.