You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Cory Zue <cz...@dimagi.com> on 2010/05/14 10:51:15 UTC

_update, XML, and POST

Hi folks,

I am prototyping an app to receive an XML POST and convert the data to
a couch object.  Based on what I've read so far it seems like the
_update method is designed to let you do exactly this.  However, I
have some questions (note that I haven't tried any of this yet, am
just basing my information off documentation on-line.

>From here: http://wiki.apache.org/couchdb/Document_Update_Handlers I
understand that "To invoke a handler, one must "PUT" the document
against the handler function itself (POST does not seem to be
supported)."  Was there any reasoning behind this decision or was it
just a feature that never made it?  Not allowing these things to run
on POSTs seems to drastically reduce their utility for no reason
that's obvious to me.

Second, is there a community recommended XML to JSON library to
perform the data transfer?  I've been evaluating several (listed
below), none of which seem to be highly active, recent, or well
established.  Anyone done this before and want to make a
recommendation?  I'm surprised that there's not a de-facto choice for
this.

http://www.thomasfrank.se/xml_to_json.html
http://goessner.net/download/prj/jsonxml/
http://www.codeproject.com/KB/scripting/XmlToJson.aspx
http://michael.hinnerup.net/blog/2008/01/26/converting-json-to-xml-and-xml-to-json/

thanks,
Cory

Re: _update, XML, and POST

Posted by Paul Bonser <mi...@gmail.com>.
On Fri, May 14, 2010 at 3:51 AM, Cory Zue <cz...@dimagi.com> wrote:
> Hi folks,
>
> I am prototyping an app to receive an XML POST and convert the data to
> a couch object.  Based on what I've read so far it seems like the
> _update method is designed to let you do exactly this.  However, I
> have some questions (note that I haven't tried any of this yet, am
> just basing my information off documentation on-line.
>
> From here: http://wiki.apache.org/couchdb/Document_Update_Handlers I
> understand that "To invoke a handler, one must "PUT" the document
> against the handler function itself (POST does not seem to be
> supported)."  Was there any reasoning behind this decision or was it
> just a feature that never made it?  Not allowing these things to run
> on POSTs seems to drastically reduce their utility for no reason
> that's obvious to me.

Actually, I'm pretty sure that statement is no longer true. I was
testing update handlers recently and it accepted POSTs just fine.

>
> Second, is there a community recommended XML to JSON library to
> perform the data transfer?  I've been evaluating several (listed
> below), none of which seem to be highly active, recent, or well
> established.  Anyone done this before and want to make a
> recommendation?  I'm surprised that there's not a de-facto choice for
> this.
>
> http://www.thomasfrank.se/xml_to_json.html
> http://goessner.net/download/prj/jsonxml/
> http://www.codeproject.com/KB/scripting/XmlToJson.aspx
> http://michael.hinnerup.net/blog/2008/01/26/converting-json-to-xml-and-xml-to-json/
>
> thanks,
> Cory
>



-- 
Paul Bonser
http://probablyprogramming.com

Re: _update, XML, and POST

Posted by Nils Breunese <N....@vpro.nl>.
Cory Zue wrote:

> On Fri, May 14, 2010 at 12:40 PM, Nils Breunese<N....@vpro.nl>  wrote:
>> Cory Zue wrote:
>>
>> Actually I'm not really surprised, since XML is a more complex data
>> format than JSON, so there is no true 1-to-1 mapping. Consider XML
>> attributes and elements for instance. Initially you might think mapping
>> them to JSON name/value pairs is a good idea, but attributes and
>> elements can have the same name:
>>
>> <person problem="yes">
>>   <problem>This is a problem.</problem>
>>   <name>Nils</name>
>> </person>
>>
>> How would you represent this in JSON? You might prefix attributes with
>> underscores or something, but it starts getting clunky fast.
>
> I was thinking of the xsd way of dealing with this by prefixing
> attributes with "@", which in your case would look like:
>
> {
>   "@problem": "yes"
>   "type": "person",
>   "problem": "This is a problem.",
>   "name": "Nils"
> }

Yeah, you could use an "@" instead of an underscore, but that still
wouldn't get you the above I guess. I think you'd get a nested structure
like this:

{
   "person": {
     "@problem": "yes",
     "problem": "This is a problem.",
     "name": "Nils"
   }
}

So I'd prefer a custom transformation myself anyway.

Nils.

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Re: _update, XML, and POST

Posted by Cory Zue <cz...@dimagi.com>.
On Fri, May 14, 2010 at 12:40 PM, Nils Breunese <N....@vpro.nl> wrote:
> Cory Zue wrote:
>
> Actually I'm not really surprised, since XML is a more complex data
> format than JSON, so there is no true 1-to-1 mapping. Consider XML
> attributes and elements for instance. Initially you might think mapping
> them to JSON name/value pairs is a good idea, but attributes and
> elements can have the same name:
>
> <person problem="yes">
>  <problem>This is a problem.</problem>
>  <name>Nils</name>
> </person>
>
> How would you represent this in JSON? You might prefix attributes with
> underscores or something, but it starts getting clunky fast.

I was thinking of the xsd way of dealing with this by prefixing
attributes with "@", which in your case would look like:

{
 "@problem": "yes"
 "type": "person",
 "problem": "This is a problem.",
 "name": "Nils"
}

However, I guess I'm just surprised no one's decided to standardize
(and implement) this conversion yet.

Cory

Re: _update, XML, and POST

Posted by Nils Breunese <N....@vpro.nl>.
Cory Zue wrote:

> Second, is there a community recommended XML to JSON library to
> perform the data transfer?  I've been evaluating several (listed
> below), none of which seem to be highly active, recent, or well
> established.  Anyone done this before and want to make a
> recommendation?  I'm surprised that there's not a de-facto choice for
> this.

Actually I'm not really surprised, since XML is a more complex data
format than JSON, so there is no true 1-to-1 mapping. Consider XML
attributes and elements for instance. Initially you might think mapping
them to JSON name/value pairs is a good idea, but attributes and
elements can have the same name:

<person problem="yes">
   <problem>This is a problem.</problem>
   <name>Nils</name>
</person>

How would you represent this in JSON? You might prefix attributes with
underscores or something, but it starts getting clunky fast.

For CouchDB purposes I'd want to map the XML above into a custom
structure anyway, so I guess I'd just write my own custom conversion
anyway (map root element to type property, omit the problem attribute)
to produce something like:

{
   "type": "person",
   "problem": "This is a problem.",
   "name": "Nils"
}

Converting JSON to XML is easy, the other way around is not trivial.

Nils.

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.