JSON 在文件大小和序列化/反序列化时间方面与 XML 相比如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2673367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does JSON compare to XML in terms of file size and serialisation/deserialisation time?
提问by Nick Bolton
I have an application that performs a little slow over the internet due to bandwidth reasons. I have enabled GZip which has improved download time by a significant amout, but I was also considering whether or not I could switch from XML to JSON in order to squeeze out that last bit of performance. Would using JSON make the message size significantly smaller, or just somewhat smaller? Let's say we're talking about 250kB of XML data (which compresses to 30kB).
由于带宽原因,我有一个应用程序在 Internet 上的执行速度有点慢。我启用了 GZip,它大大缩短了下载时间,但我也在考虑是否可以从 XML 切换到 JSON 以挤出最后一点性能。使用 JSON 会使消息大小显着变小,还是稍微小一些?假设我们谈论的是 250kB 的 XML 数据(压缩到 30kB)。
采纳答案by Cheeso
Not an answer, but rather a suggestion to examine your assumptions.
不是答案,而是检查您的假设的建议。
How is JSON smaller?
JSON 如何更小?
JSON:
JSON:
"person":{"firstname":"Fred",
"lastname":"Flintstone",
"age":38,
"spouse":"Wilma" }
XML:
XML:
<person firstname='Fred'
lastname='Flintstone'
age='38'
spouse='Wilma'/>
I just don't see how, in general, a JSON expression is going to be 30% smaller than "equivalent" XML. Even as you ramp up the complexity of these things, with nested structures and arrays, it's not going to be a 30% difference. It's approximately equivalent, with json earning an advantage because the end tag of a nested structure is a } , while XML earns an advantage because it need not quote field names.
我只是不明白,一般来说,JSON 表达式会比“等效的”XML 小 30%。即使您使用嵌套结构和数组增加这些事物的复杂性,也不会有 30% 的差异。它大致等效, json 获得优势,因为嵌套结构的结束标记是 } ,而 XML 获得优势,因为它不需要引用字段名称。
If you forced meto use XML elements, like this:
如果你强迫我使用 XML 元素,像这样:
<person>
<firstname>Fred<firstname>
<lastname>Flintstone<lastname>
<age>38</age>
<spouse>Wilma</spouse>
</person>
...sure, the resulting XML is larger than the prior JSON. But that seems like cheating.
...当然,生成的 XML 比之前的 JSON 大。但这似乎是作弊。
Now it may be that the way you format your XML currently uses elements for everything, and that there's an opportunity to shrink the payload accordingly. But that doesn't necessarily imply JSON. If you've got tools and libraries that handle XML, you can keep XML and shrink.
现在可能是您格式化 XML 的方式当前对所有内容都使用元素,并且有机会相应地缩小有效负载。但这并不一定意味着 JSON。如果您有处理 XML 的工具和库,则可以保留 XML 并缩小。
回答by Miles B.
In terms of object serialization, JSON will generally be more compact (even when compressed). For Example:
在对象序列化方面,JSON 通常会更紧凑(即使在压缩时)。例如:
I serialized the same instance of an object into both XML and JSON and got the following:
我将对象的同一个实例序列化为 XML 和 JSON 并得到以下结果:
JSON
JSON
{
"Account": "2222",
"Login": "124235",
"SalesId": null,
"CustomerReference": "9652358474",
"Status": null,
"DropShip": 0,
"PromoCode": null,
"Notes": "For the truck",
"Errors": null,
"ReferenceId": null,
"PaymentMethod": "CKPhone",
"CheckPayment": {
"Name": "Simon Riggs",
"CompanyName": "Darth Inc",
"AccountNumber": "565555555",
"RoutingNumber": "222224455116",
"CheckNumber": "32",
"Address": {
"Attention": null,
"Street1": "555 W Portebello Rd",
"Street2": null,
"City": "London",
"State": "Texas",
"Zipcode": "45217",
"Country": null,
"ReferenceId": null,
"GetAxType": 2
},
"ReferenceId": null,
"GetAxType": 2
},
"CreditCardPayment": {
"Name": "Simon Riggs",
"CardNumber": "1111222233334444",
"Cvv2": "546",
"Month": 10,
"Year": 2018,
"Address": {
"Attention": null,
"Street1": "555 W Portebello Rd",
"Street2": null,
"City": "London",
"State": "Texas",
"Zipcode": "45217",
"Country": null,
"ReferenceId": null,
"GetAxType": 2
},
"ReferenceId": "0",
"GetAxType": 2
},
"ShippingAddress": {
"Attention": "Simon Riggs",
"Street1": "555 W Portebello Rd",
"Street2": null,
"City": "London",
"State": "Texas",
"Zipcode": "45217",
"Country": null,
"ReferenceId": null,
"GetAxType": 2
},
"Totals": {
"SubTotal": 25.0,
"TotalTax": 5.0,
"ShippingTotal": 10.0,
"ShippingTax": 1.5,
"GrandTotal": 35.0
},
"Lines": [{
"SKU": "1442-4521",
"LineNum": 0.0,
"Qty": 2.0,
"Price": 72.95,
"ShippingClass": "Ground",
"ReferenceId": null,
"GetAxType": 2
},
{
"SKU": "1212-5549",
"LineNum": 0.0,
"Qty": 1.0,
"Price": 31.15,
"ShippingClass": "Ground",
"ReferenceId": null,
"GetAxType": 2
}],
"GetAxType": 2
}
XML
XML
<?xml version="1.0" encoding="utf-16"?>
<SalesOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Account>2222</Account>
<Login>124235</Login>
<CustomerReference>9652358474</CustomerReference>
<DropShip>0</DropShip>
<Notes>For the truck</Notes>
<PaymentMethod>CKPhone</PaymentMethod>
<CheckPayment>
<Name>Simon Riggs</Name>
<CompanyName>Darth Inc</CompanyName>
<AccountNumber>565555555</AccountNumber>
<RoutingNumber>222224455116</RoutingNumber>
<CheckNumber>32</CheckNumber>
<Address>
<Street1>555 W Portebello Rd</Street1>
<City>London</City>
<State>Texas</State>
<Zipcode>45217</Zipcode>
</Address>
</CheckPayment>
<CreditCardPayment>
<Name>Simon Riggs</Name>
<CardNumber>1111222233334444</CardNumber>
<Cvv2>546</Cvv2>
<Month>10</Month>
<Year>2018</Year>
<Address>
<Street1>555 W Portebello Rd</Street1>
<City>London</City>
<State>Texas</State>
<Zipcode>45217</Zipcode>
</Address>
<ReferenceId>0</ReferenceId>
</CreditCardPayment>
<ShippingAddress>
<Attention>Simon Riggs</Attention>
<Street1>555 W Portebello Rd</Street1>
<City>London</City>
<State>Texas</State>
<Zipcode>45217</Zipcode>
</ShippingAddress>
<Totals>
<SubTotal>25</SubTotal>
<TotalTax>5</TotalTax>
<ShippingTotal>10</ShippingTotal>
<ShippingTax>1.5</ShippingTax>
<GrandTotal>35</GrandTotal>
</Totals>
<Lines>
<SalesLine>
<SKU>1442-4521</SKU>
<LineNum>0</LineNum>
<Qty>2</Qty>
<Price>72.95</Price>
<ShippingClass>Ground</ShippingClass>
</SalesLine>
<SalesLine>
<SKU>1212-5549</SKU>
<LineNum>0</LineNum>
<Qty>1</Qty>
<Price>31.15</Price>
<ShippingClass>Ground</ShippingClass>
</SalesLine>
</Lines>
</SalesOrder>
When encoded in ASCII, the JSON is 1422 bytes while the XML is 1954 bytes. After compressing them using a GZipStream, the difference is smaller but still pretty clear. The JSON compresses down to 524 bytes while the XML compresses down to 695 bytes.
以 ASCII 编码时,JSON 为 1422 字节,而 XML 为 1954 字节。使用 GZipStream 压缩它们后,差异较小但仍然很明显。JSON 压缩到 524 字节,而 XML 压缩到 695 字节。
Serialization/deserialization time will vary by implementation (and hardware of course) but I serialized and deserialized the above JSON and XML 100,000 times in a loop and got the total accumulative times:
序列化/反序列化时间将因实现(当然还有硬件)而异,但我在循环中对上述 JSON 和 XML 进行了 100,000 次序列化和反序列化,并获得了总累计时间:
JSON Serialization: 5258 ms, XML Serialization: 3266 ms
JSON 序列化:5258 毫秒,XML 序列化:3266 毫秒
JSON Deserialization: 9582 ms, XML Deserialization: 4604 ms
JSON 反序列化:9582 毫秒,XML 反序列化:4604 毫秒
So XML serializes and deserializes faster using the libraries I'm using (see below), but with averages measuring in the hundredths of milliseconds, I'd say network bandwidth and transfer time is more consequential.
因此,使用我正在使用的库(见下文),XML 序列化和反序列化的速度更快,但平均测量为百分之几毫秒,我认为网络带宽和传输时间更为重要。
(Note: I did this in C# using Microsoft's System.Xml.Serialization.XmlSerializer and JSON.Net's Newtonsoft.Json.JsonConvert classes)
(注意:我在 C# 中使用 Microsoft 的 System.Xml.Serialization.XmlSerializer 和 JSON.Net 的 Newtonsoft.Json.JsonConvert 类做了这个)
回答by Jim Fuller
actually this is harder to answer then it seems,
实际上,这似乎更难回答,
several years ago json was 'faster' but the differences have become finer between the two.
几年前 json 是“更快”,但两者之间的差异变得越来越小。
what I've observed is;
我观察到的是;
- xml compresses better with gzip then json .. time saved in download can offset other components
- xml parsing/querying in raw js is roughly equiv with json
- xml parsing/querying in jquery is much slower ... I won't begrudge jquery developers to focus on json
- xml 使用 gzip 压缩得更好,然后使用 json .. 下载节省的时间可以抵消其他组件
- 原始 js 中的 xml 解析/查询与 json 大致相同
- jquery 中的 xml 解析/查询要慢得多......我不会嫉妒 jquery 开发人员专注于 json
overall, tech that speeds up modern browsers also applies to xml processing as well.
总的来说,加速现代浏览器的技术也适用于 xml 处理。
generally, whenever I hear json touted as a 'lowfat' alternative to XML I wonder if its not some subliminal obsession with weight issues ... thats on my pessimistic days;
通常,每当我听到 json 被吹捧为 XML 的“低脂”替代品时,我想知道它是否不是对体重问题的某种潜意识痴迷……那是在我悲观的日子里;
basically the rule of thumb I follow is
基本上我遵循的经验法则是
markup good for documents json good for data
标记适用于文档 json 适用于数据
and move on ... nothing to see here
继续前进……这里没什么可看的
回答by BCoates
The best way to answer this is to test it yourself, since compression is involved. You also neatly avoid the XML vs JSON holy war by having an objective answer!
回答这个问题的最好方法是自己测试,因为涉及压缩。您还可以通过客观的回答巧妙地避免 XML 与 JSON 的圣战!
Since it's just a test and doesn't really need to work, you could just write up an xml->json converter in javascript that walked the XML DOM tree and copied it into a nested array/object structure then passed it to JSON.stringify(). The only tricky bit would be deciding what becomes an array and what becomes an object.
由于它只是一个测试并且并不真正需要工作,因此您可以在 javascript 中编写一个 xml->json 转换器,该转换器遍历 XML DOM 树并将其复制到嵌套数组/对象结构中,然后将其传递给 JSON.stringify ()。唯一棘手的一点是决定什么成为数组,什么成为对象。
Find a representative sample (or a few) of the data being sent, convert it to JSON with your tool, gzip it and the original XML and compare sizes.
找到正在发送的数据的代表性样本(或几个),使用您的工具将其转换为 JSON,将其与原始 XML 进行 gzip 并比较大小。
Note: I looked around for an online XML->JSON converter and they were all terrible--copius whitespace outside of quotes (illegal in JSON, alters size) and unquoted key names (ditto). Don't use them for your test or you'll get bad data.
注意:我四处寻找一个在线 XML->JSON 转换器,它们都很糟糕——引号之外的 copius 空格(在 JSON 中是非法的,改变大小)和不带引号的键名(同上)。不要将它们用于您的测试,否则您会得到错误的数据。
回答by Marcelo Cantos
Generally speaking, JSON is much faster and smaller than the equivalent XML. XML is richer in that you can store metadata (attributes) and content separately, but the same can be achieved in JSON with appropriate structuring conventions.
一般来说,JSON 比等效的 XML 更快、更小。XML 更丰富,因为您可以分别存储元数据(属性)和内容,但同样可以通过适当的结构约定在 JSON 中实现。
回答by Ben
回答by James Anderson
Yes JSON would be about 30% faster there are fewer characters going over the line and its very quick to parse.
是的,JSON 会快 30% 左右,因为越过该行的字符更少,并且解析速度非常快。
Ypu could also take a look at "YAML" which sends an absolute mininmum of metadata in the message.
Ypu 还可以查看“YAML”,它在消息中发送绝对最小的元数据。
回答by Nicholas Petersen
<person firstname='Fred'
lastname='Flintstone'
age='38'
spouse='Wilma'/>
"person":{"firstname":"Fred",
"lastname":"Flintstone",
"age":38,
"spouse":"Wilma" }
XML: 80 chars
XML:80 个字符
JSON: 92 chars.
JSON:92 个字符。
XML is the 13% slimmer winner, who would have thought for all the bogus claims?
XML 是 13% 苗条的赢家,谁会想到所有的虚假声明?
[Example taken from Cheeso above. Note that carriage return types change the size but equally for both. Without any returns in either, the ratio is XML: 74, JSON: 86, a 14% difference]
[上面取自 Cheeso 的例子。请注意,回车类型会改变大小,但对两者的大小都是一样的。两者都没有任何回报,比率是 XML:74,JSON:86,相差 14%]
Someone claimed in this discussion that "using attributes for content is cheating" (citing [this][1]).
有人在此讨论中声称“将属性用于内容是作弊”(引用 [this][1])。
1) Pray tell how well formed XML is 'cheating'? If it doesn't cheat the (very long) spec, then let's please get over it. Valid is valid.
1) 请告诉我格式良好的 XML 是如何“作弊”的?如果它没有欺骗(很长的)规范,那么让我们克服它。有效即有效。
2) In that case an ASP.NET web.config is 'cheating', among a thousand other big-dog examples.
2) 在这种情况下,ASP.NET web.config 是“作弊”,还有一千个其他大狗示例。
<forms loginUrl="~/Home/Login"
defaultUrl="~/Home"
timeout="2880"
requireSSL="false"
slidingExpiration="true" />
To be fair, there are those who tend to have a bloated-XML mentality in how they form the XML.
公平地说,有些人在如何形成 XML 方面倾向于抱有膨胀的 XML 心态。
But it should not be said that the 13% slimmer XML variety is not just as valid. And thus it even beats JSON (in size).
但不应说精简 13% 的 XML 变体不仅如此有效。因此它甚至击败了 JSON(在大小上)。
[1]: http://www.ibm.com/developerworks/xml/library/x-eleatt/index.html[2004 article, mind you]
[1]:http: //www.ibm.com/developerworks/xml/library/x-eleatt/index.html[2004 文章,请注意]

