为什么在XML中嵌入JSON不好?

时间:2022-06-24 11:37:17

My gut tells me that putting one format in another is wrong, but I can't seem to come up with concrete reasons.

我的直觉告诉我,把一种格式转换成另一种格式是错误的,但我似乎想不出具体的原因。

<root>
 <stuff>
  thing
 </stuff>
 <more>
  <[!CDATA[{"a":["b","c"]}]]>
 </more>
</root>

versus just putting it in the xml

而不是把它放到xml中

<root>
 <stuff>
  thing
 </stuff>
 <more>
  <a>
   b
  </a>
  <a>
   c
  </a>
 </more>
</root>

The two sections are logically going to be parsed by different code, but as an interchange format, is it ok to mix and match syntax?

这两个部分在逻辑上将被不同的代码解析,但是作为交换格式,混合和匹配语法是否可以?

Does your answer change if we have an existing endpoint that parses the JSON response? We would have to recode this endpoint for XML ingestion.

如果我们有一个解析JSON响应的现有端点,您的答案会改变吗?我们必须重新编码这个端点以获取XML。

3 个解决方案

#1


21  

As an interchange format using two formats puts extra burden on people who want to inter-operate with you. Now they need to have an XML parser and a JSON parser.

作为一种使用两种格式的交换格式,希望与您进行互操作的人将承受额外的负担。现在他们需要一个XML解析器和一个JSON解析器。

It also makes it harder for people to grok the format, as they have to mentally switch gears when thinking about different parts of your file.

这也使得人们更难理解这种格式,因为他们在考虑你文件的不同部分时,必须在思想上转换思维方式。

Finally, you won't be able to easily do things that look at the entire structure at once. For example, you can't use XPath to grab JSON bits, nor can you treat the entire response as a JavaScript object. By mixing two formats you get a "worst of both worlds" problem when it comes to manipulating the data.

最后,您不能轻松地同时查看整个结构。例如,不能使用XPath获取JSON位,也不能将整个响应视为JavaScript对象。在处理数据时,混合两种格式会导致“两个世界中最糟糕的”问题。

#2


7  

It's sorta like the database normalization debate. It's cleaner and more elegant to do everything in pure XML (or normalize your database schema), that way you're not unnecessarily coupled to your particular implementation. But if you have to then convert the XML to JavaScript objects (or join 5 tables for every damned SELECT), you may end up writing lots of extra code and incur unnecessary performance hits.

这有点像数据库规范化辩论。使用纯XML(或规范化数据库模式)做任何事情都更简洁、更优雅,这样就不会不必要地耦合到特定的实现中。但是,如果您必须将XML转换为JavaScript对象(或者为每个该死的选择加入5个表),那么您最终可能会编写大量额外的代码并导致不必要的性能损失。

It all depends on how you balance convenience with formal correctness. If this is an XML interchange format that will be standardized by the W3C and used by millions then dear God, do not use JSON. If this is for an in-house app that'll only be processed by code you yourself have written then screw it, just throw the JSON in there and move on!

这完全取决于你如何平衡方便和形式正确性。如果这是一种XML交换格式,将由W3C标准化,并由数百万人使用,那么,亲爱的上帝,请不要使用JSON。如果这是一个内部的应用程序,它只能由您自己编写的代码来处理,那么就把JSON扔进去,然后继续!

#3


3  

In my opinion XML is the prefered Data transfer representation, but JSON is far more expressive when it comes to Maps and Arrays. I would have no issue with embedding JSON into xml to represent a list or map.

在我看来,XML是首选的数据传输表示,但在映射和数组方面,JSON的表达能力要强得多。将JSON嵌入xml以表示列表或映射,我不会有任何问题。

#1


21  

As an interchange format using two formats puts extra burden on people who want to inter-operate with you. Now they need to have an XML parser and a JSON parser.

作为一种使用两种格式的交换格式,希望与您进行互操作的人将承受额外的负担。现在他们需要一个XML解析器和一个JSON解析器。

It also makes it harder for people to grok the format, as they have to mentally switch gears when thinking about different parts of your file.

这也使得人们更难理解这种格式,因为他们在考虑你文件的不同部分时,必须在思想上转换思维方式。

Finally, you won't be able to easily do things that look at the entire structure at once. For example, you can't use XPath to grab JSON bits, nor can you treat the entire response as a JavaScript object. By mixing two formats you get a "worst of both worlds" problem when it comes to manipulating the data.

最后,您不能轻松地同时查看整个结构。例如,不能使用XPath获取JSON位,也不能将整个响应视为JavaScript对象。在处理数据时,混合两种格式会导致“两个世界中最糟糕的”问题。

#2


7  

It's sorta like the database normalization debate. It's cleaner and more elegant to do everything in pure XML (or normalize your database schema), that way you're not unnecessarily coupled to your particular implementation. But if you have to then convert the XML to JavaScript objects (or join 5 tables for every damned SELECT), you may end up writing lots of extra code and incur unnecessary performance hits.

这有点像数据库规范化辩论。使用纯XML(或规范化数据库模式)做任何事情都更简洁、更优雅,这样就不会不必要地耦合到特定的实现中。但是,如果您必须将XML转换为JavaScript对象(或者为每个该死的选择加入5个表),那么您最终可能会编写大量额外的代码并导致不必要的性能损失。

It all depends on how you balance convenience with formal correctness. If this is an XML interchange format that will be standardized by the W3C and used by millions then dear God, do not use JSON. If this is for an in-house app that'll only be processed by code you yourself have written then screw it, just throw the JSON in there and move on!

这完全取决于你如何平衡方便和形式正确性。如果这是一种XML交换格式,将由W3C标准化,并由数百万人使用,那么,亲爱的上帝,请不要使用JSON。如果这是一个内部的应用程序,它只能由您自己编写的代码来处理,那么就把JSON扔进去,然后继续!

#3


3  

In my opinion XML is the prefered Data transfer representation, but JSON is far more expressive when it comes to Maps and Arrays. I would have no issue with embedding JSON into xml to represent a list or map.

在我看来,XML是首选的数据传输表示,但在映射和数组方面,JSON的表达能力要强得多。将JSON嵌入xml以表示列表或映射,我不会有任何问题。