use __json__ attribute to encode custom objects

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.