JSON Marshalling fails when an object has a field of type bson.ObjectId

81 views
Skip to first unread message

Travis Reeder

unread,
Jul 22, 2011, 6:07:46 PM7/22/11
to mgo-...@googlegroups.com
Here's an example error:

2011/07/22 15:03:38 http: panic serving 127.0.0.1:36734: json: invalid UTF-8 in string: "N \xdd\xfc\\\r\xd2\rW\x00\x00\x05"
  
If I remove the field(s) like Id for instance:

Id  bson.ObjectId  "_id/c"

then it works no problem. Any ideas how to fix this?


Gustavo Niemeyer

unread,
Jul 23, 2011, 7:35:24 PM7/23/11
to mgo-...@googlegroups.com
Hey Travis,

> Here's an example error:
> 2011/07/22 15:03:38 http: panic serving 127.0.0.1:36734: json: invalid UTF-8
> in string: "N \xdd\xfc\\\r\xd2\rW\x00\x00\x05"

Unlike BSON, JSON can't take raw binary data. If you want to encode,
you'll have to encode in a more friendly format to JSON..

Hmmm, gobson can actually give a hand there, by encoding the id it as
a hash via the marshaller interface. Would that be useful to you?

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I never filed a patent.

Travis Reeder

unread,
Jul 23, 2011, 8:31:36 PM7/23/11
to mgo-...@googlegroups.com
Ya, that would definitely be helpful if it could implement the Marshaller and Unmarshaler interfaces. I think the best output would be for it to just call String() on the ObjectId and when unmarshalling if it sees an ObjectID it would create it with ObjectIdHex(string).

Gustavo Niemeyer

unread,
Jul 23, 2011, 8:42:56 PM7/23/11
to mgo-...@googlegroups.com
> Ya, that would definitely be helpful if it could implement the Marshaller
> and Unmarshaler interfaces. I think the best output would be for it to just
> call String() on the ObjectId and when unmarshalling if it sees an ObjectID
> it would create it with ObjectIdHex(string).

Sounds good, I'll see what I can do about that. Marshaller is easy, the
unmarshaller one requires a bit of thinking, but may be doable as well.

Reply all
Reply to author
Forward
0 new messages