gpt4 book ai didi

xml - JSON 在文件大小和序列化/反序列化时间方面与 XML 相比如何?

转载 作者:IT老高 更新时间:2023-10-28 12:54:41 25 4
gpt4 key购买 nike

由于带宽原因,我有一个应用程序在 Internet 上执行的速度有点慢。我启用了 GZip,它大大缩短了下载时间,但我也在考虑是否可以从 XML 切换到 JSON,以挤出最后一点性能。使用 JSON 会使消息大小显着变小,还是稍微变小一些?假设我们正在讨论 250kB 的 XML 数据(压缩到 30kB)。

最佳答案

在对象序列化方面,JSON 通常会更紧凑(即使在压缩时)。例如:

我将一个对象的同一个实例序列化为 XML 和 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 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>

以 ASCII 编码时,JSON 为 1422 字节,而 XML 为 1954 字节。使用 GZipStream 压缩它们后,差异较小但仍然很明显。 JSON 压缩到 524 字节,而 XML 压缩到 695 字节。

序列化/反序列化时间会因实现(当然还有硬件)而异,但我在一个循环中对上述 JSON 和 XML 进行了 100,000 次序列化和反序列化,并获得了总累积时间:

JSON 序列化:5258 毫秒,XML 序列化:3266 毫秒

JSON 反序列化:9582 毫秒,XML 反序列化:4604 毫秒

因此,使用我正在使用的库(见下文)可以更快地进行 XML 序列化和反序列化,但平均值为百分之几毫秒,我认为网络带宽和传输时间更为重要。

(注意:我在 C# 中使用 Microsoft 的 System.Xml.Serialization.XmlSerializer 和 JSON.Net 的 Newtonsoft.Json.JsonConvert 类)

关于xml - JSON 在文件大小和序列化/反序列化时间方面与 XML 相比如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2673367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com