As you probably know the JSON is more compact format than SOAP, which is very usable when you want to reduce the traffic on the network. To illustrate this, assume there is a class called Airport which looks like shown below:
When this class is serialized by using of DataContractSerializer the result is:
<a:Airport><a:Contacts><a:Contact><a:FirstName>huso</a:FirstName><a:LastName>djimbilic</a:LastName></a:Contact>
<a:Contact><a:FirstName>haso</a:FirstName><a:LastName>zgembic</a:LastName></a:Contact></a:Contacts ></a:Contact></a:Contacts><a:Latitude>-161.837997</a:Latitude><a:Longitude>60.779778</a:Longitude><a:Name>PABE</a:Name></a:Airport>
When using REST based service with JSON serialization (DataContractJsonSerializer) the instance of the airport-class sent over the network would look like:
{"Contacts":[{"FirstName":"huso","LastName":"djimbilic"},{"FirstName":"haso","LastName":"zgembic"}],"Latitude":87.652778,"Longitude":34.691111,"Name":"5AL5"}.
My intension is not to describe in detail differences between this two formats, but it is obvious, that the JSON is much compacter.
When we now this, it reasonable to expect that the time needed for consuming of SOAP messages in comparison to JSON messages correlate to the amount of data. If you think this, you are wrong :)
Clearly it means: SOAP transmits 20-40% more data than JSON, but it is (in WCF) faster approximately 20-25% than JSON. How this is possible? The reason is in WCF-JSON implementation itself. WCF internally serializes JSON streams to some kind of XML-Infoset wire format that requires about 50% more bytes than DataContractSerializer.
Comparison by time
Following diagram shows comparison between JSON and SOAP messaging in ten successively repeated tests. Values
in diagram are given in seconds for 3000 repeats per test. For example the first sample for JSON has taken 14 seconds and 10 seconds for SOAP each 3000 repeats. One repeat means send one request and receive response.
In this specific test we retrieved 101 instances of the class Airport (see below XML description of this type).
Exact values in seconds measured in this test ar given in the next table:
JSON | SOAP |
14 | 10 |
17 | 11 |
15 | 12 |
16 | 13 |
17 | 14 |
18 | 14 |
19 | 15 |
20 | 16 |
20 | 17 |
22 | 18 |
Comparison by network traffic
JSON (15862 bytes)
Request
GET /GetJsonCoordinates.svc/GetCoordinates HTTP/1.1
Content-Type: application/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo3f2jA1vh9tDhVf/HkaioegAAAAAjmFHoP+8OkyjmjyXSmDL4rwAh+pBIIdPmQ0493uxJYcACQAA
Host: dado-nb0:81
Connection: Keep-Alive
Response
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Fri, 26 Dec 2008 23:13:23 GMT
Content-Length: 15852
[{"Contacts":[{"FirstName":"huso","LastName":"djimbilic"},{"FirstName":"haso","LastName":"zgembic"}],"Latitude":87.652778,"Longitude":34.691111,"Name":"5AL5"},{"Contacts":[{"FirstName":"damir","LastName":"dobric"},{"FirstName":"alvin","LastName":"dobric"}],"Latitude":-85.468,"Longitude":32.220147,"Name":"AL05"},{"Contacts":[{"FirstName":"damir","LastName":"dobric"},{"FirstName":"alvin","LastName":"dobric"}],"Latitude":-86.124964,"Longitude":32.358472,"Name":"AL12"}:"dobric"},{"FirstName":"alvin","LastName":"dobric"}],"Latitude":-161.837997,"Longitude":60.779778,"Name":"PABE"}]
Note: Response not complete!
SOAP (31872 bytes)
Request
POST /GetSoapCoordinates.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo3j2jA1vh9tDhVf/HkaioegAAAAAjmFHoP+8OkyjmjyXSmDL4rwAh+pBIIdPmQ0493uxJYcACQAA
SOAPAction: "http://tempuri.org/ISoapGpsService/GetCoordinates"
Host: dado-nb0:81
Content-Length: 203
Expect: 100-continue
Connection: Keep-Alive
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetCoordinates xmlns="http://tempuri.org/"><area>GER</area><numOfRecords>100</numOfRecords></GetCoordinates></s:Body></s:Envelope>
Response
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Fri, 26 Dec 2008 23:15:44 GMT
Content-Length: 31872
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetCoordinatesResponse xmlns="http://tempuri.org/"><GetCoordinatesResult xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.ServiceModel.Samples.BasicSyndication" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Airport><a:Contacts><a:Contact><a:FirstName>huso</a:FirstName><a:LastName>djimbilic</a:LastName></a:Contact><a:Contact><a:FirstName>haso</a:FirstName><a:LastName>zgembic</a:LastName></a:Contact></a:Contacts ></a:Contact></a:Contacts><a:Latitude>-161.837997</a:Latitude><a:Longitude>60.779778</a:Longitude><a:Name>PABE</a:Name></a:Airport></GetCoordinatesResult></GetCoordinatesResponse></s:Body></s:Envelope>
Note: Response not complete!
Posted
Dec 27 2008, 03:49 PM
by
Damir Dobric