namedtuple support for simplejson #1

merged 8 commits into from Jul 4, 2011


None yet
2 participants

I need to serialize namedtuples to JSON and retain the attribute names. This StackOverflow question shows a way to do that by subclassing JSONEncoder, but it no longer works with the latest simplejson code. So, I've put together a patch to add this features, along with some unit tests to be sure it works right. Please take a look, and if you like it, pull it in!

(I didn't see an AUTHORS file for this project: I would like to note my contribution to this project, as well as where my code changes were inspired from. Do you want to add a file like that?)


Singletoned commented Jul 4, 2011

I'm happy to accept this as it's good work, but this is just a fork I made, so that I could implement a feature to optionally encode tuples to objects.

You probably want to issue a pull request to which I believe is the main branch.

I would recommend adding an AUTHORS file and adding yourself to it, Bob can always reject that commit if he doesn't want it.

Also I should probably issue a pull request to get my changes merged in.

@Singletoned Singletoned added a commit that referenced this pull request Jul 4, 2011

@Singletoned Singletoned Merge pull request #1 from singingwolfboy/master
namedtuple support for simplejson

@Singletoned Singletoned merged commit 40e3e05 into Singletoned:master Jul 4, 2011

Oops, that was silly of me. Thanks for pulling in my changes: I'm not quite sure how I'd ask Git to apply them to a different repository. I see that you created a merge request that includes my changes: thanks for that.


Singletoned commented Jul 4, 2011

Having played with your changes, I'm guessing you wrote them under 3.x, and you need to test them under 2.x. For me the tests don't even compile under 2.6 as you are using features of unittest that were only added in python 3.1.

If I fix the imports, the code doesn't seem to work under 2.6. I'll have more of a look into it this evening.

I wrote them under Python 2.7, and they work under that version. You can probably just take out the unittest.skipUnless line, which I'm guessing is what's screwing it up.

Ah, the problem was the class decorator. I've removed that and tested with Python 2.5: it seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment