I was wondering what is the most efficient way to parse JSON in C#? And by efficient I mean the one with the lower response time. I am trying to parse a large amount of data using a couple of methods, and response time in both of these methods are high. Can anyone tell me the difference between the following methods? Is there an alternative that would let me parse with a lower response time?
Option 1:
HttpWebRequest request = WebRequest.Create(jsonURL) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(obj));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
}
Option 2:
var json = new WebClient().DownloadString(jsonURL);
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(obj));
object objResponse = jsonSerializer.ReadObject(ms);
}
You can find a comparison in following link.
The libraries tested:
http://sagistech.blogspot.com/2010/03/parsing-twitter-json-comparing-c.html
Updated:
Added this information based on Matt Johnson's comment
http://theburningmonk.com/2011/11/performance-test-json-serializers-part-ii/
The first method has the opportunity to make less copies of the data. But I have trouble believing that either method makes a measurable difference. Your real cost is going to be network costs.
Still in an early stage but I build a code generator on top of Json.NET that eliminates reflection and speeds up deserialization by a factor of 4.
Checkout CGbR JSON target.
[DataContract]
public partial class Root
{
[DataMember]
public int Number { get; set; }
[DataMember]
public Partial[] Partials { get; set; }
[DataMember]
public IList<ulong> Numbers { get; set; }
}
will generate a partial class:
public Root FromJson(JsonReader reader)
{
while (reader.Read())
{
// Break on EndObject
if (reader.TokenType == JsonToken.EndObject)
break;
// Only look for properties
if (reader.TokenType != JsonToken.PropertyName)
continue;
switch ((string) reader.Value)
{
case "Number":
reader.Read();
Number = Convert.ToInt32(reader.Value);
break;
case "Partials":
reader.Read(); // Read token where array should begin
if (reader.TokenType == JsonToken.Null)
break;
var partials = new List<Partial>();
while (reader.Read() && reader.TokenType == JsonToken.StartObject)
partials.Add(new Partial().FromJson(reader));
Partials = partials.ToArray();
break;
case "Numbers":
reader.Read(); // Read token where array should begin
if (reader.TokenType == JsonToken.Null)
break;
var numbers = new List<ulong>();
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
numbers.Add(Convert.ToUInt64(reader.Value));
Numbers = numbers;
break;
}
}
return this;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With