Should I use from_xml or from_json for importing objects?
I currently have a PHP app that I'm going to be rewriting in Rails. My idea is to slowly replace all of the PHP functionality with Rails, so both will need to 开发者_开发问答run side-by-side (I think I've got this part figured out).
Now, at the moment, each of our client's has a separate database, and as it turns out, it would be really great if we could import/export certain objects between our clients (basically setup some default objects for them or share them in a community-like manner).
So, my thinking is that this is a great starting point for my Rails integration, especially since it has methods like from_xml
and from_json
. My question is: of those two, which one do you think would be better? I'd like to hear the arguments from the perspectives of backwards compatibility, reliability, speed, etc.
Thanks very much!
Here are some comparisons between XML and JSON.
What smart people have said:
- Debate: JSON vs. XML as a data interchange format
- Why JSON vs XML is a yawn
My own thoughts:
- Typically JSON is smaller, but that depends on how well the JSON and XML structures are designed (e.g. by human or by code generation). How important is bandwidth to you? Size matters less if you are using on-the-fly compression, which you should be if bandwidth matters.
- XML has a lot more validation support available (e.g. DTD, XML Schema, RelaxNG, etc.), although JSON is catching up. What are your needs for data validation?
- From the comparisons above, it appears that parsing JSON is faster than parsing XML. This matters if you have large data sets.
- Flexibility: XML offers bells and whistles, like external entities and ID types, that JSON doesn't. Ignore this if you don't need it. Can reduce portability since not all XML parsers have to support these features.
- Ease of processing: both are similar. Though you can parse JSON with a javascript eval(), it's unreliable and unsafe. So both formats require a library call to parse the data.
- JSON is made for javascript and fits like a glove, especially for accessing the pieces of the data. If portability to other languages is a factor, JSON's convenience diminishes, though there are JSON interfaces for most languages now. XML is more platform-agnostic: most languages have a DOM interface for XML, or better, an XPath interface (e.g. jQuery, dojo).
- Extensibility (which I think matches what you mean by backward compatibility): both are extensible if designed to be that way, e.g. by using named properties/elements/attributes instead of position.
- Reliability: I don't see a significant difference.
Wading into this topic is somewhat like tiptoeing into a minefield. I've tried to be fair above. Disclosure: I tend to be more of an XML fan, though I use JSON too in certain circumstances. There is definitely a place for both. Differing opinions on the above are welcome... please support them with facts and without rancor. I'm sure many on SO know more about this topic than I do.
精彩评论