gnecula / bond

Spy-based testing library.
http://necula01.github.io/bond/
Other
7 stars 3 forks source link

How to serialize object instances #5

Closed gnecula closed 9 years ago

gnecula commented 9 years ago

In the old Bond I have a custom serializer that the json module invokes when it cannot serialize the object. Then I look for a special method toJSON, and if I find it then I serialize. What should we do in the new version?

xkrogen commented 9 years ago

I like objects having their own toJSON method, but it might be nice to additionally make it so you can specify a serializer in the test (though maybe formatter can take this role?). toJSON generally provides nice OOP-style encapsulation but if it's only for testing purposes then you're doing a small amount of production code pollution.

Is toJSON standard in Python (e.g. to_json is standard in Ruby because the json stdlib uses it)? If not then I don't feel quite as good about it

gnecula commented 9 years ago

Unfortunately there is nothing standard for this purpose in Python. And you are right that the formatter can play this role.

George.

On Tue, Oct 13, 2015 at 1:13 PM, Erik notifications@github.com wrote:

I like objects having their own toJSON method, but it might be nice to additionally make it so you can specify a serializer in the test (though maybe formatter can take this role?). toJSON generally provides nice OOP-style encapsulation but if it's only for testing purposes then you're doing a small amount of production code pollution.

Is toJSON standard in Python (e.g. to_json is standard in Ruby because the json stdlib uses it)? If not then I don't feel quite as good about it

— Reply to this email directly or view it on GitHub https://github.com/necula01/bond/issues/5#issuecomment-147838901.

xkrogen commented 9 years ago

We can also just call str() on the object as a default? And if you want something more expressive then you can specify it in the formatter?

gnecula commented 9 years ago

I think that it is actually reasonable to write a to_json method on objects for the purpose of testing. It is true that it is in production code but this code really belongs in that class. Let's use the name "to_json" the same one for both Ruby and Python.

gnecula commented 9 years ago

I had to bite this bullet for the project I just converted. I am using to_json. Let's use the same for Ruby.

xkrogen commented 9 years ago

Not yet sure how to do this in Ruby, since the built-in JSON serializer doesn't have the same option as Python for accepting a custom serializer. I will investigate further when I have a chance. Hopefully since to_json is somewhat built-in, it will play nicely.

gnecula commented 9 years ago

I thought that you said in a past message that the serializer will use to_json if it exists.

George.

On Mon, Oct 19, 2015 at 2:09 PM, Erik notifications@github.com wrote:

Not yet sure how to do this in Ruby, since the built-in JSON serializer doesn't have the same option as Python for accepting a custom serializer. I will investigate further when I have a chance.

— Reply to this email directly or view it on GitHub https://github.com/necula01/bond/issues/5#issuecomment-149348241.

xkrogen commented 9 years ago

Okay, that's true, I just got confused for a bit because to_json needs to have an argument. Things will work out of the box in Ruby; I'll update the documentation to reflect this.