Last active
May 18, 2021 21:19
-
-
Save stuartd/1d156063c086f4bf214373cc109b4725 to your computer and use it in GitHub Desktop.
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
[MemoryDiagnoser] | |
public class JsonSerializeBenchmark { | |
private const string json = "{\"Name\":\"insert name here\",\"Addresses\":[{\"Line1\":\"Address line 1\",\"Line2\":\"Address line 2\"},{\"Line1\":\"Address line 1\",\"Line2\":\"Address line 2\"}]}"; | |
[Benchmark] | |
public string Deserialize() => DeserializeJson(); | |
[Benchmark] | |
public string Parse() => ParseJson(); | |
private string DeserializeJson() { | |
var data = JsonConvert.DeserializeObject < Data > (json); | |
return JsonConvert.SerializeObject(data, Formatting.Indented); | |
} | |
private string ParseJson() { | |
return JToken.Parse(json).ToString(Formatting.Indented); | |
} | |
} | |
public class Address { | |
public string Line1 { get; set; } | |
public string Line2 { get; set; } | |
} | |
public class Data { | |
public string Name { get; set; } | |
public List <Address> Addresses { get; set; } | |
} | |
public class Program { | |
public static void Main(string[] args) { | |
BenchmarkRunner.Run<JsonSerializeBenchmark>(); | |
// See the output in /bin/Release/../BenchmarkDotNet.Artifacts/results | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment