Which format should I choose to request API in C#

2019-06-28 05:06发布

I'm requesting data from a REST API in C#. The API offers me the same data in a JSON and XML format.

For which one should I go? It's clear which one I would choose in JavaScript. But what in C#? Are there any performance benefits to help me choose one over the other in C# or any good-practice reasons?

Thanks in advance!

标签: c# xml json api rest
6条回答
唯我独甜
2楼-- · 2019-06-28 05:19

You should measure both to determine the performance characteristics of your situation.

In my own usage I started with JSON, for it's obvious 'crossplatform' advantages, but was forced to go with XML after getting major slowdowns on larger data sets in Silverlight/C# (this was reported as a bug in one of my modules where everything would freeze for several seconds - the slowdown was traced to the JSON deserialization of the large dataset it used). Switching to XML resulted in a larger download (gzip compression, which had been planned, was added at this point to counter this) but more then an order of magnitude increase in performance, even with the gzip decompression added.

查看更多
Emotional °昔
3楼-- · 2019-06-28 05:26

Personally I would try both, measure the performance, compare and choose the one that's faster. From your application logic perspective this shouldn't really matter as it will be manipulating objects (not XML, nor JSON). The conversion of the XML/JSON to objects should be abstracted away into a repository which you could, within a blink of an eye, substitute with another implementation if you feel that the one you have chosen initially is slow or it doesn't work for you.

查看更多
你好瞎i
4楼-- · 2019-06-28 05:26

Personally I think that the XML serialization framework in C# is more mature and has a richer feature set than JSON support (for which there are actually two distinct .NET frameworks). But JSON is much more terse, so it depends on the characteristics of your load - whether it will be traffic-heavy, etc.

查看更多
太酷不给撩
5楼-- · 2019-06-28 05:27

C# can deserialize objects from either JSON or XML.

As a general rule XML is going to be more verbose than JSON.

If you are getting back a small dataset, then it won't make much difference, but if you have a larger dataset, then the less verbose JSON might transfer more quickly across the wire.

I'd pull samples of each and compare.

查看更多
我命由我不由天
6楼-- · 2019-06-28 05:36

At some point, possibly during debugging, you may want to inspect or display the raw API response. In that case, you are better off if you have chosen the format that you find easier to read.

查看更多
成全新的幸福
7楼-- · 2019-06-28 05:38

Both JSON and XML have tons of libraries in any language, so the ease of parsing/generating is not very relevant here.

Also though JSON seems to be smaller, (specially if you don't use XML attributes but a pure-element approach) when gzipped both are almost the same

If you are really into speed, I suggest you check Google's protocol buffers. If not it's really a matter of taste, unless you plan to make AJAX calls to that API, in that case I'd pick JSON without doubt.

查看更多
登录 后发表回答