Created
January 11, 2014 16:11
-
-
Save martinsuchan/8372808 to your computer and use it in GitHub Desktop.
Baseball Odds - different JSON parsing methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Runtime.Serialization; | |
using System.Windows; | |
using System.Windows.Resources; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace BaseballOdds | |
{ | |
[DataContract] | |
public class BaseballOdds | |
{ | |
[DataMember(Name = "isHome")] | |
public int IsHome { get; set; } | |
[DataMember(Name = "inning")] | |
public byte Inning { get; set; } | |
[DataMember(Name = "outs")] | |
public byte Outs { get; set; } | |
[DataMember(Name = "baserunners")] | |
public byte BaseRunners { get; set; } | |
[DataMember(Name = "runDiff")] | |
public sbyte RunDiff { get; set; } | |
[DataMember(Name = "numSituations")] | |
public uint NumSituations { get; set; } | |
[DataMember(Name = "numWins")] | |
public uint NumWins { get; set; } | |
} | |
public partial class MainPage | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
Test(); | |
} | |
private readonly List<Tuple<bool, byte, byte, byte, sbyte>> list1 = new List<Tuple<bool, byte, byte, byte, sbyte>>(); | |
private readonly List<Tuple<UInt32, UInt32>> list2 = new List<Tuple<UInt32, UInt32>>(); | |
private async void Test() | |
{ | |
string allDataString; | |
StreamResourceInfo resource = Application.GetResourceStream(new Uri("file.json", UriKind.Relative)); | |
using (StreamReader sr = new StreamReader(resource.Stream)) | |
{ | |
allDataString = await sr.ReadToEndAsync(); | |
} | |
Stopwatch sw = new Stopwatch(); | |
// test method using JsonConvert | |
sw.Start(); | |
List<BaseballOdds> list = JsonConvert.DeserializeObject<List<BaseballOdds>>(allDataString); | |
sw.Stop(); | |
Debug.WriteLine(sw.Elapsed); | |
// test original method | |
sw.Restart(); | |
JArray allDataArray = JArray.Parse(allDataString); | |
for (int i = 0; i < allDataArray.Count; ++i) | |
{ | |
JObject dataObj = (JObject)(allDataArray[i]); | |
Add(new Tuple<bool, byte, byte, byte, sbyte>( | |
(int)dataObj["isHome"] == 1, (byte)dataObj["inning"], (byte)dataObj["outs"], | |
(byte)dataObj["baserunners"], (sbyte)dataObj["runDiff"]), | |
new Tuple<UInt32, UInt32>((UInt32)dataObj["numSituations"], (UInt32)dataObj["numWins"])); | |
} | |
sw.Stop(); | |
Debug.WriteLine(sw.Elapsed); | |
} | |
private void Add(Tuple<bool, byte, byte, byte, sbyte> t1, Tuple<UInt32, UInt32> t2) | |
{ | |
list1.Add(t1); | |
list2.Add(t2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment