我应该使用 from_xml 还是 from_json 来导入对象?

发布于 2024-09-15 19:18:30 字数 333 浏览 7 评论 0 原文

我目前有一个 PHP 应用程序,我将用 Rails 重写它。我的想法是慢慢用 Rails 替换所有 PHP 功能,因此两者都需要并行运行(我想我已经弄清楚了这部分)。

现在,目前,我们的每个客户端都有一个单独的数据库,事实证明,如果我们可以在客户端之间导入/导出某些对象(基本上为它们设置一些默认对象或在类似社区的方式)。

因此,我的想法是,这是我的 Rails 集成的一个很好的起点,特别是因为它具有 from_xmlfrom_json 等方法。我的问题是:您认为这两者中哪一个更好?我想听听从向后兼容性、可靠性、速度等角度的争论。

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2024-09-22 19:18:30

以下是XML 和 JSON 之间的一些比较

聪明人说过的话:

我自己的想法:

  • 通常 JSON 较小,但这取决于 JSON 和 XML 结构的设计程度(例如,由人类或代码生成设计)。带宽对您来说有多重要?如果您使用即时压缩,则大小就不那么重要了,这如果带宽很重要,你应该这样做。
  • XML 拥有更多可用的验证支持(例如 DTD、XML Schema、RelaxNG 等),尽管 JSON 正在迎头赶上。您对数据验证有什么需求?
  • 从上面的比较可以看出,解析 JSON 比解析 XML 更快。如果您有大量数据集,这一点很重要。
  • 灵活性:XML 提供了额外的功能,例如外部实体和 ID 类型,而 JSON 则没有。如果您不需要它,请忽略它。可能会降低可移植性,因为并非所有 XML 解析器都必须支持这些功能。
  • 易于加工:两者相似。虽然您可以使用 JavaScript eval() 解析 JSON,但它不可靠且不安全。因此这两种格式都需要调用库来解析数据。
  • JSON 是为 JavaScript 设计的,非常适合,特别是在访问数据片段时。如果考虑到其他语言的可移植性,JSON 的便利性就会减弱,尽管现在大多数语言都有 JSON 接口。 XML 与平台无关:大多数语言都有 XML 的 DOM 接口,或者更好的是 XPath 接口(例如 jQuery,dojo)。
  • 可扩展性(我认为这与您所说的向后兼容性相匹配):如果设计成这样,那么两者都是可扩展的,例如通过使用命名属性/元素/属性而不是位置。
  • 可靠性:我没有看到显着差异。

涉足这个话题有点像小心翼翼地走进雷区。我在上面已经尽力做到公平了。披露:我更倾向于 XML 粉丝,尽管在某些情况下我也会使用 JSON。两者肯定都有一席之地。欢迎对上述内容有不同意见...请用事实而不是仇恨来支持他们。我相信许多人比我更了解这个话题。

Here are some comparisons between XML and JSON.

What smart people have said:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文