Encode custom objects with __json__

Encoding custom objects in an agonstic manner is difficult right now,
since there's no way to ensure that a custom encoder will be used. This
patch lets custom objects use a __json__ method to output an appropriate
value.