How does JSON compare to XML in terms of file size and serialisation/deserialisation time?

前端 未结 8 1857
萌比男神i
萌比男神i 2020-12-28 16:43

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, bu

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 17:09

    In terms of object serialization, JSON will generally be more compact (even when compressed). For Example:

    I serialized the same instance of an object into both XML and JSON and got the following:

    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

    
    
      2222
      124235
      9652358474
      0
      For the truck
      CKPhone
      
        Simon Riggs
        Darth Inc
        565555555
        222224455116
        32
        
    555 W Portebello Rd London Texas 45217
    Simon Riggs 1111222233334444 546 10 2018
    555 W Portebello Rd London Texas 45217
    0
    Simon Riggs 555 W Portebello Rd London Texas 45217 25 5 10 1.5 35 1442-4521 0 2 72.95 Ground 1212-5549 0 1 31.15 Ground

    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.

    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 Serialization: 5258 ms, XML Serialization: 3266 ms

    JSON Deserialization: 9582 ms, XML Deserialization: 4604 ms

    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.

    (Note: I did this in C# using Microsoft's System.Xml.Serialization.XmlSerializer and JSON.Net's Newtonsoft.Json.JsonConvert classes)

提交回复
热议问题