In the XML response I noticed that arrays were transformed into
repeating XML elements, always named <resource> That's due to the
emitter code here: http://bitbucket.org/jespern/django-piston/src/cbe9f56497ec/piston/emitters.py#cl-346
So I created a new emitter inheriting from this. I changed it so that
when it encounters an array it takes the name of the containing dict
key. So { fruit : [ 'apple', 'banana', 'pear'] } gets emitted as
<fruit>apple</fruit><fruit>banana</fruit><fruit>pear</fruit>
I'm trying to work out if this was a clever thing to do, or something
completely stupid
My next problem is that the XML elements are not output in any
particular order, which is due to dicts being unordered. I could come
up with some even more hacky solutions to this problem, but has anyone
else tackled these things? are most people are using JSON?
When it comes to element ordering, if that's important to you, you can
use an ordered dict, like http://docs.python.org/dev/library/collections.html#collections.OrderedDict.
It's unfortunately new in Python 2.7, but there are a few others you
can use:
* http://www.voidspace.org.uk/python/odict.html
* http://code.activestate.com/recipes/496761/
* http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py
* http://www.xs4all.nl/~anthon/Python/ordereddict/
* http://pypi.python.org/pypi/StableDict/0.2
HTH,
Jesper
On Feb 9, 2:26 pm, Harry Wood <harry.w...@gmail.com> wrote:
> I created a simple piston API which doesn't use any django models (no
> db involved) and just does the "business logic" in the handler. So
> rather than referencing '.objects' on a model, I'm directly building
> up a dict to match my desired response structures. I'm a django
> newbie, and python beginner so please let me know if this is the wrong
> way to do things.
>
> In the XML response I noticed that arrays were transformed into
> repeating XML elements, always named <resource> That's due to the
> emitter code here:http://bitbucket.org/jespern/django-piston/src/cbe9f56497ec/piston/em...
So using an OrderedDict would work.
Only thing is, later on I do plan to create some database-backed API
operations using django models. I guess I'll be snookered then because
model '.objects' always results in normal python dict (?)
I'm trying to stick to the most standard approach. Nobody else is
worrying much about output field ordering? (I know in *theory* it
shouldn't matter, but in practice it's an API readability issue at
least)
On Feb 9, 12:45 pm, jespern <jno...@gmail.com> wrote:
> Seeing as it's been difficult to create a versatile XML emitter, you
> are correct in subclassing it.
>
> When it comes to element ordering, if that's important to you, you can
> use an ordered dict, likehttp://docs.python.org/dev/library/collections.html#collections.Order....
>
> It's unfortunately new in Python 2.7, but there are a few others you
> can use:
>
> *http://www.voidspace.org.uk/python/odict.html
> *http://code.activestate.com/recipes/496761/
> *http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py
> *http://www.xs4all.nl/~anthon/Python/ordereddict/
> *http://pypi.python.org/pypi/StableDict/0.2
Jesper
On Feb 9, 3:54 pm, Harry Wood <harry.w...@gmail.com> wrote:
> I wondered whether the 'fields' setting would help with ordering in
> the outputhttp://bitbucket.org/jespern/django-piston/wiki/Documentation#fieldse...